فهرست منبع

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 سال پیش
والد
کامیت
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._name = name
         self._api_protocol_version_index = None
         self._api_protocol_version_index = None
+        self._api_protocol_working = False
         self._api = pytuya.Device(dev_id, address, local_key, "device")
         self._api = pytuya.Device(dev_id, address, local_key, "device")
         self._refresh_task = None
         self._refresh_task = None
         self._rotate_api_protocol_version()
         self._rotate_api_protocol_version()
@@ -220,11 +221,13 @@ class TuyaLocalDevice(object):
         for i in range(self._CONNECTION_ATTEMPTS):
         for i in range(self._CONNECTION_ATTEMPTS):
             try:
             try:
                 func()
                 func()
+                self._api_protocol_working = True
                 break
                 break
             except Exception as e:
             except Exception as e:
                 _LOGGER.info(f"Retrying after exception {e}")
                 _LOGGER.info(f"Retrying after exception {e}")
                 if i + 1 == self._CONNECTION_ATTEMPTS:
                 if i + 1 == self._CONNECTION_ATTEMPTS:
                     self._reset_cached_state()
                     self._reset_cached_state()
+                    self._api_protocol_working = False
                     _LOGGER.error(error_message)
                     _LOGGER.error(error_message)
                 else:
                 else:
                     self._rotate_api_protocol_version()
                     self._rotate_api_protocol_version()
@@ -248,7 +251,7 @@ class TuyaLocalDevice(object):
     def _rotate_api_protocol_version(self):
     def _rotate_api_protocol_version(self):
         if self._api_protocol_version_index is None:
         if self._api_protocol_version_index is None:
             self._api_protocol_version_index = 0
             self._api_protocol_version_index = 0
-        else:
+        elif self._api_protocol_working is False:
             self._api_protocol_version_index += 1
             self._api_protocol_version_index += 1
 
 
         if self._api_protocol_version_index >= len(API_PROTOCOL_VERSIONS):
         if self._api_protocol_version_index >= len(API_PROTOCOL_VERSIONS):