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

Swap order of scaling and inverting.

Inversion uses min and max from the config, so needs to be done on the device scaled value, not the HA scale as was previously done.
Jason Rumney 3 лет назад
Родитель
Сommit
1861d6de56
1 измененных файлов с 10 добавлено и 10 удалено
  1. 10 10
      custom_components/tuya_local/helpers/device_config.py

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

@@ -522,16 +522,16 @@ class TuyaDpsConfig:
                 r_dps = self._entity.find_dps(mirror)
                 return r_dps.get_value(device)
 
-            if scale != 1 and isinstance(result, (int, float)):
-                result = result / scale
-                replaced = True
-
             if invert:
                 r = self._config.get("range")
                 if r and "min" in r and "max" in r:
                     result = -1 * result + r["min"] + r["max"]
                     replaced = True
 
+            if scale != 1 and isinstance(result, (int, float)):
+                result = result / scale
+                replaced = True
+
             if replaced:
                 _LOGGER.debug(
                     "%s: Mapped dps %s value from %s to %s",
@@ -631,12 +631,6 @@ class TuyaDpsConfig:
                 r_dps = self._entity.find_dps(redirect)
                 return r_dps.get_values_to_set(device, value)
 
-            if invert:
-                r = self._config.get("range")
-                if r and "min" in r and "max" in r:
-                    result = -1 * result + r["min"] + r["max"]
-                    replaced = True
-
             if scale != 1 and isinstance(result, (int, float)):
                 _LOGGER.debug(f"Scaling {result} by {scale}")
                 result = result * scale
@@ -645,6 +639,12 @@ class TuyaDpsConfig:
                     result = remap["dps_val"]
                 replaced = True
 
+            if invert:
+                r = self._config.get("range")
+                if r and "min" in r and "max" in r:
+                    result = -1 * result + r["min"] + r["max"]
+                    replaced = True
+
             if step and isinstance(result, (int, float)):
                 _LOGGER.debug(f"Stepping {result} to {step}")
                 result = step * round(float(result) / step)