Type check KNX integration light (#48053)
* type check light * review changes
This commit is contained in:
parent
943ce8afaf
commit
fb1e76db8c
1 changed files with 40 additions and 22 deletions
|
@ -1,4 +1,8 @@
|
|||
"""Support for KNX/IP lights."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, Iterable
|
||||
|
||||
from xknx.devices import Light as XknxLight
|
||||
|
||||
from homeassistant.components.light import (
|
||||
|
@ -12,17 +16,29 @@ from homeassistant.components.light import (
|
|||
SUPPORT_WHITE_VALUE,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import (
|
||||
ConfigType,
|
||||
DiscoveryInfoType,
|
||||
HomeAssistantType,
|
||||
)
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
from .const import DOMAIN
|
||||
from .knx_entity import KnxEntity
|
||||
from .schema import LightSchema
|
||||
|
||||
DEFAULT_COLOR = (0.0, 0.0)
|
||||
DEFAULT_BRIGHTNESS = 255
|
||||
DEFAULT_WHITE_VALUE = 255
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistantType,
|
||||
config: ConfigType,
|
||||
async_add_entities: Callable[[Iterable[Entity]], None],
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up lights for KNX platform."""
|
||||
entities = []
|
||||
for device in hass.data[DOMAIN].xknx.devices:
|
||||
|
@ -34,12 +50,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
class KNXLight(KnxEntity, LightEntity):
|
||||
"""Representation of a KNX light."""
|
||||
|
||||
def __init__(self, device: XknxLight):
|
||||
def __init__(self, device: XknxLight) -> None:
|
||||
"""Initialize of KNX light."""
|
||||
self._device: XknxLight
|
||||
super().__init__(device)
|
||||
|
||||
self._min_kelvin = device.min_kelvin
|
||||
self._max_kelvin = device.max_kelvin
|
||||
self._min_kelvin = device.min_kelvin or LightSchema.DEFAULT_MIN_KELVIN
|
||||
self._max_kelvin = device.max_kelvin or LightSchema.DEFAULT_MAX_KELVIN
|
||||
self._min_mireds = color_util.color_temperature_kelvin_to_mired(
|
||||
self._max_kelvin
|
||||
)
|
||||
|
@ -48,7 +65,7 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
)
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
def brightness(self) -> int | None:
|
||||
"""Return the brightness of this light between 0..255."""
|
||||
if self._device.supports_brightness:
|
||||
return self._device.current_brightness
|
||||
|
@ -58,31 +75,31 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
return None
|
||||
|
||||
@property
|
||||
def hs_color(self):
|
||||
def hs_color(self) -> tuple[float, float] | None:
|
||||
"""Return the HS color value."""
|
||||
rgb = None
|
||||
rgb: tuple[int, int, int] | None = None
|
||||
if self._device.supports_rgbw or self._device.supports_color:
|
||||
rgb, _ = self._device.current_color
|
||||
return color_util.color_RGB_to_hs(*rgb) if rgb else None
|
||||
|
||||
@property
|
||||
def _hsv_color(self):
|
||||
def _hsv_color(self) -> tuple[float, float, float] | None:
|
||||
"""Return the HSV color value."""
|
||||
rgb = None
|
||||
rgb: tuple[int, int, int] | None = None
|
||||
if self._device.supports_rgbw or self._device.supports_color:
|
||||
rgb, _ = self._device.current_color
|
||||
return color_util.color_RGB_to_hsv(*rgb) if rgb else None
|
||||
|
||||
@property
|
||||
def white_value(self):
|
||||
def white_value(self) -> int | None:
|
||||
"""Return the white value."""
|
||||
white = None
|
||||
white: int | None = None
|
||||
if self._device.supports_rgbw:
|
||||
_, white = self._device.current_color
|
||||
return white
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
def color_temp(self) -> int | None:
|
||||
"""Return the color temperature in mireds."""
|
||||
if self._device.supports_color_temperature:
|
||||
kelvin = self._device.current_color_temperature
|
||||
|
@ -101,32 +118,32 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
return None
|
||||
|
||||
@property
|
||||
def min_mireds(self):
|
||||
def min_mireds(self) -> int:
|
||||
"""Return the coldest color temp this light supports in mireds."""
|
||||
return self._min_mireds
|
||||
|
||||
@property
|
||||
def max_mireds(self):
|
||||
def max_mireds(self) -> int:
|
||||
"""Return the warmest color temp this light supports in mireds."""
|
||||
return self._max_mireds
|
||||
|
||||
@property
|
||||
def effect_list(self):
|
||||
def effect_list(self) -> list[str] | None:
|
||||
"""Return the list of supported effects."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def effect(self):
|
||||
def effect(self) -> str | None:
|
||||
"""Return the current effect."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if light is on."""
|
||||
return self._device.state
|
||||
return bool(self._device.state)
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
def supported_features(self) -> int:
|
||||
"""Flag supported features."""
|
||||
flags = 0
|
||||
if self._device.supports_brightness:
|
||||
|
@ -142,7 +159,7 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
flags |= SUPPORT_COLOR_TEMP
|
||||
return flags
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the light on."""
|
||||
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
||||
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
||||
|
@ -183,7 +200,8 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
hs_color = DEFAULT_COLOR
|
||||
if white_value is None and self._device.supports_rgbw:
|
||||
white_value = DEFAULT_WHITE_VALUE
|
||||
rgb = color_util.color_hsv_to_RGB(*hs_color, brightness * 100 / 255)
|
||||
hsv_color = hs_color + (brightness * 100 / 255,)
|
||||
rgb = color_util.color_hsv_to_RGB(*hsv_color)
|
||||
await self._device.set_color(rgb, white_value)
|
||||
|
||||
if update_color_temp:
|
||||
|
@ -200,6 +218,6 @@ class KNXLight(KnxEntity, LightEntity):
|
|||
)
|
||||
await self._device.set_tunable_white(relative_ct)
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the light off."""
|
||||
await self._device.set_off()
|
||||
|
|
Loading…
Add table
Reference in a new issue