mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 16:57:19 +00:00
Fix tradfri light typing except command addition
This commit is contained in:
parent
7b43714adf
commit
5474fd77b2
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, cast
|
from typing import Any
|
||||||
|
|
||||||
from pytradfri.api.aiocoap_api import APIRequestProtocol
|
from pytradfri.api.aiocoap_api import APIRequestProtocol
|
||||||
|
|
||||||
@ -68,46 +68,45 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
gateway_id=gateway_id,
|
gateway_id=gateway_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._device_control = self._device.light_control
|
device_control = self._device.light_control
|
||||||
self._device_data = self._device_control.lights[0]
|
assert device_control # light_control is ensured when creating the entity
|
||||||
|
self._device_control = device_control
|
||||||
|
self._device_data = device_control.lights[0]
|
||||||
|
|
||||||
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 self._device.light_control.can_set_color:
|
if device_control.can_set_color:
|
||||||
modes.add(ColorMode.HS)
|
modes.add(ColorMode.HS)
|
||||||
if self._device.light_control.can_set_temp:
|
if device_control.can_set_temp:
|
||||||
modes.add(ColorMode.COLOR_TEMP)
|
modes.add(ColorMode.COLOR_TEMP)
|
||||||
if self._device.light_control.can_set_dimmer:
|
if device_control.can_set_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))
|
||||||
|
|
||||||
if self._device_control:
|
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
|
||||||
self._device_control.min_mireds
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self._attr_min_color_temp_kelvin = (
|
)
|
||||||
color_util.color_temperature_mired_to_kelvin(
|
self._attr_min_color_temp_kelvin = (
|
||||||
self._device_control.max_mireds
|
color_util.color_temperature_mired_to_kelvin(
|
||||||
)
|
device_control.max_mireds
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def _refresh(self) -> None:
|
def _refresh(self) -> None:
|
||||||
"""Refresh the device."""
|
"""Refresh the device."""
|
||||||
self._device_data = self.coordinator.data.light_control.lights[0]
|
self._device_data = self._device_control.lights[0]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if light is on."""
|
"""Return true if light is on."""
|
||||||
if not self._device_data:
|
return self._device_data.state
|
||||||
return False
|
|
||||||
return cast(bool, self._device_data.state)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_mode(self) -> ColorMode | None:
|
def color_mode(self) -> ColorMode | None:
|
||||||
@ -121,36 +120,31 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
@property
|
@property
|
||||||
def brightness(self) -> int | None:
|
def brightness(self) -> int | None:
|
||||||
"""Return the brightness of the light."""
|
"""Return the brightness of the light."""
|
||||||
if not self._device_data:
|
return self._device_data.dimmer
|
||||||
return None
|
|
||||||
return cast(int, self._device_data.dimmer)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp_kelvin(self) -> int | None:
|
def color_temp_kelvin(self) -> int | None:
|
||||||
"""Return the color temperature value in Kelvin."""
|
"""Return the color temperature value in Kelvin."""
|
||||||
if not self._device_data or not (color_temp := self._device_data.color_temp):
|
if (color_temp := self._device_data.color_temp) is None:
|
||||||
return None
|
return None
|
||||||
return color_util.color_temperature_mired_to_kelvin(color_temp)
|
return color_util.color_temperature_mired_to_kelvin(color_temp)
|
||||||
|
|
||||||
@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 not self._device_control or not self._device_data:
|
|
||||||
return None
|
|
||||||
if self._device_control.can_set_color:
|
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:
|
||||||
|
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)
|
||||||
if hue is not None and sat is not None:
|
return hue, sat
|
||||||
return hue, sat
|
|
||||||
return None
|
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."""
|
||||||
# This allows transitioning to off, but resets the brightness
|
# This allows transitioning to off, but resets the brightness
|
||||||
# to 1 for the next set_state(True) command
|
# to 1 for the next set_state(True) command
|
||||||
if not self._device_control:
|
|
||||||
return
|
|
||||||
transition_time = None
|
transition_time = None
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
||||||
@ -165,21 +159,17 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Instruct the light to turn on."""
|
"""Instruct the light to turn on."""
|
||||||
if not self._device_control:
|
|
||||||
return
|
|
||||||
transition_time = None
|
transition_time = None
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
||||||
|
|
||||||
dimmer_command = None
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
brightness = kwargs[ATTR_BRIGHTNESS]
|
brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
brightness = min(brightness, 254)
|
brightness = min(brightness, 254)
|
||||||
dimmer_data = {
|
|
||||||
"dimmer": brightness,
|
dimmer_command = self._device_control.set_dimmer(
|
||||||
"transition_time": transition_time,
|
dimmer=brightness, transition_time=transition_time
|
||||||
}
|
)
|
||||||
dimmer_command = self._device_control.set_dimmer(**dimmer_data)
|
|
||||||
transition_time = None
|
transition_time = None
|
||||||
else:
|
else:
|
||||||
dimmer_command = self._device_control.set_state(True)
|
dimmer_command = self._device_control.set_state(True)
|
||||||
@ -190,12 +180,10 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
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)
|
||||||
)
|
)
|
||||||
color_data = {
|
|
||||||
"hue": hue,
|
color_command = self._device_control.set_hsb(
|
||||||
"saturation": sat,
|
hue=hue, saturation=sat, transition_time=transition_time
|
||||||
"transition_time": transition_time,
|
)
|
||||||
}
|
|
||||||
color_command = self._device_control.set_hsb(**color_data)
|
|
||||||
transition_time = None
|
transition_time = None
|
||||||
|
|
||||||
temp_command = None
|
temp_command = None
|
||||||
@ -210,11 +198,10 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
temp = min_mireds
|
temp = min_mireds
|
||||||
elif temp > (max_mireds := self._device_control.max_mireds):
|
elif temp > (max_mireds := self._device_control.max_mireds):
|
||||||
temp = max_mireds
|
temp = max_mireds
|
||||||
temp_data = {
|
|
||||||
"color_temp": temp,
|
temp_command = self._device_control.set_color_temp(
|
||||||
"transition_time": transition_time,
|
color_temp=temp, transition_time=transition_time
|
||||||
}
|
)
|
||||||
temp_command = self._device_control.set_color_temp(**temp_data)
|
|
||||||
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
|
||||||
@ -222,24 +209,20 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
|||||||
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))
|
||||||
color_data = {
|
|
||||||
"hue": hue,
|
color_command = self._device_control.set_hsb(
|
||||||
"saturation": sat,
|
hue=hue, saturation=sat, transition_time=transition_time
|
||||||
"transition_time": transition_time,
|
)
|
||||||
}
|
|
||||||
color_command = self._device_control.set_hsb(**color_data)
|
|
||||||
transition_time = None
|
transition_time = None
|
||||||
|
|
||||||
# HSB can always be set, but color temp + brightness is bulb dependent
|
# HSB can always be set, but color temp + brightness is bulb dependent
|
||||||
if (command := dimmer_command) is not None:
|
command = dimmer_command
|
||||||
|
if color_command is not None:
|
||||||
command += color_command
|
command += color_command
|
||||||
else:
|
|
||||||
command = color_command
|
|
||||||
|
|
||||||
if self._device_control.can_combine_commands:
|
if self._device_control.can_combine_commands and temp_command is not None:
|
||||||
await self._api(command + temp_command)
|
await self._api(command + temp_command)
|
||||||
else:
|
else:
|
||||||
if temp_command is not None:
|
if temp_command is not None:
|
||||||
await self._api(temp_command)
|
await self._api(temp_command)
|
||||||
if command is not None:
|
await self._api(command)
|
||||||
await self._api(command)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user