add_request_dps moved into prepare_setup_entities, plus code cleaning
This commit is contained in:
@@ -55,6 +55,11 @@ def prepare_setup_entities(config_entry, platform):
|
|||||||
config_entry.data[CONF_LOCAL_KEY],
|
config_entry.data[CONF_LOCAL_KEY],
|
||||||
)
|
)
|
||||||
device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION]))
|
device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION]))
|
||||||
|
|
||||||
|
for device_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])
|
||||||
|
|
||||||
return device, entities_to_setup
|
return device, entities_to_setup
|
||||||
|
|
||||||
|
|
||||||
|
@@ -51,7 +51,6 @@ from .const import (
|
|||||||
CONF_STOP_CMD,
|
CONF_STOP_CMD,
|
||||||
)
|
)
|
||||||
from .const import CONF_OPEN_CMD, CONF_CLOSE_CMD, CONF_STOP_CMD
|
from .const import CONF_OPEN_CMD, CONF_CLOSE_CMD, CONF_STOP_CMD
|
||||||
from .pytuya import TuyaDevice
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -88,9 +87,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
|
|
||||||
covers = []
|
covers = []
|
||||||
for device_config in entities_to_setup:
|
for device_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])
|
|
||||||
|
|
||||||
covers.append(
|
covers.append(
|
||||||
LocaltuyaCover(
|
LocaltuyaCover(
|
||||||
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
||||||
|
@@ -250,8 +250,7 @@ class TuyaDevice(object):
|
|||||||
|
|
||||||
# dps 1 must always be sent, otherwise it might fail in case no dps is found in the requested range
|
# dps 1 must always be sent, otherwise it might fail in case no dps is found in the requested range
|
||||||
self.dps_to_request = {"1": None}
|
self.dps_to_request = {"1": None}
|
||||||
for dps in range(2, 11):
|
self.add_dps_to_request(range(2, 11))
|
||||||
self.add_dps_to_request(dps)
|
|
||||||
try:
|
try:
|
||||||
data = self.status()
|
data = self.status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -263,8 +262,7 @@ class TuyaDevice(object):
|
|||||||
return detected_dps
|
return detected_dps
|
||||||
|
|
||||||
self.dps_to_request = {"1": None}
|
self.dps_to_request = {"1": None}
|
||||||
for dps in range(11, 21):
|
self.add_dps_to_request(range(11, 21))
|
||||||
self.add_dps_to_request(dps)
|
|
||||||
try:
|
try:
|
||||||
data = self.status()
|
data = self.status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -273,8 +271,7 @@ class TuyaDevice(object):
|
|||||||
detected_dps.update( data["dps"] )
|
detected_dps.update( data["dps"] )
|
||||||
|
|
||||||
self.dps_to_request = {"1": None}
|
self.dps_to_request = {"1": None}
|
||||||
for dps in range(21, 31):
|
self.add_dps_to_request(range(21, 31))
|
||||||
self.add_dps_to_request(dps)
|
|
||||||
try:
|
try:
|
||||||
data = self.status()
|
data = self.status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -283,8 +280,7 @@ class TuyaDevice(object):
|
|||||||
detected_dps.update( data["dps"] )
|
detected_dps.update( data["dps"] )
|
||||||
|
|
||||||
self.dps_to_request = {"1": None}
|
self.dps_to_request = {"1": None}
|
||||||
for dps in range(100, 111):
|
self.add_dps_to_request(range(100, 111))
|
||||||
self.add_dps_to_request(dps)
|
|
||||||
try:
|
try:
|
||||||
data = self.status()
|
data = self.status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -296,7 +292,10 @@ class TuyaDevice(object):
|
|||||||
return detected_dps
|
return detected_dps
|
||||||
|
|
||||||
def add_dps_to_request(self, dps_index):
|
def add_dps_to_request(self, dps_index):
|
||||||
|
if isinstance(dps_index, int):
|
||||||
self.dps_to_request[str(dps_index)] = None
|
self.dps_to_request[str(dps_index)] = None
|
||||||
|
else:
|
||||||
|
self.dps_to_request.update({str(index): None for index in dps_index})
|
||||||
|
|
||||||
def generate_payload(self, command, data=None):
|
def generate_payload(self, command, data=None):
|
||||||
"""
|
"""
|
||||||
|
@@ -101,14 +101,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
|
|
||||||
switches = []
|
switches = []
|
||||||
for device_config in entities_to_setup:
|
for device_config in entities_to_setup:
|
||||||
# this has to be done in case the device type is type_0d
|
if device_config.get(CONF_CURRENT, "-1") != "-1":
|
||||||
device.add_dps_to_request(device_config[CONF_ID])
|
device.add_dps_to_request(device_config.get(CONF_CURRENT))
|
||||||
if device_config[CONF_CURRENT] != "-1":
|
if device_config.get(CONF_CURRENT_CONSUMPTION, "-1") != "-1":
|
||||||
device.add_dps_to_request(device_config[CONF_CURRENT])
|
device.add_dps_to_request(device_config.get(CONF_CURRENT_CONSUMPTION))
|
||||||
if device_config[CONF_CURRENT_CONSUMPTION] != "-1":
|
if device_config.get(CONF_VOLTAGE, "-1") != "-1":
|
||||||
device.add_dps_to_request(device_config[CONF_CURRENT_CONSUMPTION])
|
device.add_dps_to_request(device_config.get(CONF_VOLTAGE))
|
||||||
if device_config[CONF_VOLTAGE] != "-1":
|
|
||||||
device.add_dps_to_request(device_config[CONF_VOLTAGE])
|
|
||||||
|
|
||||||
switches.append(
|
switches.append(
|
||||||
LocaltuyaSwitch(
|
LocaltuyaSwitch(
|
||||||
@@ -127,8 +125,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
|
|
||||||
class TuyaCache:
|
class TuyaCache:
|
||||||
"""Cache wrapper for pytuya.TuyaDevice"""
|
|
||||||
|
|
||||||
def __init__(self, device, friendly_name):
|
def __init__(self, device, friendly_name):
|
||||||
"""Initialize the cache."""
|
"""Initialize the cache."""
|
||||||
self._cached_status = ""
|
self._cached_status = ""
|
||||||
@@ -222,13 +218,13 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
attrs = {}
|
attrs = {}
|
||||||
if CONF_CURRENT in self._config:
|
if self._config.get(CONF_CURRENT, "-1") != "-1":
|
||||||
attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT])
|
attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT])
|
||||||
if CONF_CURRENT_CONSUMPTION in self._config:
|
if self._config.get(ATTR_CURRENT_CONSUMPTION, "-1") != "-1":
|
||||||
attrs[ATTR_CURRENT_CONSUMPTION] = (
|
attrs[ATTR_CURRENT_CONSUMPTION] = (
|
||||||
self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10
|
self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10
|
||||||
)
|
)
|
||||||
if CONF_VOLTAGE in self._config:
|
if self._config.get(CONF_VOLTAGE, "-1") != "-1":
|
||||||
attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10
|
attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user