From 3aa37a5a973a35cbc50a83761287981f87b3b9c7 Mon Sep 17 00:00:00 2001 From: Bander <46300268+xZetsubou@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:04:26 +0300 Subject: [PATCH] fix(cloud_api): remove unnecessary calls (#513) --- custom_components/localtuya/config_flow.py | 5 +++-- custom_components/localtuya/core/cloud_api.py | 16 ++++++++++------ custom_components/localtuya/diagnostics.py | 7 ++++++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index ce8308c7a..30fce5577 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -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, @@ -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 diff --git a/custom_components/localtuya/core/cloud_api.py b/custom_components/localtuya/core/cloud_api.py index 2feb6d318..115096cad 100644 --- a/custom_components/localtuya/core/cloud_api.py +++ b/custom_components/localtuya/core/cloud_api.py @@ -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.""" diff --git a/custom_components/localtuya/diagnostics.py b/custom_components/localtuya/diagnostics.py index 4db57e8a8..cc78f2a29 100644 --- a/custom_components/localtuya/diagnostics.py +++ b/custom_components/localtuya/diagnostics.py @@ -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" @@ -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]) @@ -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 @@ -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):