Migrate homematic light to color_mode (#69267)

This commit is contained in:
Erik Montnemery 2022-04-05 15:54:27 +02:00 committed by GitHub
parent 1a9420dda0
commit 7b18d511cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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})