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

feat(device_config): extend conditions to match bitfields

When a condition references a bitfield, perform bitwise matching rather than
insisting on an exact match.

This makes it easier to use feature flags packed into bitfields in conditions.

Simplify logic by refactoring repeated fetching of cond.get("dps_val")
Jason Rumney 1 день назад
Родитель
Сommit
f7f9f3a80e
1 измененных файлов с 10 добавлено и 2 удалено
  1. 10 2
      custom_components/tuya_local/helpers/device_config.py

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

@@ -943,15 +943,23 @@ class TuyaDpsConfig:
             for cond in conditions:
                 if not self.mapping_available(cond, device):
                     continue
-                if c_val is not None and (_equal_or_in(c_val, cond.get("dps_val"))):
+                cond_dpval = cond.get("dps_val")
+                if c_val is not None and (_equal_or_in(c_val, cond_dpval)):
                     c_match = cond
                 # Case where matching None, need extra checks to ensure we
                 # are not just defaulting and it is really a match
                 elif (
                     c_val is None
                     and c_dps is not None
+                    and cond_dpval is None
                     and "dps_val" in cond
-                    and cond.get("dps_val") is None
+                ):
+                    c_match = cond
+                elif (
+                    c_val is not None
+                    and cond_dpval is not None
+                    and c_dps.rawtype == "bitfield"
+                    and (int(c_val) & int(cond_dpval)) == int(cond_dpval)
                 ):
                     c_match = cond
                 # when changing, another condition may become active