Skip to content

Commit

Permalink
light: include "off" in effects if effect is using color_mode dp.
Browse files Browse the repository at this point in the history
Issue #1598
  • Loading branch information
make-all committed Jun 14, 2024
1 parent 0ba674b commit 59e4815
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
14 changes: 13 additions & 1 deletion custom_components/tuya_local/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_WHITE,
EFFECT_OFF,
ColorMode,
LightEntity,
LightEntityFeature,
Expand Down Expand Up @@ -213,11 +214,13 @@ def effect_list(self):
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):
Expand All @@ -228,6 +231,7 @@ def effect(self):
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 = {}
Expand Down Expand Up @@ -347,6 +351,14 @@ async def async_turn_on(self, **params):
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,
Expand Down
11 changes: 8 additions & 3 deletions tests/devices/test_digoo_dgsp01_dual_nightlight_switch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -79,6 +83,7 @@ def test_light_effect_list(self):
"Scene 2",
"Scene 3",
"Scene 4",
EFFECT_OFF,
],
)

Expand All @@ -96,9 +101,9 @@ def test_light_effect(self):
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(
Expand Down
7 changes: 6 additions & 1 deletion tests/devices/test_moes_rgb_socket.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -143,6 +147,7 @@ def test_light_effect_list(self):
"Scene 2",
"Scene 3",
"Scene 4",
EFFECT_OFF,
],
)

Expand Down
12 changes: 8 additions & 4 deletions tests/devices/test_rgbcw_lightbulb.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -81,7 +85,7 @@ def test_invalid_hs_color(self):
def test_effect_list(self):
self.assertCountEqual(
self.subject.effect_list,
["Scene", "Music"],
["Scene", "Music", EFFECT_OFF],
)

def test_effect(self):
Expand All @@ -90,9 +94,9 @@ def test_effect(self):
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(
Expand Down

0 comments on commit 59e4815

Please sign in to comment.