From faecc90b389dd47ee36dcf1c94d9d0b644b5c4c8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 31 Oct 2021 10:11:07 -0500 Subject: [PATCH] Workaround brightness transition delay from off in older yeelight models (#58774) --- homeassistant/components/yeelight/__init__.py | 14 +++++++++++++ homeassistant/components/yeelight/light.py | 6 +++++- tests/components/yeelight/test_light.py | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/yeelight/__init__.py b/homeassistant/components/yeelight/__init__.py index e57979a7ea2..1408cb56709 100644 --- a/homeassistant/components/yeelight/__init__.py +++ b/homeassistant/components/yeelight/__init__.py @@ -35,6 +35,20 @@ _LOGGER = logging.getLogger(__name__) STATE_CHANGE_TIME = 0.40 # seconds POWER_STATE_CHANGE_TIME = 1 # seconds +# +# These models do not transition correctly when turning on, and +# yeelight is no longer updating the firmware on older devices +# +# https://github.com/home-assistant/core/issues/58315 +# +# The problem can be worked around by always setting the brightness +# even when the bulb is reporting the brightness is already at the +# desired level. +# +MODELS_WITH_DELAYED_ON_TRANSITION = { + "color", # YLDP02YL +} + DOMAIN = "yeelight" DATA_YEELIGHT = DOMAIN DATA_UPDATED = "yeelight_{}_data_updated" diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index a6b51046fc6..3d84a30f44e 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -63,6 +63,7 @@ from . import ( DATA_DEVICE, DATA_UPDATED, DOMAIN, + MODELS_WITH_DELAYED_ON_TRANSITION, POWER_STATE_CHANGE_TIME, YEELIGHT_FLOW_TRANSITION_SCHEMA, YeelightEntity, @@ -614,7 +615,10 @@ class YeelightGenericLight(YeelightEntity, LightEntity): """Set bulb brightness.""" if not brightness: return - if math.floor(self.brightness) == math.floor(brightness): + if ( + math.floor(self.brightness) == math.floor(brightness) + and self._bulb.model not in MODELS_WITH_DELAYED_ON_TRANSITION + ): _LOGGER.debug("brightness already set to: %s", brightness) # 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 a4e9e2d9746..4377efe129f 100644 --- a/tests/components/yeelight/test_light.py +++ b/tests/components/yeelight/test_light.py @@ -641,6 +641,25 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant): mocked_bulb.async_set_rgb.reset_mock() mocked_bulb.last_properties["flowing"] = "0" + mocked_bulb.model = "color" # color model needs a workaround (see MODELS_WITH_DELAYED_ON_TRANSITION) + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ENTITY_LIGHT, + ATTR_BRIGHTNESS_PCT: PROPERTIES["bright"], + }, + 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 == [ + call(pytest.approx(50.1, 0.1), duration=350, light_type=ANY) + ] + mocked_bulb.async_set_brightness.reset_mock() + + mocked_bulb.model = "colora" # colora does not need a workaround await hass.services.async_call( "light", SERVICE_TURN_ON, @@ -683,6 +702,7 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant): assert mocked_bulb.async_set_brightness.mock_calls == [] mocked_bulb.last_properties["flowing"] = "1" + await hass.services.async_call( "light", SERVICE_TURN_ON,