Moved TuyaCache to __init__.py, and renamed to TuyaDevice. In pytuya, renamed TuyaDevice to TuyaInterface
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
from time import time, sleep
|
||||
from threading import Lock
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -21,6 +23,10 @@ from homeassistant.helpers.entity import Entity
|
||||
from . import pytuya
|
||||
from .const import CONF_LOCAL_KEY, CONF_PROTOCOL_VERSION, DOMAIN
|
||||
|
||||
|
||||
import pprint
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_ID = "1"
|
||||
@@ -52,18 +58,18 @@ def prepare_setup_entities(config_entry, platform):
|
||||
if not entities_to_setup:
|
||||
return None, None
|
||||
|
||||
device = pytuya.TuyaDevice(
|
||||
tuyainterface = pytuya.TuyaInterface(
|
||||
config_entry.data[CONF_DEVICE_ID],
|
||||
config_entry.data[CONF_HOST],
|
||||
config_entry.data[CONF_LOCAL_KEY],
|
||||
float(config_entry.data[CONF_PROTOCOL_VERSION]),
|
||||
)
|
||||
device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION]))
|
||||
|
||||
for device_config in entities_to_setup:
|
||||
for interface_config in entities_to_setup:
|
||||
# this has to be done in case the device type is type_0d
|
||||
device.add_dps_to_request(device_config[CONF_ID])
|
||||
tuyainterface.add_dps_to_request(interface_config[CONF_ID])
|
||||
|
||||
return device, entities_to_setup
|
||||
return tuyainterface, entities_to_setup
|
||||
|
||||
|
||||
def import_from_yaml(hass, config, platform):
|
||||
@@ -132,6 +138,80 @@ def get_entity_config(config_entry, dps_id):
|
||||
raise Exception(f"missing entity config for id {dps_id}")
|
||||
|
||||
|
||||
|
||||
class TuyaDevice:
|
||||
"""Cache wrapper for pytuya.TuyaInterface"""
|
||||
|
||||
def __init__(self, interface, friendly_name):
|
||||
"""Initialize the cache."""
|
||||
self._cached_status = ""
|
||||
self._cached_status_time = 0
|
||||
self._interface = interface
|
||||
self._friendly_name = friendly_name
|
||||
self._lock = Lock()
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique device identifier."""
|
||||
return self._interface.id
|
||||
|
||||
def __get_status(self):
|
||||
_LOGGER.debug("running def __get_status from TuyaDevice")
|
||||
for i in range(5):
|
||||
try:
|
||||
status = self._interface.status()
|
||||
# print("STATUS OF [{}] IS [{}]".format(self._interface.address,status))
|
||||
return status
|
||||
except Exception:
|
||||
print(
|
||||
"Failed to update status of device [{}]".format(
|
||||
self._interface.address
|
||||
)
|
||||
)
|
||||
sleep(1.0)
|
||||
if i + 1 == 3:
|
||||
_LOGGER.error(
|
||||
"Failed to update status of device %s", self._interface.address
|
||||
)
|
||||
# return None
|
||||
raise ConnectionError("Failed to update status .")
|
||||
|
||||
def set_dps(self, state, dps_index):
|
||||
#_LOGGER.info("running def set_dps from cover")
|
||||
"""Change the Tuya switch status and clear the cache."""
|
||||
self._cached_status = ""
|
||||
self._cached_status_time = 0
|
||||
for i in range(5):
|
||||
try:
|
||||
#_LOGGER.info("Running a try from def set_dps from cover where state=%s and dps_index=%s", state, dps_index)
|
||||
return self._interface.set_dps(state, dps_index)
|
||||
except Exception:
|
||||
print(
|
||||
"Failed to set status of device [{}]".format(self._interface.address)
|
||||
)
|
||||
if i + 1 == 3:
|
||||
_LOGGER.error(
|
||||
"Failed to set status of device %s", self._interface.address
|
||||
)
|
||||
return
|
||||
|
||||
# raise ConnectionError("Failed to set status.")
|
||||
|
||||
def status(self):
|
||||
"""Get state of Tuya switch and cache the results."""
|
||||
_LOGGER.debug("running def status(self) from TuyaDevice")
|
||||
self._lock.acquire()
|
||||
try:
|
||||
now = time()
|
||||
if not self._cached_status or now - self._cached_status_time > 15:
|
||||
sleep(0.5)
|
||||
self._cached_status = self.__get_status()
|
||||
self._cached_status_time = time()
|
||||
return self._cached_status
|
||||
finally:
|
||||
self._lock.release()
|
||||
|
||||
|
||||
class LocalTuyaEntity(Entity):
|
||||
"""Representation of a Tuya entity."""
|
||||
|
||||
@@ -174,7 +254,7 @@ class LocalTuyaEntity(Entity):
|
||||
|
||||
def dps(self, dps_index):
|
||||
"""Return cached value for DPS index."""
|
||||
value = self._status["dps"].get(dps_index)
|
||||
value = self._status["dps"].get(str(dps_index))
|
||||
if value is None:
|
||||
_LOGGER.warning(
|
||||
"Entity %s is requesting unknown DPS index %s",
|
||||
|
Reference in New Issue
Block a user