Преглед изворни кода

Util functions: add less noisy best_match utility

This only returns the configs that are the best match for given dps.

- add a list of missing dps to config_match and best_match, to more
easily see what is missing from the config to judge whether it is
significant or not.
Jason Rumney пре 2 година
родитељ
комит
5539d13ae9
2 измењених фајлова са 55 додато и 1 уклоњено
  1. 50 0
      util/best_match.py
  2. 5 1
      util/config_match.py

+ 50 - 0
util/best_match.py

@@ -0,0 +1,50 @@
+"""Find matching devices for the supplied dp list"""
+import json
+import sys
+
+from custom_components.tuya_local.helpers.device_config import possible_matches
+
+
+class FakeDevice:
+    def __init__(self, dps):
+        self._dps = dps
+
+    def get_property(self, id):
+        return self._dps.get(id)
+
+    @property
+    def name(self):
+        return "cmdline"
+
+
+def main() -> int:
+    dps = json.loads(" ".join(sys.argv[1:]))
+    device = FakeDevice(dps)
+    best = 0
+    best_matches = set()
+
+    for m in possible_matches(dps):
+        if m.match_quality(dps) > best:
+            best_matches.clear()
+            best = m.match_quality(dps)
+
+        if m.match_quality(dps) == best:
+            best_matches.add(m)
+
+    for m in best_matches:
+        dps_seen = set(dps.keys())
+        print(f"{m.config_type} matched {m.match_quality(dps)}%")
+        print(f"  {m.primary_entity.config_id}:")
+        for dp in m.primary_entity.dps():
+            dps_seen.discard(dp.id)
+            print(f"   {dp.name}: {dp.get_value(device)}")
+        for entity in m.secondary_entities():
+            print(f"  {entity.config_id}:")
+            for dp in entity.dps():
+                dps_seen.discard(dp.id)
+                print(f"    {dp.name}: {dp.get_value(device)}")
+        for dp in dps_seen:
+            print(f"  Missing {dp}: {dps[dp]}")
+
+if __name__ == "__main__":
+    sys.exit(main())

+ 5 - 1
util/config_match.py

@@ -22,15 +22,19 @@ def main() -> int:
     device = FakeDevice(dps)
 
     for match in possible_matches(dps):
+        dps_seen = set(dps.keys())
         print(f"{match.config_type} matched {match.match_quality(dps)}%")
         print(f"  {match.primary_entity.config_id}:")
         for dp in match.primary_entity.dps():
+            dps_seen.discard(dp.id)
             print(f"   {dp.name}: {dp.get_value(device)}")
         for entity in match.secondary_entities():
             print(f"  {entity.config_id}:")
             for dp in entity.dps():
+                dps_seen.discard(dp.id)
                 print(f"    {dp.name}: {dp.get_value(device)}")
-
+        for dp in dps_seen:
+            print(f"  Missing {dp}: {dps[dp]}")
 
 if __name__ == "__main__":
     sys.exit(main())