Skip to content

Commit

Permalink
fix(cloud_api): remove unnecessary calls (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou authored Jan 24, 2025
1 parent 3c25718 commit 3aa37a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ async def async_step_add_device(self, user_input=None):

if user_input.pop(CONF_MASS_CONFIGURE, False):
# Handle auto configure all recognized devices.
await self.cloud_data.async_get_devices_dps_query()
devices, fails = await setup_localtuya_devices(
self.hass,
self.config_entry.entry_id,
Expand Down Expand Up @@ -1308,8 +1309,8 @@ async def validate_input(hass: HomeAssistant, entry_id, data):
# Get DP descriptions from the cloud, if the device is there.
cloud_dp_codes = {}
cloud_data: TuyaCloudApi = hass.data[DOMAIN][entry_id].cloud_data
if device_cloud_data := cloud_data.device_list.get(data[CONF_DEVICE_ID]):
cloud_dp_codes = device_cloud_data.get("dps_data", {})
if (dev_id := data.get(CONF_DEVICE_ID)) in cloud_data.device_list:
cloud_dp_codes = await cloud_data.async_get_device_functions(dev_id)

# Indicate an error if no datapoints found as the rest of the flow
# won't work in this case
Expand Down
16 changes: 10 additions & 6 deletions custom_components/localtuya/core/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ async def async_get_devices_list(self, force_update=False) -> str | None:

self.device_list.update({dev["id"]: dev for dev in resp["result"]})

# Get Devices DPS Data.
await asyncio.wait(
asyncio.create_task(self.async_get_device_functions(devid))
for devid in self.device_list
)

self._last_devices_update = int(time.time())
return "ok"

async def async_get_devices_dps_query(self):
"""Update All the devices dps_data."""
# Get Devices DPS Data.
async with aiohttp.ClientSession() as self._session:
await asyncio.wait(
asyncio.create_task(self.async_get_device_functions(devid))
for devid in self.device_list
)
return "ok"

async def async_get_device_specifications(self, device_id) -> dict[str, dict]:
"""Obtain the DP ID mappings for a device."""

Expand Down
7 changes: 6 additions & 1 deletion custom_components/localtuya/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.helpers.device_registry import DeviceEntry

from . import HassLocalTuyaData
from .const import CONF_LOCAL_KEY, CONF_USER_ID, DOMAIN
from .const import CONF_LOCAL_KEY, CONF_USER_ID, DOMAIN, CONF_NO_CLOUD

CLOUD_DEVICES = "cloud_devices"
DEVICE_CONFIG = "device_config"
Expand All @@ -31,6 +31,9 @@ async def async_get_config_entry_diagnostics(
data = dict(entry.data)
hass_localtuya: HassLocalTuyaData = hass.data[DOMAIN][entry.entry_id]
tuya_api = hass_localtuya.cloud_data
task_dps_query = None
if data.get(CONF_NO_CLOUD, True) is not True:
await hass.async_create_task(tuya_api.async_get_devices_dps_query())
# censoring private information on integration diagnostic data
for field in [CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_USER_ID]:
data[field] = obfuscate(data[field])
Expand All @@ -44,6 +47,7 @@ async def async_get_config_entry_diagnostics(
for obf, obf_len in DATA_OBFUSCATE.items():
if ob := data[CLOUD_DEVICES][dev_id].get(obf):
data[CLOUD_DEVICES][dev_id][obf] = obfuscate(ob, obf_len, obf_len)

return data


Expand All @@ -61,6 +65,7 @@ async def async_get_device_diagnostics(
hass_localtuya: HassLocalTuyaData = hass.data[DOMAIN][entry.entry_id]
tuya_api = hass_localtuya.cloud_data
if dev_id in tuya_api.device_list:
await tuya_api.async_get_device_functions(dev_id)
data[DEVICE_CLOUD_INFO] = copy.deepcopy(tuya_api.device_list[dev_id])
for obf, obf_len in DATA_OBFUSCATE.items():
if ob := data[DEVICE_CLOUD_INFO].get(obf):
Expand Down

0 comments on commit 3aa37a5

Please sign in to comment.