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 typing import Any, cast
|
||||
from typing import Any
|
||||
|
||||
from pytradfri.api.aiocoap_api import APIRequestProtocol
|
||||
|
||||
@ -68,46 +68,45 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
||||
gateway_id=gateway_id,
|
||||
)
|
||||
|
||||
self._device_control = self._device.light_control
|
||||
self._device_data = self._device_control.lights[0]
|
||||
device_control = self._device.light_control
|
||||
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._hs_color = None
|
||||
|
||||
# Calculate supported color modes
|
||||
modes: set[ColorMode] = {ColorMode.ONOFF}
|
||||
if self._device.light_control.can_set_color:
|
||||
if device_control.can_set_color:
|
||||
modes.add(ColorMode.HS)
|
||||
if self._device.light_control.can_set_temp:
|
||||
if device_control.can_set_temp:
|
||||
modes.add(ColorMode.COLOR_TEMP)
|
||||
if self._device.light_control.can_set_dimmer:
|
||||
if device_control.can_set_dimmer:
|
||||
modes.add(ColorMode.BRIGHTNESS)
|
||||
self._attr_supported_color_modes = filter_supported_color_modes(modes)
|
||||
if len(self._attr_supported_color_modes) == 1:
|
||||
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
|
||||
|
||||
if self._device_control:
|
||||
self._attr_max_color_temp_kelvin = (
|
||||
color_util.color_temperature_mired_to_kelvin(
|
||||
self._device_control.min_mireds
|
||||
)
|
||||
self._attr_max_color_temp_kelvin = (
|
||||
color_util.color_temperature_mired_to_kelvin(
|
||||
device_control.min_mireds
|
||||
)
|
||||
self._attr_min_color_temp_kelvin = (
|
||||
color_util.color_temperature_mired_to_kelvin(
|
||||
self._device_control.max_mireds
|
||||
)
|
||||
)
|
||||
self._attr_min_color_temp_kelvin = (
|
||||
color_util.color_temperature_mired_to_kelvin(
|
||||
device_control.max_mireds
|
||||
)
|
||||
)
|
||||
|
||||
def _refresh(self) -> None:
|
||||
"""Refresh the device."""
|
||||
self._device_data = self.coordinator.data.light_control.lights[0]
|
||||
self._device_data = self._device_control.lights[0]
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if light is on."""
|
||||
if not self._device_data:
|
||||
return False
|
||||
return cast(bool, self._device_data.state)
|
||||
return self._device_data.state
|
||||
|
||||
@property
|
||||
def color_mode(self) -> ColorMode | None:
|
||||
@ -121,36 +120,31 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
||||
@property
|
||||
def brightness(self) -> int | None:
|
||||
"""Return the brightness of the light."""
|
||||
if not self._device_data:
|
||||
return None
|
||||
return cast(int, self._device_data.dimmer)
|
||||
return self._device_data.dimmer
|
||||
|
||||
@property
|
||||
def color_temp_kelvin(self) -> int | None:
|
||||
"""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 color_util.color_temperature_mired_to_kelvin(color_temp)
|
||||
|
||||
@property
|
||||
def hs_color(self) -> tuple[float, float] | None:
|
||||
"""HS color of the light."""
|
||||
if not self._device_control or not self._device_data:
|
||||
return None
|
||||
if self._device_control.can_set_color:
|
||||
hsbxy = self._device_data.hsb_xy_color
|
||||
if hsbxy is None:
|
||||
return None
|
||||
hue = hsbxy[0] / (self._device_control.max_hue / 360)
|
||||
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
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Instruct the light to turn off."""
|
||||
# This allows transitioning to off, but resets the brightness
|
||||
# to 1 for the next set_state(True) command
|
||||
if not self._device_control:
|
||||
return
|
||||
transition_time = None
|
||||
if ATTR_TRANSITION in kwargs:
|
||||
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
||||
@ -165,21 +159,17 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Instruct the light to turn on."""
|
||||
if not self._device_control:
|
||||
return
|
||||
transition_time = None
|
||||
if ATTR_TRANSITION in kwargs:
|
||||
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
||||
|
||||
dimmer_command = None
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
brightness = kwargs[ATTR_BRIGHTNESS]
|
||||
brightness = min(brightness, 254)
|
||||
dimmer_data = {
|
||||
"dimmer": brightness,
|
||||
"transition_time": transition_time,
|
||||
}
|
||||
dimmer_command = self._device_control.set_dimmer(**dimmer_data)
|
||||
|
||||
dimmer_command = self._device_control.set_dimmer(
|
||||
dimmer=brightness, transition_time=transition_time
|
||||
)
|
||||
transition_time = None
|
||||
else:
|
||||
dimmer_command = self._device_control.set_state(True)
|
||||
@ -190,12 +180,10 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
||||
sat = int(
|
||||
kwargs[ATTR_HS_COLOR][1] * (self._device_control.max_saturation / 100)
|
||||
)
|
||||
color_data = {
|
||||
"hue": hue,
|
||||
"saturation": sat,
|
||||
"transition_time": transition_time,
|
||||
}
|
||||
color_command = self._device_control.set_hsb(**color_data)
|
||||
|
||||
color_command = self._device_control.set_hsb(
|
||||
hue=hue, saturation=sat, transition_time=transition_time
|
||||
)
|
||||
transition_time = None
|
||||
|
||||
temp_command = None
|
||||
@ -210,11 +198,10 @@ class TradfriLight(TradfriBaseEntity, LightEntity):
|
||||
temp = min_mireds
|
||||
elif temp > (max_mireds := self._device_control.max_mireds):
|
||||
temp = max_mireds
|
||||
temp_data = {
|
||||
"color_temp": temp,
|
||||
"transition_time": transition_time,
|
||||
}
|
||||
temp_command = self._device_control.set_color_temp(**temp_data)
|
||||
|
||||
temp_command = self._device_control.set_color_temp(
|
||||
color_temp=temp, transition_time=transition_time
|
||||
)
|
||||
transition_time = None
|
||||
# Color bulb (CWS)
|
||||
# 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)
|
||||
hue = int(hs_color[0] * (self._device_control.max_hue / 360))
|
||||
sat = int(hs_color[1] * (self._device_control.max_saturation / 100))
|
||||
color_data = {
|
||||
"hue": hue,
|
||||
"saturation": sat,
|
||||
"transition_time": transition_time,
|
||||
}
|
||||
color_command = self._device_control.set_hsb(**color_data)
|
||||
|
||||
color_command = self._device_control.set_hsb(
|
||||
hue=hue, saturation=sat, transition_time=transition_time
|
||||
)
|
||||
transition_time = None
|
||||
|
||||
# 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
|
||||
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)
|
||||
else:
|
||||
if temp_command is not None:
|
||||
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