Merge pull request #5 from rospogrigio/master

integrate master
This commit is contained in:
Rikman
2021-12-13 16:20:20 +01:00
committed by GitHub
2 changed files with 25 additions and 0 deletions

View File

@@ -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 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. 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) ![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. If you have selected one entry, you only need to input the device's Friendly Name and the localKey.

View File

@@ -116,6 +116,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
self.dps_to_request = {} self.dps_to_request = {}
self._is_closing = False self._is_closing = False
self._connect_task = None self._connect_task = None
self._disconnect_task = None
self.set_logger(_LOGGER, config_entry[CONF_DEVICE_ID]) self.set_logger(_LOGGER, config_entry[CONF_DEVICE_ID])
# This has to be done in case the device type is type_0d # This has to be done in case the device type is type_0d
@@ -133,6 +134,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
self._connect_task = asyncio.create_task(self._make_connection()) self._connect_task = asyncio.create_task(self._make_connection())
async def _make_connection(self): async def _make_connection(self):
"""Subscribe localtuya entity events."""
self.debug("Connecting to %s", self._config_entry[CONF_HOST]) self.debug("Connecting to %s", self._config_entry[CONF_HOST])
try: try:
@@ -151,6 +153,19 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
raise Exception("Failed to retrieve status") raise Exception("Failed to retrieve status")
self.status_updated(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()
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 except Exception: # pylint: disable=broad-except
self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed") self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed")
if self._interface is not None: if self._interface is not None:
@@ -166,6 +181,8 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
await self._connect_task await self._connect_task
if self._interface is not None: if self._interface is not None:
await self._interface.close() await self._interface.close()
if self._disconnect_task is not None:
self._disconnect_task()
async def set_dp(self, state, dp_index): async def set_dp(self, state, dp_index):
"""Change value of a DP of the Tuya device.""" """Change value of a DP of the Tuya device."""
@@ -195,7 +212,9 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
def status_updated(self, status): def status_updated(self, status):
"""Device updated status.""" """Device updated status."""
self._status.update(status) self._status.update(status)
self._dispatch_status()
def _dispatch_status(self):
signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}" signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}"
async_dispatcher_send(self._hass, signal, self._status) async_dispatcher_send(self._hass, signal, self._status)
@@ -243,10 +262,14 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
self.schedule_update_ha_state() self.schedule_update_ha_state()
signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}" signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}"
self.async_on_remove( self.async_on_remove(
async_dispatcher_connect(self.hass, signal, _update_handler) async_dispatcher_connect(self.hass, signal, _update_handler)
) )
signal = f"localtuya_entity_{self._config_entry.data[CONF_DEVICE_ID]}"
async_dispatcher_send(self.hass, signal, self.entity_id)
@property @property
def device_info(self): def device_info(self):
"""Return device information for the device registry.""" """Return device information for the device registry."""