Clamp tplink color temp to valid range (#107695)
This commit is contained in:
parent
2a46f201cb
commit
765c520d7a
2 changed files with 44 additions and 8 deletions
|
@ -220,6 +220,26 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
|
||||||
hue, sat = tuple(int(val) for val in hs_color)
|
hue, sat = tuple(int(val) for val in hs_color)
|
||||||
await self.device.set_hsv(hue, sat, brightness, transition=transition)
|
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(
|
async def _async_turn_on_with_brightness(
|
||||||
self, brightness: int | None, transition: int | None
|
self, brightness: int | None, transition: int | None
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -234,10 +254,8 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
brightness, transition = self._async_extract_brightness_transition(**kwargs)
|
brightness, transition = self._async_extract_brightness_transition(**kwargs)
|
||||||
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
||||||
await self.device.set_color_temp(
|
await self._async_set_color_temp(
|
||||||
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
|
kwargs[ATTR_COLOR_TEMP_KELVIN], brightness, transition
|
||||||
brightness=brightness,
|
|
||||||
transition=transition,
|
|
||||||
)
|
)
|
||||||
if ATTR_HS_COLOR in kwargs:
|
if ATTR_HS_COLOR in kwargs:
|
||||||
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
|
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
|
# we have to set an HSV value to clear the effect
|
||||||
# before we can set a color temp
|
# before we can set a color temp
|
||||||
await self.device.set_hsv(0, 0, brightness)
|
await self.device.set_hsv(0, 0, brightness)
|
||||||
await self.device.set_color_temp(
|
await self._async_set_color_temp(
|
||||||
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
|
kwargs[ATTR_COLOR_TEMP_KELVIN], brightness, transition
|
||||||
brightness=brightness,
|
|
||||||
transition=transition,
|
|
||||||
)
|
)
|
||||||
elif ATTR_HS_COLOR in kwargs:
|
elif ATTR_HS_COLOR in kwargs:
|
||||||
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
|
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
|
||||||
|
|
|
@ -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.assert_called_with(6666, brightness=None, transition=None)
|
||||||
bulb.set_color_temp.reset_mock()
|
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:
|
async def test_brightness_only_light(hass: HomeAssistant) -> None:
|
||||||
"""Test a light."""
|
"""Test a light."""
|
||||||
|
|
Loading…
Add table
Reference in a new issue