Просмотр исходного кода

Reduce retries when using a single protocol version.

Retries were increased when protocol v3.2 and v3.4 were added to
ensure that each protocol version is tried before giving up.  But
startup performance becomes very poor when devices are offline.  Using
less retries when the protocol version is fixed will let offline
devices fail sooner.

Issue #311

Also increased the time period in which we back off to avoid creating
a race condition right on 1s, where the previous command is not yet
sent but our back-off expires letting the following command go through
with a 1ms delay instead of 1s.
Jason Rumney 3 лет назад
Родитель
Сommit
7b535aca79
1 измененных файлов с 12 добавлено и 4 удалено
  1. 12 4
      custom_components/tuya_local/device.py

+ 12 - 4
custom_components/tuya_local/device.py

@@ -65,7 +65,10 @@ class TuyaLocalDevice(object):
         # its switches.
         self._FAKE_IT_TIL_YOU_MAKE_IT_TIMEOUT = 10
         self._CACHE_TIMEOUT = 20
-        self._CONNECTION_ATTEMPTS = 9
+        # More attempts are needed in auto mode so we can cycle through all
+        # the possibilities a couple of times
+        self._AUTO_CONNECTION_ATTEMPTS = 9
+        self._SINGLE_PROTO_CONNECTION_ATTEMPTS = 3
         self._lock = Lock()
 
     @property
@@ -210,7 +213,7 @@ class TuyaLocalDevice(object):
         # Only delay a second if there was recently another command.
         # Otherwise delay 1ms, to keep things simple by reusing the
         # same send mechanism.
-        waittime = 1 if since < 1.0 else 0.001
+        waittime = 1 if since < 1.1 else 0.001
 
         try:
             self._debounce.cancel()
@@ -247,15 +250,20 @@ class TuyaLocalDevice(object):
     def _retry_on_failed_connection(self, func, error_message):
         if self._api_protocol_version_index is None:
             self._rotate_api_protocol_version()
+        connections = (
+            self._AUTO_CONNECTION_ATTEMPTS
+            if (self._protocol_configured == "auto" and not self._api_protocol_working)
+            else self._SINGLE_PROTO_CONNECTION_ATTEMPTS
+        )
 
-        for i in range(self._CONNECTION_ATTEMPTS):
+        for i in range(connections):
             try:
                 func()
                 self._api_protocol_working = True
                 break
             except Exception as e:
                 _LOGGER.debug(f"Retrying after exception {e}")
-                if i + 1 == self._CONNECTION_ATTEMPTS:
+                if i + 1 == connections:
                     self._reset_cached_state()
                     self._api_protocol_working = False
                     _LOGGER.error(error_message)