Bläddra i källkod

Fix RGB light tests.

After the previous fix for light brightness after switching to RGBW color mode, the brightness in the tests is incorrect, as the brightness dp was not set in the
test, but is used now to calculate the v in hsv.

Add tests for rgbcw_lightbulb.
Jason Rumney 3 år sedan
förälder
incheckning
52bd6bbe1f

+ 10 - 0
tests/const.py

@@ -1281,3 +1281,13 @@ HYUNDAI_SAHARA_PAYLOAD = {
     "16": False,
     "19": 0,
 }
+
+RGBCW_LIGHTBULB_PAYLOAD = {
+    "20": True,
+    "21": "white",
+    "22": 1000,
+    "23": 500,
+    "24": "0000000003e8",
+    "25": "000e0d0000000000000000c80000",
+    "26": 0,
+}

+ 2 - 0
tests/devices/test_digoo_dgsp01_dual_nightlight_switch.py

@@ -68,6 +68,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
 
     def test_light_rgbw_color(self):
         self.dps[RGBW_DPS] = "ffff00003c6464"
+        self.dps[BRIGHTNESS_DPS] = 255
         self.assertSequenceEqual(
             self.light.rgbw_color,
             (255, 255, 0, 255),
@@ -137,6 +138,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
             await self.light.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
 
     async def test_set_rgbw(self):
+        self.dps[BRIGHTNESS_DPS] = 255
         async with assert_device_properties_set(
             self.light._device,
             {

+ 2 - 0
tests/devices/test_moes_rgb_socket.py

@@ -127,6 +127,7 @@ class TestMoesRGBWSocket(
 
     def test_light_rgbw_color(self):
         self.dps[RGBW_DPS] = "ffff00003cffff"
+        self.dps[BRIGHTNESS_DPS] = 255
         self.assertSequenceEqual(
             self.light.rgbw_color,
             (255, 255, 0, 255),
@@ -185,6 +186,7 @@ class TestMoesRGBWSocket(
             await self.light.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
 
     async def test_set_rgbw(self):
+        self.dps[BRIGHTNESS_DPS] = 255
         async with assert_device_properties_set(
             self.light._device,
             {

+ 147 - 0
tests/devices/test_rgbcw_lightbulb.py

@@ -0,0 +1,147 @@
+from homeassistant.components.light import (
+    ColorMode,
+    LightEntityFeature,
+    EFFECT_COLORLOOP,
+    EFFECT_RANDOM,
+)
+from homeassistant.const import TIME_MINUTES
+
+from ..const import RGBCW_LIGHTBULB_PAYLOAD
+from ..helpers import assert_device_properties_set
+from ..mixins.number import BasicNumberTests
+from .base_device_tests import TuyaDeviceTestCase
+
+SWITCH_DPS = "20"
+MODE_DPS = "21"
+BRIGHTNESS_DPS = "22"
+COLORTEMP_DPS = "23"
+HSV_DPS = "24"
+SCENE_DPS = "25"
+TIMER_DPS = "26"
+
+
+class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
+    __test__ = True
+
+    def setUp(self):
+        self.setUpForConfig("rgbcw_lightbulb.yaml", RGBCW_LIGHTBULB_PAYLOAD)
+        self.subject = self.entities.get("light")
+
+        self.setUpBasicNumber(
+            TIMER_DPS,
+            self.entities.get("number_timer"),
+            max=1440.0,
+            unit=TIME_MINUTES,
+            scale=60,
+        )
+        self.mark_secondary(["number_timer"])
+
+    def test_is_on(self):
+        self.dps[SWITCH_DPS] = True
+        self.assertTrue(self.subject.is_on)
+        self.dps[SWITCH_DPS] = False
+        self.assertFalse(self.subject.is_on)
+
+    def test_brightness(self):
+        self.dps[BRIGHTNESS_DPS] = 500
+        self.assertAlmostEqual(self.subject.brightness, 128, 0)
+
+    def test_color_temp(self):
+        self.dps[COLORTEMP_DPS] = 500
+        self.assertAlmostEqual(self.subject.color_temp, 326, 0)
+        self.dps[COLORTEMP_DPS] = 1000
+        self.assertAlmostEqual(self.subject.color_temp, 153, 0)
+        self.dps[COLORTEMP_DPS] = 0
+        self.assertAlmostEqual(self.subject.color_temp, 500, 0)
+
+    def test_color_mode(self):
+        self.dps[MODE_DPS] = "white"
+        self.assertEqual(self.subject.color_mode, ColorMode.COLOR_TEMP)
+        self.dps[MODE_DPS] = "colour"
+        self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
+        self.dps[MODE_DPS] = "scene"
+        self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
+        self.dps[MODE_DPS] = "music"
+        self.assertEqual(self.subject.color_mode, ColorMode.RGBW)
+
+    def test_rgbw_color(self):
+        self.dps[HSV_DPS] = "003c03e803e8"
+        self.dps[BRIGHTNESS_DPS] = 1000
+        self.assertSequenceEqual(
+            self.subject.rgbw_color,
+            (255, 255, 0, 255),
+        )
+
+    def test_effect_list(self):
+        self.assertCountEqual(
+            self.subject.effect_list,
+            [EFFECT_COLORLOOP, EFFECT_RANDOM],
+        )
+
+    def test_effect(self):
+        self.dps[MODE_DPS] = "scene"
+        self.assertEqual(self.subject.effect, EFFECT_COLORLOOP)
+        self.dps[MODE_DPS] = "music"
+        self.assertEqual(self.subject.effect, EFFECT_RANDOM)
+        self.dps[MODE_DPS] = "white"
+        self.assertIsNone(self.subject.effect)
+        self.dps[MODE_DPS] = "colour"
+        self.assertIsNone(self.subject.effect)
+
+    def test_supported_color_modes(self):
+        self.assertCountEqual(
+            self.subject.supported_color_modes,
+            {ColorMode.RGBW, ColorMode.COLOR_TEMP},
+        )
+
+    def test_supported_features(self):
+        self.assertEqual(self.subject.supported_features, LightEntityFeature.EFFECT)
+
+    async def test_turn_on(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {SWITCH_DPS: True},
+        ):
+            await self.subject.async_turn_on()
+
+    async def test_turn_off(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {SWITCH_DPS: False},
+        ):
+            await self.subject.async_turn_off()
+
+    async def test_set_brightness(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {
+                SWITCH_DPS: True,
+                MODE_DPS: "white",
+                BRIGHTNESS_DPS: 502,
+            },
+        ):
+            await self.subject.async_turn_on(color_mode=ColorMode.WHITE, brightness=128)
+
+    async def test_set_rgbw(self):
+        self.dps[BRIGHTNESS_DPS] = 1000
+        async with assert_device_properties_set(
+            self.subject._device,
+            {
+                SWITCH_DPS: True,
+                MODE_DPS: "colour",
+                HSV_DPS: "000003e803e8",
+            },
+        ):
+            await self.subject.async_turn_on(
+                color_mode=ColorMode.RGBW,
+                rgbw_color=(255, 0, 0, 255),
+            )
+
+    def test_extra_state_attributes(self):
+        self.dps[SCENE_DPS] = "test"
+        self.assertDictEqual(
+            self.subject.extra_state_attributes,
+            {
+                "scene_data": "test",
+            },
+        )

+ 0 - 8
tests/mixins/light.py

@@ -169,14 +169,6 @@ class DimmableLightTests:
         self.dimmableLightOff = offval
         self.dimmableLightTest = tests
 
-    def test_dimmable_light_supported_features(self):
-        self.dps[self.dimmableLightDps] = self.dimmableLightOff
-        self.assertFalse(self.dimmableLight.is_on)
-        self.dps[self.dimmableLightDps] = self.dimmableLightTest[0][0]
-        self.assertTrue(self.dimmableLight.is_on)
-        self.dps[self.dimmableLightDps] = None
-        self.assertFalse(self.dimmableLight.is_on)
-
     def test_dimmable_light_brightness(self):
         self.dps[self.dimmableLightDps] = self.dimmableLightOff
         self.assertEqual(self.dimmableLight.brightness, 0)