Use pydeconz interface controls for lights (#75261)

This commit is contained in:
Robert Svensson 2022-07-16 20:56:48 +02:00 committed by GitHub
parent 514e826fed
commit ae4b1967a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 27 deletions

View File

@ -10,8 +10,8 @@ from typing import TYPE_CHECKING, Any, cast
import async_timeout import async_timeout
from pydeconz import DeconzSession, errors from pydeconz import DeconzSession, errors
from pydeconz.interfaces import sensors from pydeconz.interfaces import sensors
from pydeconz.interfaces.api import APIItems, GroupedAPIItems from pydeconz.interfaces.api_handlers import APIHandler, GroupedAPIHandler
from pydeconz.interfaces.groups import Groups from pydeconz.interfaces.groups import GroupHandler
from pydeconz.models.event import EventType from pydeconz.models.event import EventType
from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry
@ -127,7 +127,7 @@ class DeconzGateway:
def register_platform_add_device_callback( def register_platform_add_device_callback(
self, self,
add_device_callback: Callable[[EventType, str], None], add_device_callback: Callable[[EventType, str], None],
deconz_device_interface: APIItems | GroupedAPIItems, deconz_device_interface: APIHandler | GroupedAPIHandler,
always_ignore_clip_sensors: bool = False, always_ignore_clip_sensors: bool = False,
) -> None: ) -> None:
"""Wrap add_device_callback to check allow_new_devices option.""" """Wrap add_device_callback to check allow_new_devices option."""
@ -148,7 +148,7 @@ class DeconzGateway:
self.ignored_devices.add((async_add_device, device_id)) self.ignored_devices.add((async_add_device, device_id))
return return
if isinstance(deconz_device_interface, Groups): if isinstance(deconz_device_interface, GroupHandler):
self.deconz_groups.add((async_add_device, device_id)) self.deconz_groups.add((async_add_device, device_id))
if not self.option_allow_deconz_groups: if not self.option_allow_deconz_groups:
return return

View File

@ -3,16 +3,12 @@ from __future__ import annotations
from typing import Any, Generic, TypedDict, TypeVar from typing import Any, Generic, TypedDict, TypeVar
from pydeconz.interfaces.groups import GroupHandler
from pydeconz.interfaces.lights import LightHandler
from pydeconz.models import ResourceType from pydeconz.models import ResourceType
from pydeconz.models.event import EventType from pydeconz.models.event import EventType
from pydeconz.models.group import Group from pydeconz.models.group import Group
from pydeconz.models.light import ( from pydeconz.models.light.light import Light, LightAlert, LightColorMode, LightEffect
ALERT_LONG,
ALERT_SHORT,
EFFECT_COLOR_LOOP,
EFFECT_NONE,
)
from pydeconz.models.light.light import Light
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
@ -42,8 +38,14 @@ from .deconz_device import DeconzDevice
from .gateway import DeconzGateway, get_gateway_from_config_entry from .gateway import DeconzGateway, get_gateway_from_config_entry
DECONZ_GROUP = "is_deconz_group" DECONZ_GROUP = "is_deconz_group"
EFFECT_TO_DECONZ = {EFFECT_COLORLOOP: EFFECT_COLOR_LOOP, "None": EFFECT_NONE} EFFECT_TO_DECONZ = {EFFECT_COLORLOOP: LightEffect.COLOR_LOOP, "None": LightEffect.NONE}
FLASH_TO_DECONZ = {FLASH_SHORT: ALERT_SHORT, FLASH_LONG: ALERT_LONG} FLASH_TO_DECONZ = {FLASH_SHORT: LightAlert.SHORT, FLASH_LONG: LightAlert.LONG}
DECONZ_TO_COLOR_MODE = {
LightColorMode.CT: ColorMode.COLOR_TEMP,
LightColorMode.HS: ColorMode.HS,
LightColorMode.XY: ColorMode.XY,
}
_L = TypeVar("_L", Group, Light) _L = TypeVar("_L", Group, Light)
@ -51,10 +53,10 @@ _L = TypeVar("_L", Group, Light)
class SetStateAttributes(TypedDict, total=False): class SetStateAttributes(TypedDict, total=False):
"""Attributes available with set state call.""" """Attributes available with set state call."""
alert: str alert: LightAlert
brightness: int brightness: int
color_temperature: int color_temperature: int
effect: str effect: LightEffect
hue: int hue: int
on: bool on: bool
saturation: int saturation: int
@ -130,7 +132,13 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
"""Set up light.""" """Set up light."""
super().__init__(device, gateway) super().__init__(device, gateway)
self._attr_supported_color_modes: set[str] = set() self.api: GroupHandler | LightHandler
if isinstance(self._device, Light):
self.api = self.gateway.api.lights.lights
elif isinstance(self._device, Group):
self.api = self.gateway.api.groups
self._attr_supported_color_modes: set[ColorMode] = set()
if device.color_temp is not None: if device.color_temp is not None:
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP) self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
@ -158,12 +166,8 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
@property @property
def color_mode(self) -> str | None: def color_mode(self) -> str | None:
"""Return the color mode of the light.""" """Return the color mode of the light."""
if self._device.color_mode == "ct": if self._device.color_mode in DECONZ_TO_COLOR_MODE:
color_mode = ColorMode.COLOR_TEMP color_mode = DECONZ_TO_COLOR_MODE[self._device.color_mode]
elif self._device.color_mode == "hs":
color_mode = ColorMode.HS
elif self._device.color_mode == "xy":
color_mode = ColorMode.XY
elif self._device.brightness is not None: elif self._device.brightness is not None:
color_mode = ColorMode.BRIGHTNESS color_mode = ColorMode.BRIGHTNESS
else: else:
@ -229,7 +233,7 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
if ATTR_EFFECT in kwargs and kwargs[ATTR_EFFECT] in EFFECT_TO_DECONZ: if ATTR_EFFECT in kwargs and kwargs[ATTR_EFFECT] in EFFECT_TO_DECONZ:
data["effect"] = EFFECT_TO_DECONZ[kwargs[ATTR_EFFECT]] data["effect"] = EFFECT_TO_DECONZ[kwargs[ATTR_EFFECT]]
await self._device.set_state(**data) await self.api.set_state(id=self._device.resource_id, **data)
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off light.""" """Turn off light."""
@ -246,7 +250,7 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity):
data["alert"] = FLASH_TO_DECONZ[kwargs[ATTR_FLASH]] data["alert"] = FLASH_TO_DECONZ[kwargs[ATTR_FLASH]]
del data["on"] del data["on"]
await self._device.set_state(**data) await self.api.set_state(id=self._device.resource_id, **data)
@property @property
def extra_state_attributes(self) -> dict[str, bool]: def extra_state_attributes(self) -> dict[str, bool]:

View File

@ -3,7 +3,7 @@
"name": "deCONZ", "name": "deCONZ",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/deconz", "documentation": "https://www.home-assistant.io/integrations/deconz",
"requirements": ["pydeconz==99"], "requirements": ["pydeconz==100"],
"ssdp": [ "ssdp": [
{ {
"manufacturer": "Royal Philips Electronics", "manufacturer": "Royal Philips Electronics",

View File

@ -1447,7 +1447,7 @@ pydaikin==2.7.0
pydanfossair==0.1.0 pydanfossair==0.1.0
# homeassistant.components.deconz # homeassistant.components.deconz
pydeconz==99 pydeconz==100
# homeassistant.components.delijn # homeassistant.components.delijn
pydelijn==1.0.0 pydelijn==1.0.0

View File

@ -980,7 +980,7 @@ pycoolmasternet-async==0.1.2
pydaikin==2.7.0 pydaikin==2.7.0
# homeassistant.components.deconz # homeassistant.components.deconz
pydeconz==99 pydeconz==100
# homeassistant.components.dexcom # homeassistant.components.dexcom
pydexcom==0.2.3 pydexcom==0.2.3