Clamp tplink color temp to valid range (#107695)

This commit is contained in:
J. Nick Koston 2024-01-09 17:08:49 -10:00 committed by Franck Nijhof
parent 2a46f201cb
commit 765c520d7a
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 44 additions and 8 deletions

View file

@ -220,6 +220,26 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
hue, sat = tuple(int(val) for val in hs_color)
await self.device.set_hsv(hue, sat, brightness, transition=transition)
async def _async_set_color_temp(
self, color_temp: float | int, brightness: int | None, transition: int | None
) -> None:
device = self.device
valid_temperature_range = device.valid_temperature_range
requested_color_temp = round(color_temp)
# Clamp color temp to valid range
# since if the light in a group we will
# get requests for color temps for the range
# of the group and not the light
clamped_color_temp = min(
valid_temperature_range.max,
max(valid_temperature_range.min, requested_color_temp),
)
await device.set_color_temp(
clamped_color_temp,
brightness=brightness,
transition=transition,
)
async def _async_turn_on_with_brightness(
self, brightness: int | None, transition: int | None
) -> None:
@ -234,10 +254,8 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
"""Turn the light on."""
brightness, transition = self._async_extract_brightness_transition(**kwargs)
if ATTR_COLOR_TEMP_KELVIN in kwargs:
await self.device.set_color_temp(
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
brightness=brightness,
transition=transition,
await self._async_set_color_temp(
kwargs[ATTR_COLOR_TEMP_KELVIN], brightness, transition
)
if ATTR_HS_COLOR in kwargs:
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
@ -324,10 +342,8 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
# 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.device.set_color_temp(
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
brightness=brightness,
transition=transition,
await self._async_set_color_temp(
kwargs[ATTR_COLOR_TEMP_KELVIN], brightness, transition
)
elif ATTR_HS_COLOR in kwargs:
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)

View file

@ -264,6 +264,26 @@ async def test_color_temp_light(
bulb.set_color_temp.assert_called_with(6666, brightness=None, transition=None)
bulb.set_color_temp.reset_mock()
# Verify color temp is clamped to the valid range
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP_KELVIN: 20000},
blocking=True,
)
bulb.set_color_temp.assert_called_with(9000, brightness=None, transition=None)
bulb.set_color_temp.reset_mock()
# Verify color temp is clamped to the valid range
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP_KELVIN: 1},
blocking=True,
)
bulb.set_color_temp.assert_called_with(4000, brightness=None, transition=None)
bulb.set_color_temp.reset_mock()
async def test_brightness_only_light(hass: HomeAssistant) -> None:
"""Test a light."""