set_dps now Dispatches status update to all entities

This commit is contained in:
rospogrigio
2020-09-28 23:48:57 +02:00
committed by rospogrigio
parent deeaf0e9eb
commit 0e79570483
2 changed files with 15 additions and 13 deletions

View File

@@ -92,7 +92,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up LocalTuya integration from a config entry.""" """Set up LocalTuya integration from a config entry."""
unsub_listener = entry.add_update_listener(update_listener) unsub_listener = entry.add_update_listener(update_listener)
device = TuyaDevice(entry.data) device = TuyaDevice(hass, entry.data)
async def update_state(now): async def update_state(now):
"""Read device status and update platforms.""" """Read device status and update platforms."""

View File

@@ -4,7 +4,10 @@ from time import time, sleep
from threading import Lock from threading import Lock
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_DEVICE_ID, CONF_DEVICE_ID,
@@ -47,7 +50,7 @@ def get_entity_config(config_entry, dps_id):
class TuyaDevice: class TuyaDevice:
"""Cache wrapper for pytuya.TuyaInterface.""" """Cache wrapper for pytuya.TuyaInterface."""
def __init__(self, config_entry): def __init__(self, hass, config_entry):
"""Initialize the cache.""" """Initialize the cache."""
self._cached_status = "" self._cached_status = ""
self._cached_status_time = 0 self._cached_status_time = 0
@@ -61,6 +64,7 @@ class TuyaDevice:
# 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
self._interface.add_dps_to_request(entity[CONF_ID]) self._interface.add_dps_to_request(entity[CONF_ID])
self._friendly_name = config_entry[CONF_FRIENDLY_NAME] self._friendly_name = config_entry[CONF_FRIENDLY_NAME]
self._hass = hass
self._lock = Lock() self._lock = Lock()
@property @property
@@ -74,10 +78,10 @@ class TuyaDevice:
try: try:
status = self._interface.status() status = self._interface.status()
return status return status
except Exception: except Exception as e:
print( print(
"Failed to update status of device [{}]".format( "Failed to update status of device [{}]: [{}]".format(
self._interface.address self._interface.address, e
) )
) )
sleep(1.0) sleep(1.0)
@@ -99,15 +103,13 @@ class TuyaDevice:
try: try:
result = self._interface.set_dps(state, dps_index) result = self._interface.set_dps(state, dps_index)
self._cached_status["dps"].update(result["dps"]) self._cached_status["dps"].update(result["dps"])
# NOW WE SHOULD TRIGGER status_updated FOR ALL ENTITIES signal = f"localtuya_{self._interface.id}"
# INVOLVED IN result["dps"] : async_dispatcher_send(self._hass, signal, self._cached_status)
# for dp in result["dps"]:
# have status_updated() called....
return return
except Exception: except Exception as e:
print( print(
"Failed to set status of device [{}]".format( "Failed to set status of device [{}]: [{}]".format(
self._interface.address self._interface.address, e
) )
) )
if i + 1 == 3: if i + 1 == 3: