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

Do not reverse map hidden values.

A common pattern for optional dps is that we want to map null to a default
value so when it is not reported, we can report a value.
To avoid such a mapping showing up as an option that can be set, we can mark
these mappings as hidden.  But also, these mappings should not be reversed
when setting the value:
Consider:
 - a switch has values on and off (True/False), which we just pass through.
   But the dp is optional, and we want to show it as off when missing, so
   we add a mapping from null to false.  With the previous code, every time
   we turn the switch off from HA, the mapping would be applyed in reverse,
   and we would try to set the dp to null instead of false.  By marking the
   mapping as hidden, we can now avoid that reverse mapping.
Jason Rumney 2 лет назад
Родитель
Сommit
d414bd761f
1 измененных файлов с 14 добавлено и 4 удалено
  1. 14 4
      custom_components/tuya_local/helpers/device_config.py

+ 14 - 4
custom_components/tuya_local/helpers/device_config.py

@@ -801,7 +801,7 @@ class TuyaDpsConfig:
             step = mapping.get("step")
             if not isinstance(step, Number):
                 step = None
-            if "dps_val" in mapping:
+            if "dps_val" in mapping and not mapping.get("hidden", False):
                 result = mapping["dps_val"]
                 replaced = True
             # Conditions may have side effect of setting another value.
@@ -828,7 +828,7 @@ class TuyaDpsConfig:
 
                 # Allow simple conditional mapping overrides
                 for m in cond.get("mapping", {}):
-                    if m.get("value") == value:
+                    if m.get("value") == value and not m.get("hidden", False):
                         result = m.get("dps_val", result)
 
                 step = cond.get("step", step)
@@ -843,7 +843,12 @@ class TuyaDpsConfig:
                 _LOGGER.debug("Scaling %s by %s", result, scale)
                 result = result * scale
                 remap = self._find_map_for_value(result, device)
-                if remap and "dps_val" in remap and "dps_val" not in mapping:
+                if (
+                    remap
+                    and "dps_val" in remap
+                    and "dps_val" not in mapping
+                    and not remap.get("hidden", False)
+                ):
                     result = remap["dps_val"]
                 replaced = True
 
@@ -857,7 +862,12 @@ class TuyaDpsConfig:
                 _LOGGER.debug("Stepping %s to %s", result, step)
                 result = step * round(float(result) / step)
                 remap = self._find_map_for_value(result, device)
-                if remap and "dps_val" in remap and "dps_val" not in mapping:
+                if (
+                    remap
+                    and "dps_val" in remap
+                    and "dps_val" not in mapping
+                    and not remap.get("hidden", False)
+                ):
                     result = remap["dps_val"]
                 replaced = True