Fix light color mode in zwave_js (#108783)

This commit is contained in:
Erik Montnemery 2024-01-25 09:46:22 +01:00 committed by GitHub
parent fabf8802f5
commit da7d2ef228
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -151,7 +151,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
add_to_watched_value_ids=False, add_to_watched_value_ids=False,
) )
self._calculate_color_values() self._calculate_color_support()
if self._supports_rgbw: if self._supports_rgbw:
self._supported_color_modes.add(ColorMode.RGBW) self._supported_color_modes.add(ColorMode.RGBW)
elif self._supports_color: elif self._supports_color:
@ -160,6 +160,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
self._supported_color_modes.add(ColorMode.COLOR_TEMP) self._supported_color_modes.add(ColorMode.COLOR_TEMP)
if not self._supported_color_modes: if not self._supported_color_modes:
self._supported_color_modes.add(ColorMode.BRIGHTNESS) self._supported_color_modes.add(ColorMode.BRIGHTNESS)
self._calculate_color_values()
# Entity class attributes # Entity class attributes
self.supports_brightness_transition = bool( self.supports_brightness_transition = bool(
@ -374,8 +375,8 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
self.async_write_ha_state() self.async_write_ha_state()
@callback @callback
def _calculate_color_values(self) -> None: def _get_color_values(self) -> tuple[Value | None, ...]:
"""Calculate light colors.""" """Get light colors."""
# NOTE: We lookup all values here (instead of relying on the multicolor one) # NOTE: We lookup all values here (instead of relying on the multicolor one)
# to find out what colors are supported # to find out what colors are supported
# as this is a simple lookup by key, this not heavy # as this is a simple lookup by key, this not heavy
@ -404,6 +405,30 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
CommandClass.SWITCH_COLOR, CommandClass.SWITCH_COLOR,
value_property_key=ColorComponent.COLD_WHITE.value, value_property_key=ColorComponent.COLD_WHITE.value,
) )
return (red_val, green_val, blue_val, ww_val, cw_val)
@callback
def _calculate_color_support(self) -> None:
"""Calculate light colors."""
(red_val, green_val, blue_val, ww_val, cw_val) = self._get_color_values()
# RGB support
if red_val and green_val and blue_val:
self._supports_color = True
# color temperature support
if ww_val and cw_val:
self._supports_color_temp = True
# only one white channel (warm white) = rgbw support
elif red_val and green_val and blue_val and ww_val:
self._supports_rgbw = True
# only one white channel (cool white) = rgbw support
elif cw_val:
self._supports_rgbw = True
@callback
def _calculate_color_values(self) -> None:
"""Calculate light colors."""
(red_val, green_val, blue_val, ww_val, cw_val) = self._get_color_values()
# prefer the (new) combined color property # prefer the (new) combined color property
# https://github.com/zwave-js/node-zwave-js/pull/1782 # https://github.com/zwave-js/node-zwave-js/pull/1782
combined_color_val = self.get_zwave_value( combined_color_val = self.get_zwave_value(
@ -416,8 +441,11 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
else: else:
multi_color = {} multi_color = {}
# Default: Brightness (no color) # Default: Brightness (no color) or Unknown
self._color_mode = ColorMode.BRIGHTNESS if self.supported_color_modes == {ColorMode.BRIGHTNESS}:
self._color_mode = ColorMode.BRIGHTNESS
else:
self._color_mode = ColorMode.UNKNOWN
# RGB support # RGB support
if red_val and green_val and blue_val: if red_val and green_val and blue_val:
@ -425,7 +453,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
red = multi_color.get(COLOR_SWITCH_COMBINED_RED, red_val.value) red = multi_color.get(COLOR_SWITCH_COMBINED_RED, red_val.value)
green = multi_color.get(COLOR_SWITCH_COMBINED_GREEN, green_val.value) green = multi_color.get(COLOR_SWITCH_COMBINED_GREEN, green_val.value)
blue = multi_color.get(COLOR_SWITCH_COMBINED_BLUE, blue_val.value) blue = multi_color.get(COLOR_SWITCH_COMBINED_BLUE, blue_val.value)
self._supports_color = True
if None not in (red, green, blue): if None not in (red, green, blue):
# convert to HS # convert to HS
self._hs_color = color_util.color_RGB_to_hs(red, green, blue) self._hs_color = color_util.color_RGB_to_hs(red, green, blue)
@ -434,7 +461,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
# color temperature support # color temperature support
if ww_val and cw_val: if ww_val and cw_val:
self._supports_color_temp = True
warm_white = multi_color.get(COLOR_SWITCH_COMBINED_WARM_WHITE, ww_val.value) warm_white = multi_color.get(COLOR_SWITCH_COMBINED_WARM_WHITE, ww_val.value)
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
@ -449,7 +475,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity):
self._color_temp = None self._color_temp = None
# only one white channel (warm white) = rgbw support # only one white channel (warm white) = rgbw support
elif red_val and green_val and blue_val and ww_val: elif red_val and green_val and blue_val and ww_val:
self._supports_rgbw = True
white = multi_color.get(COLOR_SWITCH_COMBINED_WARM_WHITE, ww_val.value) white = multi_color.get(COLOR_SWITCH_COMBINED_WARM_WHITE, ww_val.value)
self._rgbw_color = (red, green, blue, white) self._rgbw_color = (red, green, blue, white)
# Light supports rgbw, set color mode to rgbw # Light supports rgbw, set color mode to rgbw