diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 5eb6d81..9cefe81 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -208,20 +208,23 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): except Exception as ex: # pylint: disable=broad-except try: - self.debug( - "Initial state update failed, trying reset command " - + "for DP IDs: %s", - self._default_reset_dpids, - ) - await self._interface.reset(self._default_reset_dpids) + if (self._default_reset_dpids is not None) and ( + len(self._default_reset_dpids) > 0 + ): + self.debug( + "Initial state update failed, trying reset command " + + "for DP IDs: %s", + self._default_reset_dpids, + ) + await self._interface.reset(self._default_reset_dpids) - self.debug("Update completed, retrying initial state") - status = await self._interface.status() - if status is None or not status: - raise Exception("Failed to retrieve status") from ex + self.debug("Update completed, retrying initial state") + status = await self._interface.status() + if status is None or not status: + raise Exception("Failed to retrieve status") from ex - self._interface.start_heartbeat() - self.status_updated(status) + self._interface.start_heartbeat() + self.status_updated(status) except UnicodeDecodeError as e: # pylint: disable=broad-except self.exception( @@ -552,10 +555,10 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): Which indicates a DPS that needs to be set before it starts returning status. """ - if not self.restore_on_reconnect and (str(self._dp_id) in self._status): + if (not self.restore_on_reconnect) and (str(self._dp_id) in self._status): self.debug( - "Entity %s (DP %d) - Not restoring as restore on reconnect is \ - disabled for this entity and the entity has an initial status", + "Entity %s (DP %d) - Not restoring as restore on reconnect is " + + "disabled for this entity and the entity has an initial status", self.name, self._dp_id, ) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index e70076f..1eeb3b5 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -248,24 +248,24 @@ async def validate_input(hass: core.HomeAssistant, data): data[CONF_LOCAL_KEY], float(data[CONF_PROTOCOL_VERSION]), ) - + if CONF_RESET_DPIDS in data: + reset_ids_str = data[CONF_RESET_DPIDS].split(",") + reset_ids = [] + for reset_id in reset_ids_str: + reset_ids.append(int(reset_id.strip())) + _LOGGER.debug( + "Reset DPIDs configured: %s (%s)", + data[CONF_RESET_DPIDS], + reset_ids, + ) try: detected_dps = await interface.detect_available_dps() except Exception: # pylint: disable=broad-except try: _LOGGER.debug("Initial state update failed, trying reset command") - if CONF_RESET_DPIDS in data: - reset_ids_str = data[CONF_RESET_DPIDS].split(",") - reset_ids = [] - for reset_id in reset_ids_str: - reset_ids.append(int(reset_id.strip())) - _LOGGER.debug( - "Reset DPIDs configured: %s (%s)", - data[CONF_RESET_DPIDS], - reset_ids, - ) - await interface.reset(reset_ids) - detected_dps = await interface.detect_available_dps() + if len(reset_ids) > 0: + await interface.reset(reset_ids) + detected_dps = await interface.detect_available_dps() except Exception: # pylint: disable=broad-except _LOGGER.debug("No DPS able to be detected") detected_dps = {} @@ -282,7 +282,7 @@ async def validate_input(hass: core.HomeAssistant, data): for new_dps in manual_dps_list + (reset_ids or []): # If the DPS not in the detected dps list, then add with a # default value indicating that it has been manually added - if new_dps not in detected_dps: + if str(new_dps) not in detected_dps: detected_dps[new_dps] = -1 except (ConnectionRefusedError, ConnectionResetError) as ex: