From 58ba012ad70f834a7b4c70dc586679724fa809fd Mon Sep 17 00:00:00 2001 From: regevbr Date: Fri, 1 Oct 2021 22:41:35 +0300 Subject: [PATCH 1/3] fix sync bug --- custom_components/localtuya/common.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 88b434f..d2aa265 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -116,6 +116,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self.dps_to_request = {} self._is_closing = False self._connect_task = None + self._disconnect_task = None self.set_logger(_LOGGER, config_entry[CONF_DEVICE_ID]) # This has to be done in case the device type is type_0d @@ -151,6 +152,14 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): raise Exception("Failed to retrieve status") self.status_updated(status) + + def _new_entity_handler(entity_id): + self.debug("New entity %s was added to %s", entity_id, self._config_entry[CONF_HOST]) + self._dispatch_status() + + """Subscribe localtuya entity events.""" + signal = f"localtuya_entity_{self._config_entry[CONF_DEVICE_ID]}" + self._disconnect_task = async_dispatcher_connect(self._hass, signal, _new_entity_handler) except Exception: # pylint: disable=broad-except self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed") if self._interface is not None: @@ -166,6 +175,8 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): await self._connect_task if self._interface is not None: await self._interface.close() + if self._disconnect_task is not None: + self._disconnect_task() async def set_dp(self, state, dp_index): """Change value of a DP of the Tuya device.""" @@ -195,7 +206,9 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): def status_updated(self, status): """Device updated status.""" self._status.update(status) + self._dispatch_status() + def _dispatch_status(self): signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}" async_dispatcher_send(self._hass, signal, self._status) @@ -223,7 +236,6 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): self.set_logger(logger, self._config_entry.data[CONF_DEVICE_ID]) async def async_added_to_hass(self): - """Subscribe localtuya events.""" await super().async_added_to_hass() self.debug("Adding %s with configuration: %s", self.entity_id, self._config) @@ -243,9 +255,14 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): self.schedule_update_ha_state() signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}" + + """Subscribe localtuya events.""" self.async_on_remove( async_dispatcher_connect(self.hass, signal, _update_handler) ) + """Signal to device entity was added""" + signal = f"localtuya_entity_{self._config_entry.data[CONF_DEVICE_ID]}" + async_dispatcher_send(self.hass, signal, self.entity_id) @property def device_info(self): From 8e94b5f41174e437d743d3a0866ce4bc7db105bc Mon Sep 17 00:00:00 2001 From: regevbr Date: Fri, 1 Oct 2021 22:58:14 +0300 Subject: [PATCH 2/3] fix sync bug --- custom_components/localtuya/common.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index d2aa265..68b0d28 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -134,6 +134,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self._connect_task = asyncio.create_task(self._make_connection()) async def _make_connection(self): + """Subscribe localtuya entity events.""" self.debug("Connecting to %s", self._config_entry[CONF_HOST]) try: @@ -154,12 +155,17 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self.status_updated(status) def _new_entity_handler(entity_id): - self.debug("New entity %s was added to %s", entity_id, self._config_entry[CONF_HOST]) + self.debug( + "New entity %s was added to %s", + entity_id, + self._config_entry[CONF_HOST], + ) self._dispatch_status() - """Subscribe localtuya entity events.""" signal = f"localtuya_entity_{self._config_entry[CONF_DEVICE_ID]}" - self._disconnect_task = async_dispatcher_connect(self._hass, signal, _new_entity_handler) + self._disconnect_task = async_dispatcher_connect( + self._hass, signal, _new_entity_handler + ) except Exception: # pylint: disable=broad-except self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed") if self._interface is not None: @@ -236,6 +242,7 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): self.set_logger(logger, self._config_entry.data[CONF_DEVICE_ID]) async def async_added_to_hass(self): + """Subscribe localtuya events.""" await super().async_added_to_hass() self.debug("Adding %s with configuration: %s", self.entity_id, self._config) @@ -256,11 +263,10 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}" - """Subscribe localtuya events.""" self.async_on_remove( async_dispatcher_connect(self.hass, signal, _update_handler) ) - """Signal to device entity was added""" + signal = f"localtuya_entity_{self._config_entry.data[CONF_DEVICE_ID]}" async_dispatcher_send(self.hass, signal, self.entity_id) From 5a28cec51762b0aa6b9c3d12443d07761ab3b91d Mon Sep 17 00:00:00 2001 From: Bashir <37988348+Brahmah@users.noreply.github.com> Date: Sun, 17 Oct 2021 21:55:38 +1100 Subject: [PATCH 3/3] Update README.md This addition suggests a user close the tuya app prior to proceeding with the config flow to avoid timeouts as experienced by #591 #570 #507 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0030cf3..a4ccf04 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,8 @@ Start by going to Configuration - Integration and pressing the "+" button to cre Wait for 6 seconds for the scanning of the devices in your LAN. Then, a drop-down menu will appear containing the list of detected devices: you can select one of these, or manually input all the parameters. +> **Note: The tuya app on your device must be closed for the following steps to work reliably.** + ![discovery](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/1-discovery.png) If you have selected one entry, you only need to input the device's Friendly Name and the localKey.