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

Be less eager about rotating the protocol version once it is working

Sometimes devices fail to respond, or respond with a response that pytuya
cannot parse.  This happens especially with the encrypted 3.3 protocol.
Rotating the protocol version seems to make the problem worse, causing the
device to stop responding for some time.
Let the retry counter reach _CONNECTION_ATTEMPTS before starting to rotate
the protocol version if we have successfully communicated until then.
Jason Rumney 5 лет назад
Родитель
Сommit
eca7724c29
1 измененных файлов с 4 добавлено и 1 удалено
  1. 4 1
      custom_components/tuya_local/device.py

+ 4 - 1
custom_components/tuya_local/device.py

@@ -39,6 +39,7 @@ class TuyaLocalDevice(object):
 
         self._name = name
         self._api_protocol_version_index = None
+        self._api_protocol_working = False
         self._api = pytuya.Device(dev_id, address, local_key, "device")
         self._refresh_task = None
         self._rotate_api_protocol_version()
@@ -220,11 +221,13 @@ class TuyaLocalDevice(object):
         for i in range(self._CONNECTION_ATTEMPTS):
             try:
                 func()
+                self._api_protocol_working = True
                 break
             except Exception as e:
                 _LOGGER.info(f"Retrying after exception {e}")
                 if i + 1 == self._CONNECTION_ATTEMPTS:
                     self._reset_cached_state()
+                    self._api_protocol_working = False
                     _LOGGER.error(error_message)
                 else:
                     self._rotate_api_protocol_version()
@@ -248,7 +251,7 @@ class TuyaLocalDevice(object):
     def _rotate_api_protocol_version(self):
         if self._api_protocol_version_index is None:
             self._api_protocol_version_index = 0
-        else:
+        elif self._api_protocol_working is False:
             self._api_protocol_version_index += 1
 
         if self._api_protocol_version_index >= len(API_PROTOCOL_VERSIONS):