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

Ignore type when mapping, and improve handling of defaults.

There was an issue where defaults could clobber specific values if encountered later in the loop.
When checking values in the map, convert to string so type differences do not prevent matching.  This happens when config files map integer values, but the Tuya API surrounds them in quotes.
Jason Rumney 4 лет назад
Родитель
Сommit
f26bb0eafb
1 измененных файлов с 53 добавлено и 29 удалено
  1. 53 29
      custom_components/tuya_local/helpers/device_config.py

+ 53 - 29
custom_components/tuya_local/helpers/device_config.py

@@ -203,48 +203,72 @@ class TuyaDpsConfig:
 
 
     def map_from_dps(self, value):
     def map_from_dps(self, value):
         result = value
         result = value
+        replaced = False
+        default_value = None
         scale = 1
         scale = 1
         if "mapping" in self._config.keys():
         if "mapping" in self._config.keys():
             for map in self._config["mapping"]:
             for map in self._config["mapping"]:
-                if "value" in map and ("dps_val" not in map or map["dps_val"] == value):
-                    result = map["value"]
-                    _LOGGER.debug(
-                        "%s: Mapped dps %s value from %s to %s",
-                        self._entity._device.name,
-                        self.id,
-                        value,
-                        result,
-                    )
-                if "scale" in map and "value" not in map:
-                    scale = map["scale"]
-        return (
-            result
-            if scale == 1 or not isinstance(result, (int, float))
-            else result / scale
-        )
+                if "dps_val" not in map:
+                    if "value" in map:
+                        default_value = map["value"]
+                    if "scale" in map:
+                        scale = map["scale"]
+
+                elif str(map["dps_val"]) == str(value):
+                    if "value" in map:
+                        result = map["value"]
+                        replaced = True
+
+        if not replaced and default_value is not None:
+            result = default_value
+            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",
+                self._entity._device.name,
+                self.id,
+                value,
+                result,
+            )
+
+        return result
 
 
     def map_to_dps(self, value):
     def map_to_dps(self, value):
         result = value
         result = value
+        replaced = False
         scale = 1
         scale = 1
         if "mapping" in self._config.keys():
         if "mapping" in self._config.keys():
             for map in self._config["mapping"]:
             for map in self._config["mapping"]:
 
 
-                if "value" in map and "dps_val" in map and map["value"] == value:
+                if (
+                    "value" in map
+                    and "dps_val" in map
+                    and str(map["value"]) == str(value)
+                ):
                     result = map["dps_val"]
                     result = map["dps_val"]
-                    _LOGGER.debug(
-                        "%s: Mapped dps %s to %s from %s",
-                        self._entity._device.name,
-                        self.id,
-                        result,
-                        value,
-                    )
+                    replaced = True
+
                 if "scale" in map and "value" not in map:
                 if "scale" in map and "value" not in map:
                     scale = map["scale"]
                     scale = map["scale"]
-        return (
-            result
-            if scale == 1 or not isinstance(result, (int, float))
-            else result * scale
-        )
+
+        if scale != 1 and isinstance(result, (int, float)):
+            result = result / scale
+            replaced = True
+
+        if replaced:
+            _LOGGER.debug(
+                "%s: Mapped dps %s to %s from %s",
+                self._entity._device.name,
+                self.id,
+                result,
+                value,
+            )
+        return result
 
 
 
 
 def available_configs():
 def available_configs():