Преглед на файлове

light: include "off" in effects if effect is using color_mode dp.

Issue #1598
Jason Rumney преди 1 година
родител
ревизия
59e48159b2

+ 13 - 1
custom_components/tuya_local/light.py

@@ -12,6 +12,7 @@ from homeassistant.components.light import (
     ATTR_EFFECT,
     ATTR_HS_COLOR,
     ATTR_WHITE,
+    EFFECT_OFF,
     ColorMode,
     LightEntity,
     LightEntityFeature,
@@ -213,11 +214,13 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
         if self._effect_dps:
             return self._effect_dps.values(self._device)
         elif self._color_mode_dps:
-            return [
+            effects = [
                 effect
                 for effect in self._color_mode_dps.values(self._device)
                 if effect and not hasattr(ColorMode, effect.upper())
             ]
+            effects.append(EFFECT_OFF)
+            return effects
 
     @property
     def effect(self):
@@ -228,6 +231,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             mode = self._color_mode_dps.get_value(self._device)
             if mode and not hasattr(ColorMode, mode.upper()):
                 return mode
+            return EFFECT_OFF
 
     async def async_turn_on(self, **params):
         settings = {}
@@ -347,6 +351,14 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             elif not self._effect_dps:
                 effect = params.get(ATTR_EFFECT)
                 if effect:
+                    if effect == EFFECT_OFF:
+                        # Turn off the effect. Ideally this should keep the
+                        # previous mode, but since the mode is shared with
+                        # effect, use the default, or first in the list
+                        effect = (
+                            self._color_mode_dps.default
+                            or self._color_mode_dps.values(self.device)[0]
+                        )
                     _LOGGER.debug(
                         "Emulating effect using color mode of %s",
                         effect,

+ 8 - 3
tests/devices/test_digoo_dgsp01_dual_nightlight_switch.py

@@ -1,6 +1,10 @@
 """Tests for the switch entity."""
 
-from homeassistant.components.light import ColorMode, LightEntityFeature
+from homeassistant.components.light import (
+    ColorMode,
+    LightEntityFeature,
+    EFFECT_OFF,
+)
 from homeassistant.components.switch import SwitchDeviceClass
 
 from ..const import DIGOO_DGSP01_SOCKET_PAYLOAD
@@ -79,6 +83,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
                 "Scene 2",
                 "Scene 3",
                 "Scene 4",
+                EFFECT_OFF,
             ],
         )
 
@@ -96,9 +101,9 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
         self.dps[COLORMODE_DPS] = "scene_4"
         self.assertEqual(self.light.effect, "Scene 4")
         self.dps[COLORMODE_DPS] = "white"
-        self.assertIsNone(self.light.effect)
+        self.assertEqual(self.light.effect, EFFECT_OFF)
         self.dps[COLORMODE_DPS] = "colour"
-        self.assertIsNone(self.light.effect)
+        self.assertEqual(self.light.effect, EFFECT_OFF)
 
     def test_light_supported_color_modes(self):
         self.assertCountEqual(

+ 6 - 1
tests/devices/test_moes_rgb_socket.py

@@ -1,6 +1,10 @@
 """Tests for the MoesHouse RGB smart socket."""
 
-from homeassistant.components.light import ColorMode, LightEntityFeature
+from homeassistant.components.light import (
+    ColorMode,
+    LightEntityFeature,
+    EFFECT_OFF,
+)
 from homeassistant.components.sensor import SensorDeviceClass
 from homeassistant.components.switch import SwitchDeviceClass
 from homeassistant.const import (
@@ -143,6 +147,7 @@ class TestMoesRGBSocket(
                 "Scene 2",
                 "Scene 3",
                 "Scene 4",
+                EFFECT_OFF,
             ],
         )
 

+ 8 - 4
tests/devices/test_rgbcw_lightbulb.py

@@ -1,4 +1,8 @@
-from homeassistant.components.light import ColorMode, LightEntityFeature
+from homeassistant.components.light import (
+    ColorMode,
+    LightEntityFeature,
+    EFFECT_OFF,
+)
 from homeassistant.const import UnitOfTime
 
 from ..const import RGBCW_LIGHTBULB_PAYLOAD
@@ -81,7 +85,7 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
     def test_effect_list(self):
         self.assertCountEqual(
             self.subject.effect_list,
-            ["Scene", "Music"],
+            ["Scene", "Music", EFFECT_OFF],
         )
 
     def test_effect(self):
@@ -90,9 +94,9 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
         self.dps[MODE_DPS] = "music"
         self.assertEqual(self.subject.effect, "Music")
         self.dps[MODE_DPS] = "white"
-        self.assertIsNone(self.subject.effect)
+        self.assertEqual(self.subject.effect, EFFECT_OFF)
         self.dps[MODE_DPS] = "colour"
-        self.assertIsNone(self.subject.effect)
+        self.assertEqual(self.subject.effect, EFFECT_OFF)
 
     def test_supported_color_modes(self):
         self.assertCountEqual(