diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index ee0da21..ccd9bb1 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -55,7 +55,6 @@ def prepare_setup_entities(config_entry, platform): config_entry.data[CONF_LOCAL_KEY], ) device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION])) - device.set_dpsUsed({}) return device, entities_to_setup diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 2e13040..767f4c4 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -86,12 +86,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if not entities_to_setup: return - # TODO: keeping for now but should be removed - dps = {} - covers = [] for device_config in entities_to_setup: - dps[device_config[CONF_ID]] = None + # this has to be done in case the device type is type_0d + device.add_dps_to_request(device_config[CONF_ID]) + covers.append( LocaltuyaCover( TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), @@ -100,7 +99,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ) ) - device.set_dpsUsed(dps) async_add_entities(covers, True) def setup_platform(hass, config, add_devices, discovery_info=None): diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index d43a168..f2e4e0d 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -61,6 +61,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): lights = [] 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]) + lights.append( LocaltuyaLight( TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index a701597..a3ff81b 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -20,8 +20,8 @@ json = status() # returns json payload set_version(version) # 3.1 [default] or 3.3 detect_available_dps() # returns a list of available dps provided by the device - set_dpsUsed(dpsUsed) - set_dps(on, dps_index) # Set int value of any dps index. + add_dps_to_request(dps_index) # adds dps_index to the list of dps used by the device (to be queried in the payload) + set_dps(on, dps_index) # Set value of any dps index. set_timer(num_secs): @@ -194,6 +194,7 @@ class TuyaDevice(object): self.connection_timeout = connection_timeout self.version = 3.1 self.dev_type = 'type_0a' + self.dps_to_request = {} self.port = 6668 # default - do not expect caller to pass in @@ -248,7 +249,9 @@ class TuyaDevice(object): detected_dps = {} # dps 1 must always be sent, otherwise it might fail in case no dps is found in the requested range - self.set_dpsUsed({ str(dps): None for dps in chain(range(1,2), range(2, 11)) }) + self.dps_to_request = {"1": None} + for dps in range(2, 11): + self.add_dps_to_request(dps) try: data = self.status() except Exception as e: @@ -259,7 +262,9 @@ class TuyaDevice(object): if self.dev_type == "type_0a": return detected_dps - self.set_dpsUsed({ str(dps): None for dps in chain(range(1,2), range(11, 21)) }) + self.dps_to_request = {"1": None} + for dps in range(11, 21): + self.add_dps_to_request(dps) try: data = self.status() except Exception as e: @@ -267,7 +272,9 @@ class TuyaDevice(object): raise CannotConnect detected_dps.update( data["dps"] ) - self.set_dpsUsed({ str(dps): None for dps in chain(range(1,2), range(21, 31)) }) + self.dps_to_request = {"1": None} + for dps in range(21, 31): + self.add_dps_to_request(dps) try: data = self.status() except Exception as e: @@ -275,7 +282,9 @@ class TuyaDevice(object): raise CannotConnect detected_dps.update( data["dps"] ) - self.set_dpsUsed({ str(dps): None for dps in chain(range(1,2), range(100, 111)) }) + self.dps_to_request = {"1": None} + for dps in range(100, 111): + self.add_dps_to_request(dps) try: data = self.status() except Exception as e: @@ -286,8 +295,8 @@ class TuyaDevice(object): return detected_dps - def set_dpsUsed(self, dpsUsed): - self.dpsUsed = dpsUsed + def add_dps_to_request(self, dps_index): + self.dps_to_request[str(dps_index)] = None def generate_payload(self, command, data=None): """ @@ -314,8 +323,8 @@ class TuyaDevice(object): if data is not None: json_data['dps'] = data if command_hb == '0d': - json_data['dps'] = self.dpsUsed -# log.info('******** COMMAND IS %r', self.dpsUsed) + json_data['dps'] = self.dps_to_request +# log.info('******** COMMAND IS %r', self.dps_to_request) # Create byte buffer from hex data json_payload = json.dumps(json_data) diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index acdcae6..9d1b2b8 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -101,6 +101,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): switches = [] 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]) + if device_config[CONF_CURRENT] != "-1": + device.add_dps_to_request(device_config[CONF_CURRENT]) + if device_config[CONF_CURRENT_CONSUMPTION] != "-1": + device.add_dps_to_request(device_config[CONF_CURRENT_CONSUMPTION]) + if device_config[CONF_VOLTAGE] != "-1": + device.add_dps_to_request(device_config[CONF_VOLTAGE]) + switches.append( LocaltuyaSwitch( TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),