فهرست منبع

fix(device): avoid retries, waiting and processing results when stopping

When the receive loop has been stopped, there is no point in processing any
reponse. We should also avoid any between poll waits, and disable tinytuya
internal retries.

This change does not include any micro-optimisations that interfere with
the tinytuya internals or introduce new signals to avoid a potential 5s
delay due to socket timeout or inter-poll timer that already started.

Issue #6339
Jason Rumney 1 روز پیش
والد
کامیت
8f50aa4ff9
1فایلهای تغییر یافته به همراه7 افزوده شده و 2 حذف شده
  1. 7 2
      custom_components/tuya_local/device.py

+ 7 - 2
custom_components/tuya_local/device.py

@@ -225,8 +225,11 @@ class TuyaLocalDevice(object):
         self._children.clear()
         self._force_dps.clear()
         if self._refresh_task:
+            # Prevent any internal tinytuya retries
+            self._api.set_socketRetryLimit(0)
             self._api.set_socketPersistent(False)
             if self._api.parent:
+                self._api.parent.set_socketRetryLimit(0)
                 self._api.parent.set_socketPersistent(False)
             await self._refresh_task
         _LOGGER.debug("Monitor loop for %s stopped", self.name)
@@ -406,7 +409,8 @@ class TuyaLocalDevice(object):
                     force_backoff = True
                     poll = None
 
-                if poll:
+                # Skip processing any response if the device has been stopped while waiting for the response.
+                if poll and self._running:
                     if "Err" in poll:
                         # Limit disconnects to the errors that are caused low level
                         # communication problems
@@ -464,7 +468,8 @@ class TuyaLocalDevice(object):
 
             if not self.has_returned_state or wifi_api.socket is None:
                 force_backoff = True
-            await asyncio.sleep(5 if force_backoff else 0.1)
+            if self.running:
+                await asyncio.sleep(5 if force_backoff else 0.1)
 
         # Close the persistent connection when exiting the loop
         self._api.set_socketPersistent(False)