Explorar el Código

Utils: refactor to reuse some common functions

- add error handling when files given at command-line cannot be loaded
Jason Rumney hace 1 año
padre
commit
c873440798
Se han modificado 5 ficheros con 62 adiciones y 74 borrados
  1. 1 12
      util/best_match.py
  2. 48 0
      util/common_funcs.py
  3. 1 11
      util/config_match.py
  4. 7 38
      util/duplicates.py
  5. 5 13
      util/match_against.py

+ 1 - 12
util/best_match.py

@@ -4,18 +4,7 @@ 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"
+from common_funcs import FakeDevice
 
 
 def main() -> int:

+ 48 - 0
util/common_funcs.py

@@ -0,0 +1,48 @@
+"""Common functions used in the unilities."""
+
+from custom_components.tuya_local.helpers.device_config import (
+    get_config,
+    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 load_config(filename):
+    """Load the config for the device."""
+    if filename.endswith(".yaml"):
+        filename = filename[:-5]
+    if "/" in filename:
+        filename = filename.split("/")[-1]
+
+    return get_config(filename)
+
+
+def representation(dp):
+    """Return a represenative value for the dp."""
+    if dp.type is bool:
+        return True
+    if dp.type is int:
+        if dp._config.get(range):
+            return dp._config.get(range)["min"]
+        return 0
+    if dp.type is str:
+        return ""
+    if dp.type is float:
+        return 0.0
+
+
+def make_sample_dps(config):
+    """Return a dictionary of sample DPS values."""
+    all_dps = config._get_all_dps()
+    return {dp.id: representation(dp) for dp in all_dps}

+ 1 - 11
util/config_match.py

@@ -5,17 +5,7 @@ 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"
+from common_funcs import FakeDevice
 
 
 def main() -> int:

+ 7 - 38
util/duplicates.py

@@ -2,48 +2,17 @@
 
 import sys
 
-from custom_components.tuya_local.helpers.device_config import (
-    get_config,
-    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 representation(dp):
-    """Return a represenative value for the dp."""
-    if dp.type is bool:
-        return True
-    if dp.type is int:
-        if dp._config.get(range):
-            return dp._config.get(range)["min"]
-        return 0
-    if dp.type is str:
-        return ""
-    if dp.type is float:
-        return 0.0
+from custom_components.tuya_local.helpers.device_config import possible_matches
+from common_funcs import load_config, make_sample_dps
 
 
 def main():
     for filename in sys.argv[1:]:
-        if filename.endswith(".yaml"):
-            filename = filename[:-5]
-        if "/" in filename:
-            filename = filename.split("/")[-1]
-
-        config = get_config(filename)
-        all_dps = config._get_all_dps()
-        sample_dps = {dp.id: representation(dp) for dp in all_dps}
+        config = load_config(filename)
+        if config is None:
+            print(f"No config could be loaded for {filename}")
+            continue
+        sample_dps = make_sample_dps(config)
 
         # device = FakeDevice(sample_dps)
         for m in possible_matches(sample_dps):

+ 5 - 13
util/match_against.py

@@ -7,24 +7,16 @@ from custom_components.tuya_local.helpers.device_config import (
     TuyaDeviceConfig,
     _typematch,
 )
-
-
-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"
+from common_funcs import FakeDevice, load_config
 
 
 def main() -> int:
     dps = json.loads(" ".join(sys.argv[2:]))
     device = FakeDevice(dps)
-    config = TuyaDeviceConfig(sys.argv[1])
+    config = load_config(sys.argv[1])
+    if config is None:
+        print(f"No config could be loaded for {sys.argv[1]}")
+        return 1
     print(f"{config.primary_entity.config_id}:")
     for dp in config.primary_entity.dps():
         if dp.id not in dps.keys():