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,6 +208,9 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
except Exception as ex: # pylint: disable=broad-except
try:
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",
@@ -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,
)

View File

@@ -248,12 +248,6 @@ async def validate_input(hass: core.HomeAssistant, data):
data[CONF_LOCAL_KEY],
float(data[CONF_PROTOCOL_VERSION]),
)
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 = []
@@ -264,6 +258,12 @@ async def validate_input(hass: core.HomeAssistant, data):
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 len(reset_ids) > 0:
await interface.reset(reset_ids)
detected_dps = await interface.detect_available_dps()
except Exception: # pylint: disable=broad-except
@@ -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: