From da7d2ef228863fb535d6b765ad957acd8f25c8f1 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 25 Jan 2024 09:46:22 +0100 Subject: [PATCH] Fix light color mode in zwave_js (#108783) --- homeassistant/components/zwave_js/light.py | 41 +++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/zwave_js/light.py b/homeassistant/components/zwave_js/light.py index 8ba50c15e02..2b286240aa3 100644 --- a/homeassistant/components/zwave_js/light.py +++ b/homeassistant/components/zwave_js/light.py @@ -151,7 +151,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): add_to_watched_value_ids=False, ) - self._calculate_color_values() + self._calculate_color_support() if self._supports_rgbw: self._supported_color_modes.add(ColorMode.RGBW) elif self._supports_color: @@ -160,6 +160,7 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): self._supported_color_modes.add(ColorMode.COLOR_TEMP) if not self._supported_color_modes: self._supported_color_modes.add(ColorMode.BRIGHTNESS) + self._calculate_color_values() # Entity class attributes self.supports_brightness_transition = bool( @@ -374,8 +375,8 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): self.async_write_ha_state() @callback - def _calculate_color_values(self) -> None: - """Calculate light colors.""" + def _get_color_values(self) -> tuple[Value | None, ...]: + """Get light colors.""" # NOTE: We lookup all values here (instead of relying on the multicolor one) # to find out what colors are supported # as this is a simple lookup by key, this not heavy @@ -404,6 +405,30 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): CommandClass.SWITCH_COLOR, 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 # https://github.com/zwave-js/node-zwave-js/pull/1782 combined_color_val = self.get_zwave_value( @@ -416,8 +441,11 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): else: multi_color = {} - # Default: Brightness (no color) - self._color_mode = ColorMode.BRIGHTNESS + # Default: Brightness (no color) or Unknown + if self.supported_color_modes == {ColorMode.BRIGHTNESS}: + self._color_mode = ColorMode.BRIGHTNESS + else: + self._color_mode = ColorMode.UNKNOWN # RGB support 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) green = multi_color.get(COLOR_SWITCH_COMBINED_GREEN, green_val.value) blue = multi_color.get(COLOR_SWITCH_COMBINED_BLUE, blue_val.value) - self._supports_color = True if None not in (red, green, blue): # convert to HS self._hs_color = color_util.color_RGB_to_hs(red, green, blue) @@ -434,7 +461,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): # color temperature support if ww_val and cw_val: - self._supports_color_temp = True 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) # Calculate color temps based on whites @@ -449,7 +475,6 @@ class ZwaveLight(ZWaveBaseEntity, LightEntity): self._color_temp = None # only one white channel (warm white) = rgbw support 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) self._rgbw_color = (red, green, blue, white) # Light supports rgbw, set color mode to rgbw