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

Remap scaled and stepped values

If a mapping causes a value to be scaled or stepped due to a default mapping,
check the map again for a matching rule.

Use case:  For a light that supports 4 brightness levels that are mapped to
strings: level0, level1, level2, level3, these can be mapped to values
0, 85, 170, 255 for the standard 0-255 brightness range.  But if a different
brightness is passed in, we want it to snap to one of those values
- a step of 85 as the default mapping should do that, but we need to pass
it back through the mapping after snapping to the step to translate to the
strings.
Jason Rumney 4 лет назад
Родитель
Сommit
1b9430d2ef
1 измененных файлов с 6 добавлено и 0 удалено
  1. 6 0
      custom_components/tuya_local/helpers/device_config.py

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

@@ -476,11 +476,17 @@ class TuyaDpsConfig:
             if scale != 1 and isinstance(result, (int, float)):
                 _LOGGER.debug(f"Scaling {result} by {scale}")
                 result = result * scale
+                remap = self._find_map_for_value(result)
+                if remap and "dps_val" in remap and "dps_val" not in mapping:
+                    result = remap["dps_val"]
                 replaced = True
 
             if step and isinstance(result, (int, float)):
                 _LOGGER.debug(f"Stepping {result} to {step}")
                 result = step * round(float(result) / step)
+                remap = self._find_map_for_value(result)
+                if remap and "dps_val" in remap and "dps_val" not in mapping:
+                    result = remap["dps_val"]
                 replaced = True
 
             if replaced: