mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Improve type hints in yeelight lights (#76018)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
eb0b6f3d75
commit
66b742f110
@ -63,6 +63,7 @@ from .const import (
|
|||||||
MODELS_WITH_DELAYED_ON_TRANSITION,
|
MODELS_WITH_DELAYED_ON_TRANSITION,
|
||||||
POWER_STATE_CHANGE_TIME,
|
POWER_STATE_CHANGE_TIME,
|
||||||
)
|
)
|
||||||
|
from .device import YeelightDevice
|
||||||
from .entity import YeelightEntity
|
from .entity import YeelightEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -218,7 +219,7 @@ def _transitions_config_parser(transitions):
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _parse_custom_effects(effects_config):
|
def _parse_custom_effects(effects_config) -> dict[str, dict[str, Any]]:
|
||||||
effects = {}
|
effects = {}
|
||||||
for config in effects_config:
|
for config in effects_config:
|
||||||
params = config[CONF_FLOW_PARAMS]
|
params = config[CONF_FLOW_PARAMS]
|
||||||
@ -414,18 +415,23 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
)
|
)
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, device, entry, custom_effects=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: YeelightDevice,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
custom_effects: dict[str, dict[str, Any]] | None = None,
|
||||||
|
) -> None:
|
||||||
"""Initialize the Yeelight light."""
|
"""Initialize the Yeelight light."""
|
||||||
super().__init__(device, entry)
|
super().__init__(device, entry)
|
||||||
|
|
||||||
self.config = device.config
|
self.config = device.config
|
||||||
|
|
||||||
self._color_temp = None
|
self._color_temp: int | None = None
|
||||||
self._effect = None
|
self._effect = None
|
||||||
|
|
||||||
model_specs = self._bulb.get_model_specs()
|
model_specs = self._bulb.get_model_specs()
|
||||||
self._min_mireds = kelvin_to_mired(model_specs["color_temp"]["max"])
|
self._attr_min_mireds = kelvin_to_mired(model_specs["color_temp"]["max"])
|
||||||
self._max_mireds = kelvin_to_mired(model_specs["color_temp"]["min"])
|
self._attr_max_mireds = kelvin_to_mired(model_specs["color_temp"]["min"])
|
||||||
|
|
||||||
self._light_type = LightType.Main
|
self._light_type = LightType.Main
|
||||||
|
|
||||||
@ -437,7 +443,7 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
self._unexpected_state_check = None
|
self._unexpected_state_check = None
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed(self):
|
def async_state_changed(self) -> None:
|
||||||
"""Call when the device changes state."""
|
"""Call when the device changes state."""
|
||||||
if not self._device.available:
|
if not self._device.available:
|
||||||
self._async_cancel_pending_state_check()
|
self._async_cancel_pending_state_check()
|
||||||
@ -455,12 +461,12 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def effect_list(self):
|
def effect_list(self) -> list[str]:
|
||||||
"""Return the list of supported effects."""
|
"""Return the list of supported effects."""
|
||||||
return self._predefined_effects + self.custom_effects_names
|
return self._predefined_effects + self.custom_effects_names
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp(self) -> int:
|
def color_temp(self) -> int | None:
|
||||||
"""Return the color temperature."""
|
"""Return the color temperature."""
|
||||||
if temp_in_k := self._get_property("ct"):
|
if temp_in_k := self._get_property("ct"):
|
||||||
self._color_temp = kelvin_to_mired(int(temp_in_k))
|
self._color_temp = kelvin_to_mired(int(temp_in_k))
|
||||||
@ -489,32 +495,22 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
return round(255 * (int(brightness) / 100))
|
return round(255 * (int(brightness) / 100))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def min_mireds(self):
|
def custom_effects(self) -> dict[str, dict[str, Any]]:
|
||||||
"""Return minimum supported color temperature."""
|
|
||||||
return self._min_mireds
|
|
||||||
|
|
||||||
@property
|
|
||||||
def max_mireds(self):
|
|
||||||
"""Return maximum supported color temperature."""
|
|
||||||
return self._max_mireds
|
|
||||||
|
|
||||||
@property
|
|
||||||
def custom_effects(self):
|
|
||||||
"""Return dict with custom effects."""
|
"""Return dict with custom effects."""
|
||||||
return self._custom_effects
|
return self._custom_effects
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def custom_effects_names(self):
|
def custom_effects_names(self) -> list[str]:
|
||||||
"""Return list with custom effects names."""
|
"""Return list with custom effects names."""
|
||||||
return list(self.custom_effects)
|
return list(self.custom_effects)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def light_type(self):
|
def light_type(self) -> LightType:
|
||||||
"""Return light type."""
|
"""Return light type."""
|
||||||
return self._light_type
|
return self._light_type
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hs_color(self) -> tuple[int, int] | None:
|
def hs_color(self) -> tuple[float, float] | None:
|
||||||
"""Return the color property."""
|
"""Return the color property."""
|
||||||
hue = self._get_property("hue")
|
hue = self._get_property("hue")
|
||||||
sat = self._get_property("sat")
|
sat = self._get_property("sat")
|
||||||
@ -537,7 +533,7 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
return (red, green, blue)
|
return (red, green, blue)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def effect(self):
|
def effect(self) -> str | None:
|
||||||
"""Return the current effect."""
|
"""Return the current effect."""
|
||||||
return self._effect if self.device.is_color_flow_enabled else None
|
return self._effect if self.device.is_color_flow_enabled else None
|
||||||
|
|
||||||
@ -549,27 +545,27 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
def _properties(self) -> dict:
|
def _properties(self) -> dict:
|
||||||
return self._bulb.last_properties if self._bulb else {}
|
return self._bulb.last_properties if self._bulb else {}
|
||||||
|
|
||||||
def _get_property(self, prop, default=None):
|
def _get_property(self, prop: str, default=None):
|
||||||
return self._properties.get(prop, default)
|
return self._properties.get(prop, default)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _brightness_property(self):
|
def _brightness_property(self) -> str:
|
||||||
return "bright"
|
return "bright"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _power_property(self):
|
def _power_property(self) -> str:
|
||||||
return "power"
|
return "power"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _turn_on_power_mode(self):
|
def _turn_on_power_mode(self) -> PowerMode:
|
||||||
return PowerMode.LAST
|
return PowerMode.LAST
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _predefined_effects(self):
|
def _predefined_effects(self) -> list[str]:
|
||||||
return YEELIGHT_MONO_EFFECT_LIST
|
return YEELIGHT_MONO_EFFECT_LIST
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the device specific state attributes."""
|
"""Return the device specific state attributes."""
|
||||||
attributes = {
|
attributes = {
|
||||||
"flowing": self.device.is_color_flow_enabled,
|
"flowing": self.device.is_color_flow_enabled,
|
||||||
@ -582,7 +578,7 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
|||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device(self):
|
def device(self) -> YeelightDevice:
|
||||||
"""Return yeelight device."""
|
"""Return yeelight device."""
|
||||||
return self._device
|
return self._device
|
||||||
|
|
||||||
@ -890,7 +886,7 @@ class YeelightColorLightSupport(YeelightGenericLight):
|
|||||||
return ColorMode.UNKNOWN
|
return ColorMode.UNKNOWN
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _predefined_effects(self):
|
def _predefined_effects(self) -> list[str]:
|
||||||
return YEELIGHT_COLOR_EFFECT_LIST
|
return YEELIGHT_COLOR_EFFECT_LIST
|
||||||
|
|
||||||
|
|
||||||
@ -901,7 +897,7 @@ class YeelightWhiteTempLightSupport(YeelightGenericLight):
|
|||||||
_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
|
_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _predefined_effects(self):
|
def _predefined_effects(self) -> list[str]:
|
||||||
return YEELIGHT_TEMP_ONLY_EFFECT_LIST
|
return YEELIGHT_TEMP_ONLY_EFFECT_LIST
|
||||||
|
|
||||||
|
|
||||||
@ -909,7 +905,7 @@ class YeelightNightLightSupport:
|
|||||||
"""Representation of a Yeelight nightlight support."""
|
"""Representation of a Yeelight nightlight support."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _turn_on_power_mode(self):
|
def _turn_on_power_mode(self) -> PowerMode:
|
||||||
return PowerMode.NORMAL
|
return PowerMode.NORMAL
|
||||||
|
|
||||||
|
|
||||||
@ -917,7 +913,7 @@ class YeelightWithoutNightlightSwitchMixIn(YeelightGenericLight):
|
|||||||
"""A mix-in for yeelights without a nightlight switch."""
|
"""A mix-in for yeelights without a nightlight switch."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _brightness_property(self):
|
def _brightness_property(self) -> str:
|
||||||
# If the nightlight is not active, we do not
|
# If the nightlight is not active, we do not
|
||||||
# want to "current_brightness" since it will check
|
# want to "current_brightness" since it will check
|
||||||
# "bg_power" and main light could still be on
|
# "bg_power" and main light could still be on
|
||||||
@ -926,11 +922,11 @@ class YeelightWithoutNightlightSwitchMixIn(YeelightGenericLight):
|
|||||||
return super()._brightness_property
|
return super()._brightness_property
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp(self) -> int:
|
def color_temp(self) -> int | None:
|
||||||
"""Return the color temperature."""
|
"""Return the color temperature."""
|
||||||
if self.device.is_nightlight_enabled:
|
if self.device.is_nightlight_enabled:
|
||||||
# Enabling the nightlight locks the colortemp to max
|
# Enabling the nightlight locks the colortemp to max
|
||||||
return self._max_mireds
|
return self.max_mireds
|
||||||
return super().color_temp
|
return super().color_temp
|
||||||
|
|
||||||
|
|
||||||
@ -978,6 +974,7 @@ class YeelightNightLightMode(YeelightGenericLight):
|
|||||||
"""Representation of a Yeelight when in nightlight mode."""
|
"""Representation of a Yeelight when in nightlight mode."""
|
||||||
|
|
||||||
_attr_color_mode = ColorMode.BRIGHTNESS
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
||||||
|
_attr_icon = "mdi:weather-night"
|
||||||
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -991,26 +988,21 @@ class YeelightNightLightMode(YeelightGenericLight):
|
|||||||
"""Return the name of the device if any."""
|
"""Return the name of the device if any."""
|
||||||
return f"{self.device.name} Nightlight"
|
return f"{self.device.name} Nightlight"
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon to use in the frontend, if any."""
|
|
||||||
return "mdi:weather-night"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return super().is_on and self.device.is_nightlight_enabled
|
return super().is_on and self.device.is_nightlight_enabled
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _brightness_property(self):
|
def _brightness_property(self) -> str:
|
||||||
return "nl_br"
|
return "nl_br"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _turn_on_power_mode(self):
|
def _turn_on_power_mode(self) -> PowerMode:
|
||||||
return PowerMode.MOONLIGHT
|
return PowerMode.MOONLIGHT
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self) -> int:
|
||||||
"""Flag no supported features."""
|
"""Flag no supported features."""
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ -1019,7 +1011,7 @@ class YeelightNightLightModeWithAmbientSupport(YeelightNightLightMode):
|
|||||||
"""Representation of a Yeelight, with ambient support, when in nightlight mode."""
|
"""Representation of a Yeelight, with ambient support, when in nightlight mode."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _power_property(self):
|
def _power_property(self) -> str:
|
||||||
return "main_power"
|
return "main_power"
|
||||||
|
|
||||||
|
|
||||||
@ -1040,7 +1032,7 @@ class YeelightWithAmbientWithoutNightlight(YeelightWhiteTempWithoutNightlightSwi
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _power_property(self):
|
def _power_property(self) -> str:
|
||||||
return "main_power"
|
return "main_power"
|
||||||
|
|
||||||
|
|
||||||
@ -1051,7 +1043,7 @@ class YeelightWithAmbientAndNightlight(YeelightWithNightLight):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _power_property(self):
|
def _power_property(self) -> str:
|
||||||
return "main_power"
|
return "main_power"
|
||||||
|
|
||||||
|
|
||||||
@ -1063,8 +1055,8 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""Initialize the Yeelight Ambient light."""
|
"""Initialize the Yeelight Ambient light."""
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._min_mireds = kelvin_to_mired(6500)
|
self._attr_min_mireds = kelvin_to_mired(6500)
|
||||||
self._max_mireds = kelvin_to_mired(1700)
|
self._attr_max_mireds = kelvin_to_mired(1700)
|
||||||
|
|
||||||
self._light_type = LightType.Ambient
|
self._light_type = LightType.Ambient
|
||||||
|
|
||||||
@ -1080,10 +1072,10 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch):
|
|||||||
return f"{self.device.name} Ambilight"
|
return f"{self.device.name} Ambilight"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _brightness_property(self):
|
def _brightness_property(self) -> str:
|
||||||
return "bright"
|
return "bright"
|
||||||
|
|
||||||
def _get_property(self, prop, default=None):
|
def _get_property(self, prop: str, default=None):
|
||||||
if not (bg_prop := self.PROPERTIES_MAPPING.get(prop)):
|
if not (bg_prop := self.PROPERTIES_MAPPING.get(prop)):
|
||||||
bg_prop = f"bg_{prop}"
|
bg_prop = f"bg_{prop}"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user