Răsfoiți Sursa

Use lock when getting status from device.

Sending commands to the device was protected by a lock, but getting status
could still run in parallel.
This can cause the device to fail to respond, as the Tuya firmware only seems to handle one connection at a time.
Use the same lock so that status requests will wait for commands before firing.
Jason Rumney 5 ani în urmă
părinte
comite
c103d036e2
1 a modificat fișierele cu 11 adăugiri și 6 ștergeri
  1. 11 6
      custom_components/tuya_local/device.py

+ 11 - 6
custom_components/tuya_local/device.py

@@ -162,12 +162,17 @@ class TuyaLocalDevice(object):
         self._pending_updates = {}
 
     def _refresh_cached_state(self):
-        new_state = self._api.status()
-        self._cached_state = new_state["dps"]
-        self._cached_state["updated_at"] = time()
-        _LOGGER.debug(f"refreshed device state: {json.dumps(new_state)}")
-        _LOGGER.debug(
-            f"new cache state (including pending properties): {json.dumps(self._get_cached_state())}"
+        try:
+            self._lock.acquire()
+            new_state = self._api.status()
+            self._cached_state = new_state["dps"]
+            self._cached_state["updated_at"] = time()
+            _LOGGER.debug(f"refreshed device state: {json.dumps(new_state)}")
+            _LOGGER.debug(
+                f"new cache state (including pending properties): {json.dumps(self._get_cached_state())}"
+            )
+        finally:
+            self._lock.release()
         )
 
     def _set_properties(self, properties):