Kaynağa Gözat

Light: return brightness from color hsv data in colour mode.

In colour mode, the brightness is set through the colour data for Tuya
lights.  Return it from there as well so it is consistent.

Issue #691
Jason Rumney 2 yıl önce
ebeveyn
işleme
919bcb199e

+ 33 - 19
custom_components/tuya_local/light.py

@@ -134,27 +134,27 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
     @property
     @property
     def brightness(self):
     def brightness(self):
         """Get the current brightness of the light"""
         """Get the current brightness of the light"""
+        if self.raw_color_mode == ColorMode.HS and self._rgbhsv_dps:
+            return self._hsv_brightness
+        return self._white_brightness
+
+    @property
+    def _white_brightness(self):
         if self._brightness_dps:
         if self._brightness_dps:
             return self._brightness_dps.get_value(self._device)
             return self._brightness_dps.get_value(self._device)
 
 
     @property
     @property
-    def hs_color(self):
-        """Get the current hs color of the light"""
+    def _unpacked_rgbhsv(self):
+        """Get the unpacked rgbhsv data"""
         if self._rgbhsv_dps:
         if self._rgbhsv_dps:
-            # color data in hex format RRGGBBHHHHSSVV (14 digit hex)
-            # can also be base64 encoded.
-            # Either RGB or HSV can be used.
-            # Others are color data in hex format HHHHSSSSVVVV (12 digit hex)
             color = self._rgbhsv_dps.decoded_value(self._device)
             color = self._rgbhsv_dps.decoded_value(self._device)
-
             fmt = self._rgbhsv_dps.format
             fmt = self._rgbhsv_dps.format
             if fmt and color:
             if fmt and color:
                 vals = unpack(fmt.get("format"), color)
                 vals = unpack(fmt.get("format"), color)
-                rgbhsv = {}
                 idx = 0
                 idx = 0
+                rgbhsv = {}
                 for v in vals:
                 for v in vals:
-                    # Range in HA is 0-100 for s, 0-255 for rgb and v, 0-360
-                    # for h
+                    # Range in HA is 0-100 for s, 0-255 for rgb and v, 0-360 for h
                     n = fmt["names"][idx]
                     n = fmt["names"][idx]
                     r = fmt["ranges"][idx]
                     r = fmt["ranges"][idx]
                     mx = r["max"]
                     mx = r["max"]
@@ -169,14 +169,29 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                     rgbhsv[n] = round(scale * v)
                     rgbhsv[n] = round(scale * v)
                     idx += 1
                     idx += 1
 
 
-                if "h" in rgbhsv and "s" in rgbhsv and "v" in rgbhsv:
-                    hs = (rgbhsv["h"], rgbhsv["s"])
-                else:
-                    r = rgbhsv.get("r")
-                    g = rgbhsv.get("g")
-                    b = rgbhsv.get("b")
-                    hs = color_util.color_rgb_to_hs(r, g, b)
-                return hs
+                return rgbhsv
+
+    @property
+    def _hsv_brightness(self):
+        """Get the colour mode brightness from the light"""
+        rgbhsv = self._unpacked_rgbhsv
+        if rgbhsv:
+            return rgbhsv.get("v", self._white_brightness)
+        return self._white_brightness
+
+    @property
+    def hs_color(self):
+        """Get the current hs color of the light"""
+        rgbhsv = self._unpacked_rgbhsv
+        if rgbhsv:
+            if "h" in rgbhsv and "s" in rgbhsv:
+                hs = (rgbhsv["h"], rgbhsv["s"])
+            else:
+                r = rgbhsv.get("r")
+                g = rgbhsv.get("g")
+                b = rgbhsv.get("b")
+                hs = color_util.color_rgb_to_hs(r, g, b)
+            return hs
 
 
     @property
     @property
     def effect_list(self):
     def effect_list(self):
@@ -277,7 +292,6 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                             r["min"],
                             r["min"],
                         )
                         )
                         val = r["min"]
                         val = r["min"]
-                    _LOGGER.warning("%s=%d", n, val)
                     ordered.append(val)
                     ordered.append(val)
                     idx += 1
                     idx += 1
                 binary = pack(fmt["format"], *ordered)
                 binary = pack(fmt["format"], *ordered)

+ 2 - 1
tests/devices/test_digoo_dgsp01_dual_nightlight_switch.py

@@ -44,6 +44,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
 
 
     def test_light_brightness(self):
     def test_light_brightness(self):
         self.dps[BRIGHTNESS_DPS] = 45
         self.dps[BRIGHTNESS_DPS] = 45
+        self.dps[COLORMODE_DPS] = "white"
         self.assertEqual(self.light.brightness, 45)
         self.assertEqual(self.light.brightness, 45)
 
 
     def test_light_color_mode(self):
     def test_light_color_mode(self):
@@ -133,7 +134,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
             await self.light.async_turn_on(brightness=128)
             await self.light.async_turn_on(brightness=128)
 
 
     async def test_set_hs_color(self):
     async def test_set_hs_color(self):
-        self.dps[BRIGHTNESS_DPS] = 255
+        self.dps[RGB_DPS] = "ffffff00000064"
         self.dps[COLORMODE_DPS] = "colour"
         self.dps[COLORMODE_DPS] = "colour"
         async with assert_device_properties_set(
         async with assert_device_properties_set(
             self.light._device,
             self.light._device,

+ 4 - 0
tests/devices/test_moes_rgb_socket.py

@@ -106,7 +106,11 @@ class TestMoesRGBSocket(
 
 
     def test_light_brightness(self):
     def test_light_brightness(self):
         self.dps[BRIGHTNESS_DPS] = 45
         self.dps[BRIGHTNESS_DPS] = 45
+        self.dps[MODE_DPS] = "white"
         self.assertEqual(self.light.brightness, 45)
         self.assertEqual(self.light.brightness, 45)
+        self.dps[RGB_DPS] = "808000003cff80"
+        self.dps[MODE_DPS] = "colour"
+        self.assertEqual(self.light.brightness, 128)
 
 
     def test_light_color_mode(self):
     def test_light_color_mode(self):
         self.dps[MODE_DPS] = "colour"
         self.dps[MODE_DPS] = "colour"