Explorar o código

Add synchronization when checking if it is time to refresh state.

When checking if it is time to refresh status, there is possibility of a race
condition when there are two or more entities assigned to a device, where both
request status at the same time, and both check the last update time before
either can update the update time.

- Fix syntax error in last checkin.
Jason Rumney %!s(int64=5) %!d(string=hai) anos
pai
achega
0797eae5ec
Modificáronse 1 ficheiros con 7 adicións e 4 borrados
  1. 7 4
      custom_components/tuya_local/device.py

+ 7 - 4
custom_components/tuya_local/device.py

@@ -122,9 +122,13 @@ class TuyaLocalDevice(object):
 
     async def async_refresh(self):
         last_updated = self._get_cached_state()["updated_at"]
-        if self._refresh_task is None or time() - last_updated >= self._CACHE_TIMEOUT:
-            self._cached_state["updated_at"] = time()
-            self._refresh_task = self._hass.async_add_executor_job(self.refresh)
+        try:
+            self._lock.aquire()
+            if self._refresh_task is None or time() - last_updated >= self._CACHE_TIMEOUT:
+                self._cached_state["updated_at"] = time()
+                self._refresh_task = self._hass.async_add_executor_job(self.refresh)
+        finally:
+            self._lock.release()
 
         await self._refresh_task
 
@@ -173,7 +177,6 @@ class TuyaLocalDevice(object):
             )
         finally:
             self._lock.release()
-        )
 
     def _set_properties(self, properties):
         if len(properties) == 0: