Migrate zwave_js lights to use Kelvin (#132818)

pull/132831/head
epenet 2024-12-10 11:01:22 +01:00 committed by GitHub
parent b7018deebc
commit 13a37da917
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 29 deletions

View File

@ -29,7 +29,7 @@ from zwave_js_server.model.value import Value
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_RGBW_COLOR, ATTR_RGBW_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
@ -60,6 +60,8 @@ MULTI_COLOR_MAP = {
ColorComponent.CYAN: COLOR_SWITCH_COMBINED_CYAN, ColorComponent.CYAN: COLOR_SWITCH_COMBINED_CYAN,
ColorComponent.PURPLE: COLOR_SWITCH_COMBINED_PURPLE, 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( async def async_setup_entry(
@ -103,6 +105,9 @@ def byte_to_zwave_brightness(value: int) -> int:
class ZwaveLight(ZWaveBaseEntity, LightEntity): class ZwaveLight(ZWaveBaseEntity, LightEntity):
"""Representation of a Z-Wave light.""" """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__( def __init__(
self, config_entry: ConfigEntry, driver: Driver, info: ZwaveDiscoveryInfo self, config_entry: ConfigEntry, driver: Driver, info: ZwaveDiscoveryInfo
) -> None: ) -> None:
@ -116,8 +121,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
self._hs_color: tuple[float, float] | None = None self._hs_color: tuple[float, float] | None = None
self._rgbw_color: tuple[int, int, int, int] | None = None self._rgbw_color: tuple[int, int, int, int] | None = None
self._color_temp: 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( self._warm_white = self.get_zwave_value(
TARGET_COLOR_PROPERTY, TARGET_COLOR_PROPERTY,
CommandClass.SWITCH_COLOR, CommandClass.SWITCH_COLOR,
@ -241,20 +244,10 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
return self._rgbw_color return self._rgbw_color
@property @property
def color_temp(self) -> int | None: def color_temp_kelvin(self) -> int | None:
"""Return the color temperature.""" """Return the color temperature value in Kelvin."""
return self._color_temp 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 @property
def supported_color_modes(self) -> set[ColorMode] | None: def supported_color_modes(self) -> set[ColorMode] | None:
"""Flag supported features.""" """Flag supported features."""
@ -267,10 +260,10 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
brightness = kwargs.get(ATTR_BRIGHTNESS) brightness = kwargs.get(ATTR_BRIGHTNESS)
hs_color = kwargs.get(ATTR_HS_COLOR) 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) 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: if new_colors is not None:
await self._async_set_colors(new_colors, transition) await self._async_set_colors(new_colors, transition)
@ -284,7 +277,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
def _get_new_colors( def _get_new_colors(
self, self,
hs_color: tuple[float, float] | None, hs_color: tuple[float, float] | None,
color_temp: int | None, color_temp_k: int | None,
rgbw: tuple[int, int, int, int] | None, rgbw: tuple[int, int, int, int] | None,
brightness_scale: float | None = None, brightness_scale: float | None = None,
) -> dict[ColorComponent, int] | None: ) -> dict[ColorComponent, int] | None:
@ -309,17 +302,14 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
return colors return colors
# Color temperature # 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 # Limit color temp to min/max values
color_temp = color_util.color_temperature_kelvin_to_mired(color_temp_k)
cold = max( cold = max(
0, 0,
min( min(
255, 255,
round( round((MAX_MIREDS - color_temp) / (MAX_MIREDS - MIN_MIREDS) * 255),
(self._max_mireds - color_temp)
/ (self._max_mireds - self._min_mireds)
* 255
),
), ),
) )
warm = 255 - cold warm = 255 - cold
@ -505,9 +495,8 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
cold_white = multi_color.get(COLOR_SWITCH_COMBINED_COLD_WHITE, cw_val.value) cold_white = multi_color.get(COLOR_SWITCH_COMBINED_COLD_WHITE, cw_val.value)
# Calculate color temps based on whites # Calculate color temps based on whites
if cold_white or warm_white: if cold_white or warm_white:
self._color_temp = round( self._color_temp = color_util.color_temperature_mired_to_kelvin(
self._max_mireds MAX_MIREDS - ((cold_white / 255) * (MAX_MIREDS - MIN_MIREDS))
- ((cold_white / 255) * (self._max_mireds - self._min_mireds))
) )
# White channels turned on, set color mode to color_temp # White channels turned on, set color mode to color_temp
self._color_mode = ColorMode.COLOR_TEMP self._color_mode = ColorMode.COLOR_TEMP
@ -568,7 +557,7 @@ class ZwaveColorOnOffLight(ZwaveLight):
if ( if (
kwargs.get(ATTR_RGBW_COLOR) is not None 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, # RGBW and color temp are not supported in this mode,
# delegate to the parent class # delegate to the parent class
@ -629,7 +618,7 @@ class ZwaveColorOnOffLight(ZwaveLight):
if new_colors is None: if new_colors is None:
new_colors = self._get_new_colors( 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: if new_colors is not None: