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

Add tests for missing redirect dps.

- on the way, check that mapping and conditions are lists.
- Somgom single switch: fix a mapping that was incorrectly formatted,
  detected by these new tests.
Jason Rumney 2 лет назад
Родитель
Сommit
10de3514b9
2 измененных файлов с 41 добавлено и 19 удалено
  1. 17 17
      custom_components/tuya_local/devices/somgom_single_switch.yaml
  2. 24 2
      tests/test_device_config.py

+ 17 - 17
custom_components/tuya_local/devices/somgom_single_switch.yaml

@@ -35,23 +35,23 @@ secondary_entities:
         name: option
         name: option
         optional: true
         optional: true
         mapping:
         mapping:
-          conditions:
-            - dps_val: ["0", "1", "2"]
-              mapping:
-                - dps_val: "0"
-                  value: "Off"
-                - dps_val: "1"
-                  value: "On"
-                - dps_val: "2"
-                  value: Last state
-            - dps_val: ["memory", "off", "on"]
-              mapping:
-                - dps_val: "off"
-                  value: "Off"
-                - dps_val: "on"
-                  value: "On"
-                - dps_val: memory
-                  value: Last state
+          - conditions:
+              - dps_val: ["0", "1", "2"]
+                mapping:
+                  - dps_val: "0"
+                    value: "Off"
+                  - dps_val: "1"
+                    value: "On"
+                  - dps_val: "2"
+                    value: Last state
+              - dps_val: ["memory", "off", "on"]
+                mapping:
+                  - dps_val: "off"
+                    value: "Off"
+                  - dps_val: "on"
+                    value: "On"
+                  - dps_val: memory
+                    value: Last state
   - entity: light
   - entity: light
     name: Backlight
     name: Backlight
     category: config
     category: config

+ 24 - 2
tests/test_device_config.py

@@ -228,7 +228,7 @@ class TestDeviceConfig(IsolatedAsyncioTestCase):
     def check_entity(self, entity, cfg):
     def check_entity(self, entity, cfg):
         """
         """
         Check that the entity has a dps list and each dps has an id,
         Check that the entity has a dps list and each dps has an id,
-        type and name.
+        type and name, and any other consistency checks.
         """
         """
         self.assertIsNotNone(
         self.assertIsNotNone(
             entity._config.get("entity"), f"entity type missing in {cfg}"
             entity._config.get("entity"), f"entity type missing in {cfg}"
@@ -240,7 +240,10 @@ class TestDeviceConfig(IsolatedAsyncioTestCase):
         functions = set()
         functions = set()
         extra = set()
         extra = set()
         known = set()
         known = set()
+        redirects = set()
 
 
+        # Basic checks of dps, and initialising of redirects and extras sets
+        # for later checking
         for dp in entity.dps():
         for dp in entity.dps():
             self.assertIsNotNone(
             self.assertIsNotNone(
                 dp._config.get("id"), f"dp id missing from {e} in {cfg}"
                 dp._config.get("id"), f"dp id missing from {e} in {cfg}"
@@ -252,7 +255,26 @@ class TestDeviceConfig(IsolatedAsyncioTestCase):
                 dp._config.get("name"), f"dp name missing from {e} in {cfg}"
                 dp._config.get("name"), f"dp name missing from {e} in {cfg}"
             )
             )
             extra.add(dp.name)
             extra.add(dp.name)
-
+            mappings = dp._config.get("mapping", [])
+            self.assertIsInstance(mappings, list, f"mapping is not a list in {cfg}; entity {e}, dp {dp.name}")
+            for m in mappings:
+                conditions = m.get("conditions", [])
+                self.assertIsInstance(conditions, list, f"conditions is not a list in {cfg}; entity {e}, dp {dp.name}")
+                for c in conditions:
+                    if c.get("value_redirect"):
+                        redirects.add(c.get("value_redirect"))
+                    if c.get("value_mirror"):
+                        redirects.add(c.get("value_mirror"))
+                if m.get("value_redirect"):
+                    redirects.add(m.get("value_redirect"))
+                if m.get("value_mirror"):
+                    redirects.add(m.get("value_mirror"))
+
+        # Check redirects all exist
+        for redirect in redirects:
+            self.assertIn(redirect, extra, f"dp {redirect} missing from {e} in {cfg}")
+
+        # Check dps that are required for this entity type all exist 
         expected = KNOWN_DPS.get(entity.entity)
         expected = KNOWN_DPS.get(entity.entity)
         for rule in expected["required"]:
         for rule in expected["required"]:
             self.assertTrue(
             self.assertTrue(