Sfoglia il codice sorgente

fix(light): when scale brightness without turn_off value

When light is turned off by setting brightness to 0, the range to scale
the brightness over should not include the 0.

Alternative solution for PR #5218 that does not make the assumption that 0 is always off
even if it may currently be the case for all known configs (I am not sure I trust the
AI analysis on this, as I am sure there have been devices in the past that used 0 as a discrete
non-off brightness value, but there are too many device configs for a manual search).

PR #5218 by @Zuz666
Jason Rumney 3 giorni fa
parent
commit
e1914c63b3
1 ha cambiato i file con 16 aggiunte e 4 eliminazioni
  1. 16 4
      custom_components/tuya_local/light.py

+ 16 - 4
custom_components/tuya_local/light.py

@@ -186,9 +186,21 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
         return self._white_brightness
 
     @property
-    def _white_brightness(self):
+    def _effective_brightness_range(self):
+        """Get the effective brightness range of the light"""
         if self._brightness_dps:
             r = self._brightness_dps.range(self._device)
+            if r:
+                if self._switch_dps is None and r[0] == 0:
+                    # If the light has no switch, and the brightness range starts
+                    # at 0, the effective minimum brightness is 1
+                    r[0] = self._brightness_dps.step(self._device, False)
+                return r
+
+    @property
+    def _white_brightness(self):
+        if self._brightness_dps:
+            r = self._effective_brightness_rang
             val = self._brightness_dps.get_value(self._device)
             if r and val:
                 val = color_util.value_to_brightness(r, val)
@@ -302,7 +314,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                 color_mode = ColorMode.WHITE
             if ATTR_BRIGHTNESS not in params and self._brightness_dps:
                 bright = params.get(ATTR_WHITE)
-                r = self._brightness_dps.range(self._device)
+                r = self._effective_brightness_range
                 if r:
                     bright = _ha_brightness_to_dp_value(bright, r)
 
@@ -475,7 +487,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
         ):
             bright = params.get(ATTR_BRIGHTNESS)
 
-            r = self._brightness_dps.range(self._device)
+            r = self._effective_brightness_range
             if r:
                 bright = _ha_brightness_to_dp_value(bright, r)
             _LOGGER.info("%s setting brightness to %d", self._config.config_id, bright)
@@ -528,7 +540,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             )
         ):
             bright = 255
-            r = self._brightness_dps.range(self._device)
+            r = self._effective_brightness_range
             if r:
                 bright = color_util.brightness_to_value(r, bright)
             _LOGGER.info(