Adjust tradfri light supported features

This commit is contained in:
Martin Hjelmare 2022-06-23 21:21:36 +02:00
parent 5474fd77b2
commit 136ffc898b

View File

@ -71,33 +71,30 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
device_control = self._device.light_control device_control = self._device.light_control
assert device_control # light_control is ensured when creating the entity assert device_control # light_control is ensured when creating the entity
self._device_control = device_control self._device_control = device_control
self._device_data = device_control.lights[0] device_data = device_control.lights[0]
self._device_data = device_data
self._attr_unique_id = f"light-{gateway_id}-{self._device_id}" self._attr_unique_id = f"light-{gateway_id}-{self._device_id}"
self._hs_color = None self._hs_color = None
# Calculate supported color modes # Calculate supported color modes
modes: set[ColorMode] = {ColorMode.ONOFF} modes: set[ColorMode] = {ColorMode.ONOFF}
if device_control.can_set_color: if device_control.supports_hsb_xy_color:
modes.add(ColorMode.HS) modes.add(ColorMode.HS)
if device_control.can_set_temp: if device_control.supports_color_temp:
modes.add(ColorMode.COLOR_TEMP) modes.add(ColorMode.COLOR_TEMP)
if device_control.can_set_dimmer: if device_control.supports_dimmer:
modes.add(ColorMode.BRIGHTNESS) modes.add(ColorMode.BRIGHTNESS)
self._attr_supported_color_modes = filter_supported_color_modes(modes) self._attr_supported_color_modes = filter_supported_color_modes(modes)
if len(self._attr_supported_color_modes) == 1: if len(self._attr_supported_color_modes) == 1:
self._fixed_color_mode = next(iter(self._attr_supported_color_modes)) self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
self._attr_max_color_temp_kelvin = ( self._attr_max_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(
color_util.color_temperature_mired_to_kelvin(
device_control.min_mireds device_control.min_mireds
) )
) self._attr_min_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(
self._attr_min_color_temp_kelvin = (
color_util.color_temperature_mired_to_kelvin(
device_control.max_mireds device_control.max_mireds
) )
)
def _refresh(self) -> None: def _refresh(self) -> None:
"""Refresh the device.""" """Refresh the device."""
@ -132,14 +129,12 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
@property @property
def hs_color(self) -> tuple[float, float] | None: def hs_color(self) -> tuple[float, float] | None:
"""HS color of the light.""" """HS color of the light."""
if self._device_control.can_set_color:
hsbxy = self._device_data.hsb_xy_color hsbxy = self._device_data.hsb_xy_color
if hsbxy is None: if hsbxy is None:
return None return None
hue = hsbxy[0] / (self._device_control.max_hue / 360) hue = hsbxy[0] / (self._device_control.max_hue / 360)
sat = hsbxy[1] / (self._device_control.max_saturation / 100) sat = hsbxy[1] / (self._device_control.max_saturation / 100)
return hue, sat return hue, sat
return None
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off.""" """Instruct the light to turn off."""
@ -175,7 +170,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
dimmer_command = self._device_control.set_state(True) dimmer_command = self._device_control.set_state(True)
color_command = None color_command = None
if ATTR_HS_COLOR in kwargs and self._device_control.can_set_color: if ATTR_HS_COLOR in kwargs and self._device_data.supports_hsb_xy_color:
hue = int(kwargs[ATTR_HS_COLOR][0] * (self._device_control.max_hue / 360)) hue = int(kwargs[ATTR_HS_COLOR][0] * (self._device_control.max_hue / 360))
sat = int( sat = int(
kwargs[ATTR_HS_COLOR][1] * (self._device_control.max_saturation / 100) kwargs[ATTR_HS_COLOR][1] * (self._device_control.max_saturation / 100)
@ -188,11 +183,12 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
temp_command = None temp_command = None
if ATTR_COLOR_TEMP_KELVIN in kwargs and ( if ATTR_COLOR_TEMP_KELVIN in kwargs and (
self._device_control.can_set_temp or self._device_control.can_set_color self._device_data.supports_color_temp
or self._device_data.supports_hsb_xy_color
): ):
temp_k = kwargs[ATTR_COLOR_TEMP_KELVIN] temp_k = kwargs[ATTR_COLOR_TEMP_KELVIN]
# White Spectrum bulb # White Spectrum bulb
if self._device_control.can_set_temp: if self._device_data.supports_color_temp:
temp = color_util.color_temperature_kelvin_to_mired(temp_k) temp = color_util.color_temperature_kelvin_to_mired(temp_k)
if temp < (min_mireds := self._device_control.min_mireds): if temp < (min_mireds := self._device_control.min_mireds):
temp = min_mireds temp = min_mireds
@ -205,7 +201,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
transition_time = None transition_time = None
# Color bulb (CWS) # Color bulb (CWS)
# color_temp needs to be set with hue/saturation # color_temp needs to be set with hue/saturation
elif self._device_control.can_set_color: elif self._device_data.supports_hsb_xy_color:
hs_color = color_util.color_temperature_to_hs(temp_k) hs_color = color_util.color_temperature_to_hs(temp_k)
hue = int(hs_color[0] * (self._device_control.max_hue / 360)) hue = int(hs_color[0] * (self._device_control.max_hue / 360))
sat = int(hs_color[1] * (self._device_control.max_saturation / 100)) sat = int(hs_color[1] * (self._device_control.max_saturation / 100))