Workaround brightness transition delay from off in older yeelight models (#58774)

This commit is contained in:
J. Nick Koston 2021-10-31 10:11:07 -05:00 committed by GitHub
parent f94bbf351d
commit faecc90b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View File

@ -35,6 +35,20 @@ _LOGGER = logging.getLogger(__name__)
STATE_CHANGE_TIME = 0.40 # seconds STATE_CHANGE_TIME = 0.40 # seconds
POWER_STATE_CHANGE_TIME = 1 # 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" DOMAIN = "yeelight"
DATA_YEELIGHT = DOMAIN DATA_YEELIGHT = DOMAIN
DATA_UPDATED = "yeelight_{}_data_updated" DATA_UPDATED = "yeelight_{}_data_updated"

View File

@ -63,6 +63,7 @@ from . import (
DATA_DEVICE, DATA_DEVICE,
DATA_UPDATED, DATA_UPDATED,
DOMAIN, DOMAIN,
MODELS_WITH_DELAYED_ON_TRANSITION,
POWER_STATE_CHANGE_TIME, POWER_STATE_CHANGE_TIME,
YEELIGHT_FLOW_TRANSITION_SCHEMA, YEELIGHT_FLOW_TRANSITION_SCHEMA,
YeelightEntity, YeelightEntity,
@ -614,7 +615,10 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
"""Set bulb brightness.""" """Set bulb brightness."""
if not brightness: if not brightness:
return 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) _LOGGER.debug("brightness already set to: %s", brightness)
# Already set, and since we get pushed updates # Already set, and since we get pushed updates
# we avoid setting it again to ensure we do not # we avoid setting it again to ensure we do not

View File

@ -641,6 +641,25 @@ async def test_state_already_set_avoid_ratelimit(hass: HomeAssistant):
mocked_bulb.async_set_rgb.reset_mock() mocked_bulb.async_set_rgb.reset_mock()
mocked_bulb.last_properties["flowing"] = "0" 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( await hass.services.async_call(
"light", "light",
SERVICE_TURN_ON, 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 == [] assert mocked_bulb.async_set_brightness.mock_calls == []
mocked_bulb.last_properties["flowing"] = "1" mocked_bulb.last_properties["flowing"] = "1"
await hass.services.async_call( await hass.services.async_call(
"light", "light",
SERVICE_TURN_ON, SERVICE_TURN_ON,