Explorar o código

Alternate calling of status and updatedps.

When dps are set to force, alternate calls to status and updatedps to ensure
that all dps get fresh data, especially after they have been offline and missed
state changes.

Reduce CACHE_TIMEOUT from 2 minutes to 30s to ensure each dps is updated at
least once a minute.

Issue #420
Jason Rumney %!s(int64=3) %!d(string=hai) anos
pai
achega
9f2fcdc3cc
Modificáronse 1 ficheiros con 8 adicións e 2 borrados
  1. 8 2
      custom_components/tuya_local/device.py

+ 8 - 2
custom_components/tuya_local/device.py

@@ -86,7 +86,7 @@ class TuyaLocalDevice(object):
         # we can overlay onto the state while we wait for the board to update
         # we can overlay onto the state while we wait for the board to update
         # its switches.
         # its switches.
         self._FAKE_IT_TIMEOUT = 5
         self._FAKE_IT_TIMEOUT = 5
-        self._CACHE_TIMEOUT = 120
+        self._CACHE_TIMEOUT = 30
         # More attempts are needed in auto mode so we can cycle through all
         # More attempts are needed in auto mode so we can cycle through all
         # the possibilities a couple of times
         # the possibilities a couple of times
         self._AUTO_CONNECTION_ATTEMPTS = len(API_PROTOCOL_VERSIONS) * 2 + 1
         self._AUTO_CONNECTION_ATTEMPTS = len(API_PROTOCOL_VERSIONS) * 2 + 1
@@ -206,6 +206,10 @@ class TuyaLocalDevice(object):
         # If we didn't yet get any state from the device, we may need to
         # If we didn't yet get any state from the device, we may need to
         # negotiate the protocol before making the connection persistent
         # negotiate the protocol before making the connection persistent
         persist = not self.should_poll
         persist = not self.should_poll
+        # flag to alternate updatedps and status calls to ensure we get
+        # all dps updated
+        dps_updated = False
+
         self._api.set_socketPersistent(persist)
         self._api.set_socketPersistent(persist)
         while self._running:
         while self._running:
             try:
             try:
@@ -220,16 +224,18 @@ class TuyaLocalDevice(object):
                     self._api.set_socketPersistent(persist)
                     self._api.set_socketPersistent(persist)
 
 
                 if now - last_cache > self._CACHE_TIMEOUT:
                 if now - last_cache > self._CACHE_TIMEOUT:
-                    if self._force_dps:
+                    if self._force_dps and not dps_updated:
                         poll = await self._retry_on_failed_connection(
                         poll = await self._retry_on_failed_connection(
                             lambda: self._api.updatedps(self._force_dps),
                             lambda: self._api.updatedps(self._force_dps),
                             f"Failed to refresh device state for {self.name}",
                             f"Failed to refresh device state for {self.name}",
                         )
                         )
+                        dps_updated = True
                     else:
                     else:
                         poll = await self._retry_on_failed_connection(
                         poll = await self._retry_on_failed_connection(
                             lambda: self._api.status(),
                             lambda: self._api.status(),
                             f"Failed to refresh device state for {self.name}",
                             f"Failed to refresh device state for {self.name}",
                         )
                         )
+                        dps_updated = False
                 else:
                 else:
                     await self._hass.async_add_executor_job(
                     await self._hass.async_add_executor_job(
                         self._api.heartbeat,
                         self._api.heartbeat,