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

Output retry log message as debug instead of warning.

Although it is a communication failure, it is expected when the protocol
version has not been detected yet. For persistent failures there is an error
log message after the retry limit is reached.

Include the device name in most log messages, making it easier to see what is
happening.

Don't count updated_at entry when judging match quality.

dps from tinytuya api has an updated_at entry in addition to actual dps.
Don't let this lower the match quality.
This should move everything from an 83.3-92.85% match to a 100% match.
Jason Rumney 4 лет назад
Родитель
Сommit
54d080eedd

+ 15 - 7
custom_components/tuya_local/device.py

@@ -83,16 +83,20 @@ class TuyaLocalDevice(object):
     async def async_inferred_type(self):
 
         cached_state = self._get_cached_state()
-        if "1" not in cached_state and "3" not in cached_state:
+        if len(cached_state) == 1:
             await self.async_refresh()
             cached_state = self._get_cached_state()
 
-        _LOGGER.info(f"Inferring device type from cached state: {cached_state}")
+        _LOGGER.info(
+            f"{self.name} inferring device type from cached state: {cached_state}"
+        )
         best_match = None
         best_quality = 0
         for config in possible_matches(cached_state):
             quality = config.match_quality(cached_state)
-            _LOGGER.info(f"Considering {config.name} with quality {quality}")
+            _LOGGER.info(
+                f"{self.name} considering {config.name} with quality {quality}"
+            )
             if quality > best_quality:
                 best_quality = quality
                 best_match = config
@@ -148,7 +152,7 @@ class TuyaLocalDevice(object):
         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"{self.name} refreshed device state: {json.dumps(new_state)}")
         _LOGGER.debug(
             f"new cache state (including pending properties): {json.dumps(self._get_cached_state())}"
         )
@@ -167,7 +171,9 @@ class TuyaLocalDevice(object):
         for key, value in properties.items():
             pending_updates[key] = {"value": value, "updated_at": now}
 
-        _LOGGER.debug(f"new pending updates: {json.dumps(self._pending_updates)}")
+        _LOGGER.debug(
+            f"{self.name} new pending updates: {json.dumps(self._pending_updates)}"
+        )
 
     def _debounce_sending_updates(self):
         try:
@@ -181,7 +187,9 @@ class TuyaLocalDevice(object):
         pending_properties = self._get_pending_properties()
         payload = self._api.generate_payload(tinytuya.CONTROL, pending_properties)
 
-        _LOGGER.debug(f"sending dps update: {json.dumps(pending_properties)}")
+        _LOGGER.debug(
+            f"{self.name} sending dps update: {json.dumps(pending_properties)}"
+        )
 
         self._retry_on_failed_connection(
             lambda: self._send_payload(payload), "Failed to update device state."
@@ -206,7 +214,7 @@ class TuyaLocalDevice(object):
                 self._api_protocol_working = True
                 break
             except Exception as e:
-                _LOGGER.warning(f"Retrying after exception {e}")
+                _LOGGER.debug(f"Retrying after exception {e}")
                 if i + 1 == self._CONNECTION_ATTEMPTS:
                     self._reset_cached_state()
                     self._api_protocol_working = False

+ 2 - 0
custom_components/tuya_local/helpers/device_config.py

@@ -87,6 +87,8 @@ class TuyaDeviceConfig:
     def match_quality(self, dps):
         """Determine the match quality for the provided dps map."""
         keys = list(dps.keys())
+        if "updated_at" in keys:
+            keys.remove("updated_at")
         total = len(keys)
         for d in self.primary_entity.dps():
             if d.id not in keys or not _typematch(d.type, dps[d.id]):

+ 1 - 1
custom_components/tuya_local/manifest.json

@@ -2,7 +2,7 @@
     "domain": "tuya_local",
     "iot_class": "local_polling",
     "name": "Tuya based devices local control",
-    "version": "0.5.1", 
+    "version": "0.5.2", 
     "documentation": "https://github.com/make-all/tuya-local",
     "issue_tracker": "https://github.com/make-all/tuya-local/issues",
     "dependencies": [],