4
0
Эх сурвалжийг харах

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 өдөр өмнө
parent
commit
f7f9f3a80e

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

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