Use device-native kelvins for tplink color temperature (#85646)

This commit is contained in:
Teemu R 2023-02-21 19:52:50 +01:00 committed by GitHub
parent c06420689e
commit a93b4e7197
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 36 deletions

View file

@ -10,7 +10,7 @@ import voluptuous as vol
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_TRANSITION,
@ -23,10 +23,6 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import (
color_temperature_kelvin_to_mired as kelvin_to_mired,
color_temperature_mired_to_kelvin as mired_to_kelvin,
)
from . import legacy_device_id
from .const import DOMAIN
@ -206,18 +202,6 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
return brightness, transition
async def _async_set_color_temp(
self, color_temp_mireds: int, brightness: int | None, transition: int | None
) -> None:
# Handle temp conversion mireds -> kelvin being slightly outside of valid range
kelvin = mired_to_kelvin(color_temp_mireds)
kelvin_range = self.device.valid_temperature_range
color_tmp = max(kelvin_range.min, min(kelvin_range.max, kelvin))
_LOGGER.debug("Changing color temp to %s", color_tmp)
await self.device.set_color_temp(
color_tmp, brightness=brightness, transition=transition
)
async def _async_set_hsv(
self, hs_color: tuple[int, int], brightness: int | None, transition: int | None
) -> None:
@ -238,9 +222,11 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
brightness, transition = self._async_extract_brightness_transition(**kwargs)
if ATTR_COLOR_TEMP in kwargs:
await self._async_set_color_temp(
int(kwargs[ATTR_COLOR_TEMP]), brightness, transition
if ATTR_COLOR_TEMP_KELVIN in kwargs:
await self.device.set_color_temp(
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
brightness=brightness,
transition=transition,
)
if ATTR_HS_COLOR in kwargs:
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
@ -255,19 +241,19 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
await self.device.turn_off(transition=transition)
@property
def min_mireds(self) -> int:
def min_color_temp_kelvin(self) -> int:
"""Return minimum supported color temperature."""
return kelvin_to_mired(self.device.valid_temperature_range.max)
return cast(int, self.device.valid_temperature_range.min)
@property
def max_mireds(self) -> int:
def max_color_temp_kelvin(self) -> int:
"""Return maximum supported color temperature."""
return kelvin_to_mired(self.device.valid_temperature_range.min)
return cast(int, self.device.valid_temperature_range.max)
@property
def color_temp(self) -> int | None:
"""Return the color temperature of this light in mireds for HA."""
return kelvin_to_mired(self.device.color_temp)
def color_temp_kelvin(self) -> int:
"""Return the color temperature of this light."""
return cast(int, self.device.color_temp)
@property
def brightness(self) -> int | None:
@ -341,14 +327,16 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
await self.device.set_effect(
kwargs[ATTR_EFFECT], brightness=brightness, transition=transition
)
elif ATTR_COLOR_TEMP in kwargs:
elif ATTR_COLOR_TEMP_KELVIN in kwargs:
if self.effect:
# If there is an effect in progress
# 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._async_set_color_temp(
int(kwargs[ATTR_COLOR_TEMP]), brightness, transition
await self.device.set_color_temp(
int(kwargs[ATTR_COLOR_TEMP_KELVIN]),
brightness=brightness,
transition=transition,
)
elif ATTR_HS_COLOR in kwargs:
await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)

View file

@ -10,7 +10,7 @@ from homeassistant.components import tplink
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_EFFECT_LIST,
ATTR_HS_COLOR,
@ -113,7 +113,7 @@ async def test_color_light(
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{**BASE_PAYLOAD, ATTR_COLOR_TEMP: 150},
{**BASE_PAYLOAD, ATTR_COLOR_TEMP_KELVIN: 6666},
blocking=True,
)
bulb.set_color_temp.assert_called_with(
@ -124,7 +124,7 @@ async def test_color_light(
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{**BASE_PAYLOAD, ATTR_COLOR_TEMP: 150},
{**BASE_PAYLOAD, ATTR_COLOR_TEMP_KELVIN: 6666},
blocking=True,
)
bulb.set_color_temp.assert_called_with(
@ -233,7 +233,7 @@ async def test_color_temp_light(
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["brightness", "color_temp"]
assert attributes[ATTR_MIN_MIREDS] == 111
assert attributes[ATTR_MAX_MIREDS] == 250
assert attributes[ATTR_COLOR_TEMP] == 250
assert attributes[ATTR_COLOR_TEMP_KELVIN] == 4000
await hass.services.async_call(
LIGHT_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
@ -258,7 +258,7 @@ async def test_color_temp_light(
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP: 150},
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP_KELVIN: 6666},
blocking=True,
)
bulb.set_color_temp.assert_called_with(6666, brightness=None, transition=None)
@ -417,7 +417,7 @@ async def test_smart_strip_effects(hass: HomeAssistant) -> None:
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP: 250},
{ATTR_ENTITY_ID: entity_id, ATTR_COLOR_TEMP_KELVIN: 4000},
blocking=True,
)
strip.set_hsv.assert_called_once_with(0, 0, None)