Migrate osramlightify light to color_mode (#70915)
This commit is contained in:
parent
b366354d55
commit
c8c9a4b09f
1 changed files with 37 additions and 13 deletions
|
@ -16,11 +16,10 @@ from homeassistant.components.light import (
|
|||
ATTR_TRANSITION,
|
||||
EFFECT_RANDOM,
|
||||
PLATFORM_SCHEMA,
|
||||
SUPPORT_BRIGHTNESS,
|
||||
SUPPORT_COLOR,
|
||||
SUPPORT_COLOR_TEMP,
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
LightEntityFeature,
|
||||
brightness_supported,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
@ -206,21 +205,35 @@ class Luminary(LightEntity):
|
|||
"""Get a unique ID (not implemented)."""
|
||||
raise NotImplementedError
|
||||
|
||||
def _get_supported_color_modes(self):
|
||||
"""Get supported color modes."""
|
||||
color_modes = set()
|
||||
if "temp" in self._luminary.supported_features():
|
||||
color_modes.add(ColorMode.COLOR_TEMP)
|
||||
|
||||
if "rgb" in self._luminary.supported_features():
|
||||
color_modes.add(ColorMode.HS)
|
||||
|
||||
if not color_modes and "lum" in self._luminary.supported_features():
|
||||
color_modes.add(ColorMode.BRIGHTNESS)
|
||||
|
||||
if not color_modes:
|
||||
color_modes.add(ColorMode.ONOFF)
|
||||
|
||||
return color_modes
|
||||
|
||||
def _get_supported_features(self):
|
||||
"""Get list of supported features."""
|
||||
features = 0
|
||||
if "lum" in self._luminary.supported_features():
|
||||
features = features | SUPPORT_BRIGHTNESS | LightEntityFeature.TRANSITION
|
||||
features = features | LightEntityFeature.TRANSITION
|
||||
|
||||
if "temp" in self._luminary.supported_features():
|
||||
features = features | SUPPORT_COLOR_TEMP | LightEntityFeature.TRANSITION
|
||||
features = features | LightEntityFeature.TRANSITION
|
||||
|
||||
if "rgb" in self._luminary.supported_features():
|
||||
features = (
|
||||
features
|
||||
| SUPPORT_COLOR
|
||||
| LightEntityFeature.TRANSITION
|
||||
| LightEntityFeature.EFFECT
|
||||
features | LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
|
||||
)
|
||||
|
||||
return features
|
||||
|
@ -350,31 +363,42 @@ class Luminary(LightEntity):
|
|||
def update_static_attributes(self):
|
||||
"""Update static attributes of the luminary."""
|
||||
self._unique_id = self._get_unique_id()
|
||||
self._attr_supported_color_modes = self._get_supported_color_modes()
|
||||
self._supported_features = self._get_supported_features()
|
||||
self._effect_list = self._get_effect_list()
|
||||
if self._supported_features & SUPPORT_COLOR_TEMP:
|
||||
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
|
||||
self._min_mireds = color_util.color_temperature_kelvin_to_mired(
|
||||
self._luminary.max_temp() or DEFAULT_KELVIN
|
||||
)
|
||||
self._max_mireds = color_util.color_temperature_kelvin_to_mired(
|
||||
self._luminary.min_temp() or DEFAULT_KELVIN
|
||||
)
|
||||
if len(self._attr_supported_color_modes == 1):
|
||||
# The light supports only a single color mode
|
||||
self._attr_color_mode = list(self._attr_supported_color_modes)[0]
|
||||
|
||||
def update_dynamic_attributes(self):
|
||||
"""Update dynamic attributes of the luminary."""
|
||||
self._is_on = self._luminary.on()
|
||||
self._available = self._luminary.reachable() and not self._luminary.deleted()
|
||||
if self._supported_features & SUPPORT_BRIGHTNESS:
|
||||
if brightness_supported(self._attr_supported_color_modes):
|
||||
self._brightness = int(self._luminary.lum() * 2.55)
|
||||
|
||||
if self._supported_features & SUPPORT_COLOR_TEMP:
|
||||
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
|
||||
self._color_temp = color_util.color_temperature_kelvin_to_mired(
|
||||
self._luminary.temp() or DEFAULT_KELVIN
|
||||
)
|
||||
|
||||
if self._supported_features & SUPPORT_COLOR:
|
||||
if ColorMode.HS in self._attr_supported_color_modes:
|
||||
self._rgb_color = self._luminary.rgb()
|
||||
|
||||
if len(self._attr_supported_color_modes > 1):
|
||||
# The light supports hs + color temp, determine which one it is
|
||||
if self._rgb_color == (0, 0, 0):
|
||||
self._attr_color_mode = ColorMode.COLOR_TEMP
|
||||
else:
|
||||
self._attr_color_mode = ColorMode.HS
|
||||
|
||||
def update(self) -> None:
|
||||
"""Synchronize state with bridge."""
|
||||
changed = self.update_func()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue