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

Avoid null mappings being applied in reverse.

When a default is specified using a null mapping, the default value
should not be reverse mapped back to null.  Also the logic should stop
considering the mapping when it detects it as a default mapping, as
the default should only.

Issue #759
Jason Rumney 2 лет назад
Родитель
Сommit
3488430bad

+ 8 - 3
custom_components/tuya_local/devices/README.md

@@ -283,15 +283,20 @@ a simple heater just has a boolean off/on switch.  It can also be used to
 change the icon when a specific mode is operational.  For example if
 a heater device has a fan-only mode, you could change the icon to "mdi:fan"
 instead of "mdi:radiator" when in that mode.
-
+A `dps_val` of `null` can be used to specify a value to be assumed when a
+dp is not being returned by the device, to avoid None in some locations where
+that causes an issue such as entities showing as unavailable.  Such a mapping
+is one-way, the value will not be mapped back to a null when setting the dp.
+x
 ### `value`
 
 *Optional.*
 
 This can be used to set the attribute value seen by Home Assistant to something
 different than the DP value from the Tuya protocol.  Normally it will be used
-with `dps_val` to map from one value to another. It could also be used at top
-level to override all values, but I can't imagine a useful purpose for that.
+with `dps_val` to map from one value to another. Without `dps_val` it will
+one-way map all otherwise unmapped dps values to the specified value.  This
+can be useful for a binary_sensor.
 
 ### `hidden`
 

+ 6 - 0
custom_components/tuya_local/helpers/device_config.py

@@ -689,6 +689,12 @@ class TuyaDpsConfig:
         for m in self._config.get("mapping", {}):
             if "dps_val" not in m:
                 default = m
+            # The following avoids further matching on the above case
+            # and in the null mapping case, which is intended to be
+            # a one-way map to prevent the entity showing as unavailable
+            # when no value is being reported by the device.
+            if m.get("dps_val") is None:
+                continue
             if "value" in m and str(m["value"]) == str(value):
                 return m
             if (