Implementing safe default for devices not requiring RESET

This commit is contained in:
sibowler
2022-09-07 23:21:06 +10:00
parent fa55cd6a38
commit 21f7af6ba8
2 changed files with 32 additions and 29 deletions

View File

@@ -208,20 +208,23 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
except Exception as ex: # pylint: disable=broad-except except Exception as ex: # pylint: disable=broad-except
try: try:
self.debug( if (self._default_reset_dpids is not None) and (
"Initial state update failed, trying reset command " len(self._default_reset_dpids) > 0
+ "for DP IDs: %s", ):
self._default_reset_dpids, self.debug(
) "Initial state update failed, trying reset command "
await self._interface.reset(self._default_reset_dpids) + "for DP IDs: %s",
self._default_reset_dpids,
)
await self._interface.reset(self._default_reset_dpids)
self.debug("Update completed, retrying initial state") self.debug("Update completed, retrying initial state")
status = await self._interface.status() status = await self._interface.status()
if status is None or not status: if status is None or not status:
raise Exception("Failed to retrieve status") from ex raise Exception("Failed to retrieve status") from ex
self._interface.start_heartbeat() self._interface.start_heartbeat()
self.status_updated(status) self.status_updated(status)
except UnicodeDecodeError as e: # pylint: disable=broad-except except UnicodeDecodeError as e: # pylint: disable=broad-except
self.exception( self.exception(
@@ -552,10 +555,10 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
Which indicates a DPS that needs to be set before it starts returning Which indicates a DPS that needs to be set before it starts returning
status. 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( self.debug(
"Entity %s (DP %d) - Not restoring as restore on reconnect is \ "Entity %s (DP %d) - Not restoring as restore on reconnect is "
disabled for this entity and the entity has an initial status", + "disabled for this entity and the entity has an initial status",
self.name, self.name,
self._dp_id, self._dp_id,
) )

View File

@@ -248,24 +248,24 @@ async def validate_input(hass: core.HomeAssistant, data):
data[CONF_LOCAL_KEY], data[CONF_LOCAL_KEY],
float(data[CONF_PROTOCOL_VERSION]), 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: try:
detected_dps = await interface.detect_available_dps() detected_dps = await interface.detect_available_dps()
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
try: try:
_LOGGER.debug("Initial state update failed, trying reset command") _LOGGER.debug("Initial state update failed, trying reset command")
if CONF_RESET_DPIDS in data: if len(reset_ids) > 0:
reset_ids_str = data[CONF_RESET_DPIDS].split(",") await interface.reset(reset_ids)
reset_ids = [] detected_dps = await interface.detect_available_dps()
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()
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
_LOGGER.debug("No DPS able to be detected") _LOGGER.debug("No DPS able to be detected")
detected_dps = {} detected_dps = {}
@@ -282,7 +282,7 @@ async def validate_input(hass: core.HomeAssistant, data):
for new_dps in manual_dps_list + (reset_ids or []): for new_dps in manual_dps_list + (reset_ids or []):
# If the DPS not in the detected dps list, then add with a # If the DPS not in the detected dps list, then add with a
# default value indicating that it has been manually added # 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 detected_dps[new_dps] = -1
except (ConnectionRefusedError, ConnectionResetError) as ex: except (ConnectionRefusedError, ConnectionResetError) as ex: