Browse Source

Light: consider target color mode for brightness adjust method

The logic for controlling brightness relied a lot on the order of operations.
In previous changes to better decide when using the v from hsv was appropriate,
that order was changed, so now there are cases where white brightness overrides
hsv brightness, causing some lights to revert straight to white mode when
they are changed to colour.

This adds an optional parameter for passing the target mode, so that can be
taken into account as well.

Issue #2552
Jason Rumney 1 year ago
parent
commit
17d646a16d

+ 7 - 7
custom_components/tuya_local/light.py

@@ -145,13 +145,12 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             # assume always on if they are responding
             return self.available
 
-    @property
-    def _brightness_control_by_hsv(self):
+    def _brightness_control_by_hsv(self, target_mode=None):
         """Return whether brightness is controlled by HSV."""
         v_available = self._rgbhsv_dps and "v" in self._rgbhsv_dps.format["names"]
         b_available = self._brightness_dps is not None
-        current_raw_mode = self.raw_color_mode
-        current_mode = self.color_mode
+        current_raw_mode = target_mode or self.raw_color_mode
+        current_mode = target_mode or self.color_mode
 
         if current_raw_mode == ColorMode.HS and v_available:
             return True
@@ -164,7 +163,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
     @property
     def brightness(self):
         """Get the current brightness of the light"""
-        if self._brightness_control_by_hsv:
+        if self._brightness_control_by_hsv():
             return self._hsv_brightness
         return self._white_brightness
 
@@ -322,7 +321,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             }
         elif self._rgbhsv_dps and (
             ATTR_HS_COLOR in params
-            or (ATTR_BRIGHTNESS in params and self._brightness_control_by_hsv)
+            or (ATTR_BRIGHTNESS in params and self._brightness_control_by_hsv())
         ):
             if self.color_mode != ColorMode.HS:
                 color_mode = ColorMode.HS
@@ -431,11 +430,12 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
 
         if (
             ATTR_BRIGHTNESS in params
-            and not self._brightness_control_by_hsv
+            and not self._brightness_control_by_hsv(color_mode)
             and self._brightness_dps
         ):
             bright = params.get(ATTR_BRIGHTNESS)
             _LOGGER.debug("Setting brightness to %s", bright)
+
             r = self._brightness_dps.range(self._device)
             if r:
                 bright = color_util.brightness_to_value(r, bright)

+ 1 - 1
custom_components/tuya_local/manifest.json

@@ -15,5 +15,5 @@
         "tinytuya==1.15.1",
         "tuya-device-sharing-sdk>=0.1.9,<=0.2.99"
     ],
-    "version": "2024.12.0"
+    "version": "2024.11.4"
 }