From fb5d117df40282ec745f4eedb7c7429d157743a7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Oct 2021 06:37:23 -1000 Subject: [PATCH] Always send color/temp when switching from an effect in yeelight (#57745) --- homeassistant/components/yeelight/light.py | 18 +++++-- tests/components/yeelight/test_light.py | 59 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index e40fd2726b2..abe1285f609 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -631,7 +631,11 @@ class YeelightGenericLight(YeelightEntity, LightEntity): """Set bulb's color.""" if not hs_color or COLOR_MODE_HS not in self.supported_color_modes: return - if self.color_mode == COLOR_MODE_HS and self.hs_color == hs_color: + if ( + not self.device.is_color_flow_enabled + and self.color_mode == COLOR_MODE_HS + and self.hs_color == hs_color + ): _LOGGER.debug("HS already set to: %s", hs_color) # Already set, and since we get pushed updates # we avoid setting it again to ensure we do not @@ -648,7 +652,11 @@ class YeelightGenericLight(YeelightEntity, LightEntity): """Set bulb's color.""" if not rgb or COLOR_MODE_RGB not in self.supported_color_modes: return - if self.color_mode == COLOR_MODE_RGB and self.rgb_color == rgb: + if ( + not self.device.is_color_flow_enabled + and self.color_mode == COLOR_MODE_RGB + and self.rgb_color == rgb + ): _LOGGER.debug("RGB already set to: %s", rgb) # Already set, and since we get pushed updates # we avoid setting it again to ensure we do not @@ -667,7 +675,11 @@ class YeelightGenericLight(YeelightEntity, LightEntity): return temp_in_k = mired_to_kelvin(colortemp) - if self.color_mode == COLOR_MODE_COLOR_TEMP and self.color_temp == colortemp: + if ( + not self.device.is_color_flow_enabled + and self.color_mode == COLOR_MODE_COLOR_TEMP + and self.color_temp == colortemp + ): _LOGGER.debug("Color temp already set to: %s", temp_in_k) # Already set, and since we get pushed updates # we avoid setting it again to ensure we do not diff --git a/tests/components/yeelight/test_light.py b/tests/components/yeelight/test_light.py index 9c5a76e4a4b..42ac3675548 100644 --- a/tests/components/yeelight/test_light.py +++ b/tests/components/yeelight/test_light.py @@ -625,6 +625,22 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant): assert mocked_bulb.async_set_brightness.mock_calls == [] mocked_bulb.async_set_rgb.reset_mock() + mocked_bulb.last_properties["flowing"] = "1" + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_RGB_COLOR: (red, green, blue)}, + blocking=True, + ) + assert mocked_bulb.async_set_hsv.mock_calls == [] + assert mocked_bulb.async_set_rgb.mock_calls == [ + call(255, 0, 0, duration=350, light_type=ANY) + ] + assert mocked_bulb.async_set_color_temp.mock_calls == [] + assert mocked_bulb.async_set_brightness.mock_calls == [] + mocked_bulb.async_set_rgb.reset_mock() + mocked_bulb.last_properties["flowing"] = "0" + await hass.services.async_call( "light", SERVICE_TURN_ON, @@ -666,6 +682,22 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant): assert mocked_bulb.async_set_color_temp.mock_calls == [] assert mocked_bulb.async_set_brightness.mock_calls == [] + mocked_bulb.last_properties["flowing"] = "1" + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_COLOR_TEMP: 250}, + blocking=True, + ) + assert mocked_bulb.async_set_hsv.mock_calls == [] + assert mocked_bulb.async_set_rgb.mock_calls == [] + assert mocked_bulb.async_set_color_temp.mock_calls == [ + call(4000, duration=350, light_type=ANY) + ] + assert mocked_bulb.async_set_brightness.mock_calls == [] + mocked_bulb.async_set_color_temp.reset_mock() + mocked_bulb.last_properties["flowing"] = "0" + mocked_bulb.last_properties["color_mode"] = 3 # This last change should generate a call even though # the color mode is the same since the HSV has changed @@ -681,6 +713,33 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant): assert mocked_bulb.async_set_rgb.mock_calls == [] assert mocked_bulb.async_set_color_temp.mock_calls == [] assert mocked_bulb.async_set_brightness.mock_calls == [] + mocked_bulb.async_set_hsv.reset_mock() + + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_HS_COLOR: (100, 35)}, + blocking=True, + ) + assert mocked_bulb.async_set_hsv.mock_calls == [] + assert mocked_bulb.async_set_rgb.mock_calls == [] + assert mocked_bulb.async_set_color_temp.mock_calls == [] + assert mocked_bulb.async_set_brightness.mock_calls == [] + + mocked_bulb.last_properties["flowing"] = "1" + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_HS_COLOR: (100, 35)}, + blocking=True, + ) + assert mocked_bulb.async_set_hsv.mock_calls == [ + call(100.0, 35.0, duration=350, light_type=ANY) + ] + assert mocked_bulb.async_set_rgb.mock_calls == [] + assert mocked_bulb.async_set_color_temp.mock_calls == [] + assert mocked_bulb.async_set_brightness.mock_calls == [] + mocked_bulb.last_properties["flowing"] = "0" async def test_device_types(hass: HomeAssistant, caplog):