mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Migrate tradfri lights to use Kelvin (#132800)
This commit is contained in:
parent
f0e7cb5794
commit
36ce90177f
@ -9,7 +9,7 @@ from pytradfri.command import Command
|
|||||||
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_COLOR_TEMP,
|
ATTR_COLOR_TEMP_KELVIN,
|
||||||
ATTR_HS_COLOR,
|
ATTR_HS_COLOR,
|
||||||
ATTR_TRANSITION,
|
ATTR_TRANSITION,
|
||||||
ColorMode,
|
ColorMode,
|
||||||
@ -87,8 +87,16 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
|
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
|
||||||
|
|
||||||
if self._device_control:
|
if self._device_control:
|
||||||
self._attr_min_mireds = self._device_control.min_mireds
|
self._attr_max_color_temp_kelvin = (
|
||||||
self._attr_max_mireds = self._device_control.max_mireds
|
color_util.color_temperature_mired_to_kelvin(
|
||||||
|
self._device_control.min_mireds
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._attr_min_color_temp_kelvin = (
|
||||||
|
color_util.color_temperature_mired_to_kelvin(
|
||||||
|
self._device_control.max_mireds
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def _refresh(self) -> None:
|
def _refresh(self) -> None:
|
||||||
"""Refresh the device."""
|
"""Refresh the device."""
|
||||||
@ -118,11 +126,11 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
return cast(int, self._device_data.dimmer)
|
return cast(int, self._device_data.dimmer)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp(self) -> int | None:
|
def color_temp_kelvin(self) -> int | None:
|
||||||
"""Return the color temp value in mireds."""
|
"""Return the color temperature value in Kelvin."""
|
||||||
if not self._device_data:
|
if not self._device_data or not (color_temp := self._device_data.color_temp):
|
||||||
return None
|
return None
|
||||||
return cast(int, self._device_data.color_temp)
|
return color_util.color_temperature_mired_to_kelvin(color_temp)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hs_color(self) -> tuple[float, float] | None:
|
def hs_color(self) -> tuple[float, float] | None:
|
||||||
@ -191,18 +199,19 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
transition_time = None
|
transition_time = None
|
||||||
|
|
||||||
temp_command = None
|
temp_command = None
|
||||||
if ATTR_COLOR_TEMP in kwargs and (
|
if ATTR_COLOR_TEMP_KELVIN in kwargs and (
|
||||||
self._device_control.can_set_temp or self._device_control.can_set_color
|
self._device_control.can_set_temp or self._device_control.can_set_color
|
||||||
):
|
):
|
||||||
temp = kwargs[ATTR_COLOR_TEMP]
|
temp_k = kwargs[ATTR_COLOR_TEMP_KELVIN]
|
||||||
# White Spectrum bulb
|
# White Spectrum bulb
|
||||||
if self._device_control.can_set_temp:
|
if self._device_control.can_set_temp:
|
||||||
if temp > self.max_mireds:
|
temp = color_util.color_temperature_kelvin_to_mired(temp_k)
|
||||||
temp = self.max_mireds
|
if temp < (min_mireds := self._device_control.min_mireds):
|
||||||
elif temp < self.min_mireds:
|
temp = min_mireds
|
||||||
temp = self.min_mireds
|
elif temp > (max_mireds := self._device_control.max_mireds):
|
||||||
|
temp = max_mireds
|
||||||
temp_data = {
|
temp_data = {
|
||||||
ATTR_COLOR_TEMP: temp,
|
"color_temp": temp,
|
||||||
"transition_time": transition_time,
|
"transition_time": transition_time,
|
||||||
}
|
}
|
||||||
temp_command = self._device_control.set_color_temp(**temp_data)
|
temp_command = self._device_control.set_color_temp(**temp_data)
|
||||||
@ -210,7 +219,6 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
# Color bulb (CWS)
|
# Color bulb (CWS)
|
||||||
# color_temp needs to be set with hue/saturation
|
# color_temp needs to be set with hue/saturation
|
||||||
elif self._device_control.can_set_color:
|
elif self._device_control.can_set_color:
|
||||||
temp_k = color_util.color_temperature_mired_to_kelvin(temp)
|
|
||||||
hs_color = color_util.color_temperature_to_hs(temp_k)
|
hs_color = color_util.color_temperature_to_hs(temp_k)
|
||||||
hue = int(hs_color[0] * (self._device_control.max_hue / 360))
|
hue = int(hs_color[0] * (self._device_control.max_hue / 360))
|
||||||
sat = int(hs_color[1] * (self._device_control.max_saturation / 100))
|
sat = int(hs_color[1] * (self._device_control.max_saturation / 100))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user