Ensure state is restored when turning on tplink lights without a color mode (#69308)

pull/69372/head
J. Nick Koston 2022-04-05 10:46:12 -10:00 committed by GitHub
parent d6d7f3cb3c
commit 8b2948f030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 50 deletions

View File

@ -318,15 +318,6 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
device: SmartLightStrip
def __init__(
self,
device: SmartLightStrip,
coordinator: TPLinkDataUpdateCoordinator,
) -> None:
"""Initialize the smart light strip."""
super().__init__(device, coordinator)
self._last_custom_effect: dict[str, Any] = {}
@property
def supported_features(self) -> int:
"""Flag supported features."""
@ -351,6 +342,11 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
"""Turn the light on."""
brightness, transition = self._async_extract_brightness_transition(**kwargs)
if ATTR_COLOR_TEMP in kwargs:
if self.effect:
# If there is an effect in progress
# we have to set an HSV value to clear the effect
# before we can set a color temp
await self.device.set_hsv(0, 0, brightness)
await self._async_set_color_temp(
int(kwargs[ATTR_COLOR_TEMP]), brightness, transition
)
@ -358,20 +354,6 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
elif ATTR_EFFECT in kwargs:
await self.device.set_effect(kwargs[ATTR_EFFECT])
elif (
self.device.is_off
and self.device.effect
and self.device.effect["enable"] == 0
and self.device.effect["name"]
):
if not self.device.effect["custom"]:
await self.device.set_effect(self.device.effect["name"])
elif self._last_custom_effect:
await self.device.set_custom_effect(self._last_custom_effect)
# The device does not remember custom effects
# so we must set a default value or it can never turn back on
else:
await self.device.set_hsv(0, 0, 100, transition=transition)
else:
await self._async_turn_on_with_brightness(brightness, transition)
@ -412,7 +394,6 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
if transition_range:
effect["transition_range"] = transition_range
effect["transition"] = 0
self._last_custom_effect = effect
await self.device.set_custom_effect(effect)
async def async_set_sequence_effect(
@ -434,5 +415,4 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
"spread": spread,
"direction": direction,
}
self._last_custom_effect = effect
await self.device.set_custom_effect(effect)

View File

@ -3,7 +3,7 @@
"name": "TP-Link Kasa Smart",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tplink",
"requirements": ["python-kasa==0.4.2"],
"requirements": ["python-kasa==0.4.3"],
"codeowners": ["@rytilahti", "@thegardenmonkey"],
"dependencies": ["network"],
"quality_scale": "platinum",

View File

@ -1898,7 +1898,7 @@ python-join-api==0.0.9
python-juicenet==1.1.0
# homeassistant.components.tplink
python-kasa==0.4.2
python-kasa==0.4.3
# homeassistant.components.lirc
# python-lirc==1.2.3

View File

@ -1242,7 +1242,7 @@ python-izone==1.2.3
python-juicenet==1.1.0
# homeassistant.components.tplink
python-kasa==0.4.2
python-kasa==0.4.3
# homeassistant.components.xiaomi_miio
python-miio==0.5.11

View File

@ -412,6 +412,19 @@ async def test_smart_strip_effects(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_EFFECT] == "Effect1"
assert state.attributes[ATTR_EFFECT_LIST] == ["Effect1", "Effect2"]
# Ensure setting color temp when an effect
# is in progress calls set_hsv to clear the effect
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP: 250},
blocking=True,
)
strip.set_hsv.assert_called_once_with(0, 0, None)
strip.set_color_temp.assert_called_once_with(4000, brightness=None, transition=None)
strip.set_hsv.reset_mock()
strip.set_color_temp.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
@ -444,8 +457,8 @@ async def test_smart_strip_effects(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
strip.set_effect.assert_called_once_with("Effect1")
strip.set_effect.reset_mock()
strip.turn_on.assert_called_once()
strip.turn_on.reset_mock()
strip.is_off = False
strip.is_on = True
@ -539,24 +552,8 @@ async def test_smart_strip_custom_random_effect(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
strip.set_custom_effect.assert_called_once_with(
{
"custom": 1,
"id": "yMwcNpLxijmoKamskHCvvravpbnIqAIN",
"brightness": 100,
"name": "Custom",
"segments": [0],
"expansion_strategy": 1,
"enable": 1,
"duration": 0,
"transition": 0,
"type": "random",
"init_states": [[340, 20, 50]],
"random_seed": 100,
"backgrounds": [(340, 20, 50), (20, 50, 50), (0, 100, 50)],
}
)
strip.set_custom_effect.reset_mock()
strip.turn_on.assert_called_once()
strip.turn_on.reset_mock()
await hass.services.async_call(
DOMAIN,
@ -632,8 +629,8 @@ async def test_smart_strip_custom_random_effect_at_start(hass: HomeAssistant) ->
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
strip.set_hsv.assert_called_with(0, 0, 100, transition=None)
strip.set_hsv.reset_mock()
strip.turn_on.assert_called_once()
strip.turn_on.reset_mock()
async def test_smart_strip_custom_sequence_effect(hass: HomeAssistant) -> None: