Migrate osramlightify light to color_mode (#70915)

This commit is contained in:
Erik Montnemery 2022-08-30 20:58:38 +02:00 committed by GitHub
parent b366354d55
commit c8c9a4b09f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,11 +16,10 @@ from homeassistant.components.light import (
ATTR_TRANSITION, ATTR_TRANSITION,
EFFECT_RANDOM, EFFECT_RANDOM,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SUPPORT_BRIGHTNESS, ColorMode,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
LightEntity, LightEntity,
LightEntityFeature, LightEntityFeature,
brightness_supported,
) )
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -206,21 +205,35 @@ class Luminary(LightEntity):
"""Get a unique ID (not implemented).""" """Get a unique ID (not implemented)."""
raise NotImplementedError 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): def _get_supported_features(self):
"""Get list of supported features.""" """Get list of supported features."""
features = 0 features = 0
if "lum" in self._luminary.supported_features(): if "lum" in self._luminary.supported_features():
features = features | SUPPORT_BRIGHTNESS | LightEntityFeature.TRANSITION features = features | LightEntityFeature.TRANSITION
if "temp" in self._luminary.supported_features(): 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(): if "rgb" in self._luminary.supported_features():
features = ( features = (
features features | LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
| SUPPORT_COLOR
| LightEntityFeature.TRANSITION
| LightEntityFeature.EFFECT
) )
return features return features
@ -350,31 +363,42 @@ class Luminary(LightEntity):
def update_static_attributes(self): def update_static_attributes(self):
"""Update static attributes of the luminary.""" """Update static attributes of the luminary."""
self._unique_id = self._get_unique_id() 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._supported_features = self._get_supported_features()
self._effect_list = self._get_effect_list() 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._min_mireds = color_util.color_temperature_kelvin_to_mired(
self._luminary.max_temp() or DEFAULT_KELVIN self._luminary.max_temp() or DEFAULT_KELVIN
) )
self._max_mireds = color_util.color_temperature_kelvin_to_mired( self._max_mireds = color_util.color_temperature_kelvin_to_mired(
self._luminary.min_temp() or DEFAULT_KELVIN 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): def update_dynamic_attributes(self):
"""Update dynamic attributes of the luminary.""" """Update dynamic attributes of the luminary."""
self._is_on = self._luminary.on() self._is_on = self._luminary.on()
self._available = self._luminary.reachable() and not self._luminary.deleted() 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) 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._color_temp = color_util.color_temperature_kelvin_to_mired(
self._luminary.temp() or DEFAULT_KELVIN 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() 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: def update(self) -> None:
"""Synchronize state with bridge.""" """Synchronize state with bridge."""
changed = self.update_func() changed = self.update_func()