Second part of one_device_only big merge
This commit is contained in:
@@ -16,8 +16,6 @@ sensor:
|
||||
device_class: current
|
||||
"""
|
||||
import logging
|
||||
from time import time, sleep
|
||||
from threading import Lock
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -26,11 +24,7 @@ from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASSES_SCHEMA,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_ID,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_FRIENDLY_NAME,
|
||||
)
|
||||
from homeassistant.const import CONF_ID, CONF_DEVICE_CLASS
|
||||
|
||||
from .common import LocalTuyaEntity, prepare_setup_entities
|
||||
|
||||
@@ -51,7 +45,9 @@ def flow_schema(dps):
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up a Tuya sensor based on a config entry."""
|
||||
device, entities_to_setup = prepare_setup_entities(config_entry, DOMAIN)
|
||||
tuyainterface, entities_to_setup = prepare_setup_entities(
|
||||
hass, config_entry, DOMAIN
|
||||
)
|
||||
if not entities_to_setup:
|
||||
return
|
||||
|
||||
@@ -59,7 +55,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
for device_config in entities_to_setup:
|
||||
sensors.append(
|
||||
LocaltuyaBinarySensor(
|
||||
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
||||
tuyainterface,
|
||||
config_entry,
|
||||
device_config[CONF_ID],
|
||||
)
|
||||
@@ -68,74 +64,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
async_add_entities(sensors, True)
|
||||
|
||||
|
||||
class TuyaCache:
|
||||
"""Cache wrapper for pytuya.TuyaDevice."""
|
||||
|
||||
def __init__(self, device, friendly_name):
|
||||
"""Initialize the cache."""
|
||||
self._cached_status = ""
|
||||
self._cached_status_time = 0
|
||||
self._device = device
|
||||
self._friendly_name = friendly_name
|
||||
self._lock = Lock()
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique device identifier."""
|
||||
return self._device.id
|
||||
|
||||
def __get_status(self):
|
||||
for i in range(5):
|
||||
try:
|
||||
status = self._device.status()
|
||||
return status
|
||||
except Exception:
|
||||
print(
|
||||
"Failed to update status of device [{}]".format(
|
||||
self._device.address
|
||||
)
|
||||
)
|
||||
sleep(1.0)
|
||||
if i + 1 == 3:
|
||||
_LOGGER.error(
|
||||
"Failed to update status of device %s", self._device.address
|
||||
)
|
||||
# return None
|
||||
raise ConnectionError("Failed to update status .")
|
||||
|
||||
def set_dps(self, state, dps_index):
|
||||
"""Change the Tuya sensor status and clear the cache."""
|
||||
self._cached_status = ""
|
||||
self._cached_status_time = 0
|
||||
for i in range(5):
|
||||
try:
|
||||
return self._device.set_dps(state, dps_index)
|
||||
except Exception:
|
||||
print(
|
||||
"Failed to set status of device [{}]".format(self._device.address)
|
||||
)
|
||||
if i + 1 == 3:
|
||||
_LOGGER.error(
|
||||
"Failed to set status of device %s", self._device.address
|
||||
)
|
||||
return
|
||||
|
||||
# raise ConnectionError("Failed to set status.")
|
||||
|
||||
def status(self):
|
||||
"""Get state of Tuya sensor and cache the results."""
|
||||
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 LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity):
|
||||
"""Representation of a Tuya binary sensor."""
|
||||
|
||||
|
Reference in New Issue
Block a user