From 7b18d511cc26a22c856f75a553dd9d8b92d9681d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 5 Apr 2022 15:54:27 +0200 Subject: [PATCH] Migrate homematic light to color_mode (#69267) --- homeassistant/components/homematic/light.py | 46 ++++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/homematic/light.py b/homeassistant/components/homematic/light.py index 711d939900c..c1ac048ae98 100644 --- a/homeassistant/components/homematic/light.py +++ b/homeassistant/components/homematic/light.py @@ -7,9 +7,9 @@ from homeassistant.components.light import ( ATTR_EFFECT, ATTR_HS_COLOR, ATTR_TRANSITION, - SUPPORT_BRIGHTNESS, - SUPPORT_COLOR, - SUPPORT_COLOR_TEMP, + COLOR_MODE_BRIGHTNESS, + COLOR_MODE_COLOR_TEMP, + COLOR_MODE_HS, SUPPORT_EFFECT, SUPPORT_TRANSITION, LightEntity, @@ -21,8 +21,6 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .const import ATTR_DISCOVER_DEVICES from .entity import HMDevice -SUPPORT_HOMEMATIC = SUPPORT_BRIGHTNESS - def setup_platform( hass: HomeAssistant, @@ -62,22 +60,40 @@ class HMLight(HMDevice, LightEntity): return False @property - def supported_features(self): - """Flag supported features.""" - features = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION + def color_mode(self) -> str: + """Return the color mode of the light.""" + if "COLOR" in self._hmdevice.WRITENODE: + return COLOR_MODE_HS + if hasattr(self._hmdevice, "get_color_temp"): + return COLOR_MODE_COLOR_TEMP + return COLOR_MODE_BRIGHTNESS + + @property + def supported_color_modes(self) -> set[str] | None: + """Flag supported color modes.""" + color_modes = set() if "COLOR" in self._hmdevice.WRITENODE: - features |= SUPPORT_COLOR + color_modes.add(COLOR_MODE_HS) + if hasattr(self._hmdevice, "get_color_temp"): + color_modes.add(COLOR_MODE_COLOR_TEMP) + if not color_modes: + color_modes.add(COLOR_MODE_BRIGHTNESS) + + return color_modes + + @property + def supported_features(self): + """Flag supported features.""" + features = SUPPORT_TRANSITION if "PROGRAM" in self._hmdevice.WRITENODE: features |= SUPPORT_EFFECT - if hasattr(self._hmdevice, "get_color_temp"): - features |= SUPPORT_COLOR_TEMP return features @property def hs_color(self): """Return the hue and saturation color value [float, float].""" - if not self.supported_features & SUPPORT_COLOR: + if COLOR_MODE_HS not in self.supported_color_modes: return None hue, sat = self._hmdevice.get_hs_color(self._channel) return hue * 360.0, sat * 100.0 @@ -85,7 +101,7 @@ class HMLight(HMDevice, LightEntity): @property def color_temp(self): """Return the color temp in mireds [int].""" - if not self.supported_features & SUPPORT_COLOR_TEMP: + if COLOR_MODE_COLOR_TEMP not in self.supported_color_modes: return None hm_color_temp = self._hmdevice.get_color_temp(self._channel) return self.max_mireds - (self.max_mireds - self.min_mireds) * hm_color_temp @@ -119,7 +135,7 @@ class HMLight(HMDevice, LightEntity): ): self._hmdevice.on(self._channel) - if ATTR_HS_COLOR in kwargs and self.supported_features & SUPPORT_COLOR: + if ATTR_HS_COLOR in kwargs: self._hmdevice.set_hs_color( hue=kwargs[ATTR_HS_COLOR][0] / 360.0, saturation=kwargs[ATTR_HS_COLOR][1] / 100.0, @@ -146,7 +162,7 @@ class HMLight(HMDevice, LightEntity): self._state = "LEVEL" self._data[self._state] = None - if self.supported_features & SUPPORT_COLOR: + if COLOR_MODE_HS in self.supported_color_modes: self._data.update({"COLOR": None}) if self.supported_features & SUPPORT_EFFECT: self._data.update({"PROGRAM": None})