Migrate zwave_js lights to use Kelvin (#132818)
parent
b7018deebc
commit
13a37da917
|
@ -29,7 +29,7 @@ from zwave_js_server.model.value import Value
|
|||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_TEMP,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_HS_COLOR,
|
||||
ATTR_RGBW_COLOR,
|
||||
ATTR_TRANSITION,
|
||||
|
@ -60,6 +60,8 @@ MULTI_COLOR_MAP = {
|
|||
ColorComponent.CYAN: COLOR_SWITCH_COMBINED_CYAN,
|
||||
ColorComponent.PURPLE: COLOR_SWITCH_COMBINED_PURPLE,
|
||||
}
|
||||
MIN_MIREDS = 153 # 6500K as a safe default
|
||||
MAX_MIREDS = 370 # 2700K as a safe default
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
@ -103,6 +105,9 @@ def byte_to_zwave_brightness(value: int) -> int:
|
|||
class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
||||
"""Representation of a Z-Wave light."""
|
||||
|
||||
_attr_min_color_temp_kelvin = 2700 # 370 mireds as a safe default
|
||||
_attr_max_color_temp_kelvin = 6500 # 153 mireds as a safe default
|
||||
|
||||
def __init__(
|
||||
self, config_entry: ConfigEntry, driver: Driver, info: ZwaveDiscoveryInfo
|
||||
) -> None:
|
||||
|
@ -116,8 +121,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
self._hs_color: tuple[float, float] | None = None
|
||||
self._rgbw_color: tuple[int, int, int, int] | None = None
|
||||
self._color_temp: int | None = None
|
||||
self._min_mireds = 153 # 6500K as a safe default
|
||||
self._max_mireds = 370 # 2700K as a safe default
|
||||
self._warm_white = self.get_zwave_value(
|
||||
TARGET_COLOR_PROPERTY,
|
||||
CommandClass.SWITCH_COLOR,
|
||||
|
@ -241,20 +244,10 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
return self._rgbw_color
|
||||
|
||||
@property
|
||||
def color_temp(self) -> int | None:
|
||||
"""Return the color temperature."""
|
||||
def color_temp_kelvin(self) -> int | None:
|
||||
"""Return the color temperature value in Kelvin."""
|
||||
return self._color_temp
|
||||
|
||||
@property
|
||||
def min_mireds(self) -> int:
|
||||
"""Return the coldest color_temp that this light supports."""
|
||||
return self._min_mireds
|
||||
|
||||
@property
|
||||
def max_mireds(self) -> int:
|
||||
"""Return the warmest color_temp that this light supports."""
|
||||
return self._max_mireds
|
||||
|
||||
@property
|
||||
def supported_color_modes(self) -> set[ColorMode] | None:
|
||||
"""Flag supported features."""
|
||||
|
@ -267,10 +260,10 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
||||
|
||||
hs_color = kwargs.get(ATTR_HS_COLOR)
|
||||
color_temp = kwargs.get(ATTR_COLOR_TEMP)
|
||||
color_temp_k = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
|
||||
rgbw = kwargs.get(ATTR_RGBW_COLOR)
|
||||
|
||||
new_colors = self._get_new_colors(hs_color, color_temp, rgbw)
|
||||
new_colors = self._get_new_colors(hs_color, color_temp_k, rgbw)
|
||||
if new_colors is not None:
|
||||
await self._async_set_colors(new_colors, transition)
|
||||
|
||||
|
@ -284,7 +277,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
def _get_new_colors(
|
||||
self,
|
||||
hs_color: tuple[float, float] | None,
|
||||
color_temp: int | None,
|
||||
color_temp_k: int | None,
|
||||
rgbw: tuple[int, int, int, int] | None,
|
||||
brightness_scale: float | None = None,
|
||||
) -> dict[ColorComponent, int] | None:
|
||||
|
@ -309,17 +302,14 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
return colors
|
||||
|
||||
# Color temperature
|
||||
if color_temp is not None and self._supports_color_temp:
|
||||
if color_temp_k is not None and self._supports_color_temp:
|
||||
# Limit color temp to min/max values
|
||||
color_temp = color_util.color_temperature_kelvin_to_mired(color_temp_k)
|
||||
cold = max(
|
||||
0,
|
||||
min(
|
||||
255,
|
||||
round(
|
||||
(self._max_mireds - color_temp)
|
||||
/ (self._max_mireds - self._min_mireds)
|
||||
* 255
|
||||
),
|
||||
round((MAX_MIREDS - color_temp) / (MAX_MIREDS - MIN_MIREDS) * 255),
|
||||
),
|
||||
)
|
||||
warm = 255 - cold
|
||||
|
@ -505,9 +495,8 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
|
|||
cold_white = multi_color.get(COLOR_SWITCH_COMBINED_COLD_WHITE, cw_val.value)
|
||||
# Calculate color temps based on whites
|
||||
if cold_white or warm_white:
|
||||
self._color_temp = round(
|
||||
self._max_mireds
|
||||
- ((cold_white / 255) * (self._max_mireds - self._min_mireds))
|
||||
self._color_temp = color_util.color_temperature_mired_to_kelvin(
|
||||
MAX_MIREDS - ((cold_white / 255) * (MAX_MIREDS - MIN_MIREDS))
|
||||
)
|
||||
# White channels turned on, set color mode to color_temp
|
||||
self._color_mode = ColorMode.COLOR_TEMP
|
||||
|
@ -568,7 +557,7 @@ class ZwaveColorOnOffLight(ZwaveLight):
|
|||
|
||||
if (
|
||||
kwargs.get(ATTR_RGBW_COLOR) is not None
|
||||
or kwargs.get(ATTR_COLOR_TEMP) is not None
|
||||
or kwargs.get(ATTR_COLOR_TEMP_KELVIN) is not None
|
||||
):
|
||||
# RGBW and color temp are not supported in this mode,
|
||||
# delegate to the parent class
|
||||
|
@ -629,7 +618,7 @@ class ZwaveColorOnOffLight(ZwaveLight):
|
|||
|
||||
if new_colors is None:
|
||||
new_colors = self._get_new_colors(
|
||||
hs_color=hs_color, color_temp=None, rgbw=None, brightness_scale=scale
|
||||
hs_color=hs_color, color_temp_k=None, rgbw=None, brightness_scale=scale
|
||||
)
|
||||
|
||||
if new_colors is not None:
|
||||
|
|
Loading…
Reference in New Issue