Implement recommendation

This commit is contained in:
epenet 2022-08-01 10:22:57 +00:00
parent 72ecfbce5a
commit fdf548124e
12 changed files with 41 additions and 32 deletions

View file

@ -80,9 +80,7 @@ async def async_setup_entry(
) )
class DeconzAlarmControlPanel( class DeconzAlarmControlPanel(DeconzDevice[AncillaryControl], AlarmControlPanelEntity):
DeconzDevice[AncillaryControl], AlarmControlPanelEntity # type: ignore[type-var]
):
"""Representation of a deCONZ alarm control panel.""" """Representation of a deCONZ alarm control panel."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -233,9 +233,7 @@ async def async_setup_entry(
) )
class DeconzBinarySensor( class DeconzBinarySensor(DeconzDevice[SensorResources], BinarySensorEntity):
DeconzDevice[SensorResources], BinarySensorEntity # type: ignore[type-var]
):
"""Representation of a deCONZ binary sensor.""" """Representation of a deCONZ binary sensor."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -92,9 +92,7 @@ async def async_setup_entry(
) )
class DeconzThermostat( class DeconzThermostat(DeconzDevice[Thermostat], ClimateEntity):
DeconzDevice[Thermostat], ClimateEntity # type: ignore[type-var]
):
"""Representation of a deCONZ thermostat.""" """Representation of a deCONZ thermostat."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -49,7 +49,7 @@ async def async_setup_entry(
) )
class DeconzCover(DeconzDevice[Cover], CoverEntity): # type: ignore[type-var] class DeconzCover(DeconzDevice[Cover], CoverEntity):
"""Representation of a deCONZ cover.""" """Representation of a deCONZ cover."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Generic, TypeVar from typing import TYPE_CHECKING, Generic, TypeVar, Union
from pydeconz.models.group import Group as PydeconzGroup from pydeconz.models.group import Group as PydeconzGroup
from pydeconz.models.light import LightBase as PydeconzLightBase from pydeconz.models.light import LightBase as PydeconzLightBase
@ -18,7 +18,13 @@ from .const import DOMAIN as DECONZ_DOMAIN
from .gateway import DeconzGateway from .gateway import DeconzGateway
_DeviceTypeT = TypeVar( _DeviceTypeT = TypeVar(
"_DeviceTypeT", PydeconzGroup, PydeconzLightBase, PydeconzScene, PydeconzSensorBase "_DeviceTypeT",
bound=Union[
PydeconzGroup,
PydeconzLightBase,
PydeconzSensorBase,
PydeconzScene,
],
) )
@ -37,13 +43,19 @@ class DeconzBase(Generic[_DeviceTypeT]):
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique identifier for this device.""" """Return a unique identifier for this device."""
assert not isinstance(self._device, PydeconzScene) if TYPE_CHECKING:
assert isinstance(
self._device, (PydeconzGroup, PydeconzLightBase, PydeconzSensorBase)
)
return self._device.unique_id return self._device.unique_id
@property @property
def serial(self) -> str | None: def serial(self) -> str | None:
"""Return a serial number for this device.""" """Return a serial number for this device."""
assert not isinstance(self._device, PydeconzScene) if TYPE_CHECKING:
assert isinstance(
self._device, (PydeconzGroup, PydeconzLightBase, PydeconzSensorBase)
)
if not self._device.unique_id or self._device.unique_id.count(":") != 7: if not self._device.unique_id or self._device.unique_id.count(":") != 7:
return None return None
return self._device.unique_id.split("-", 1)[0] return self._device.unique_id.split("-", 1)[0]
@ -51,7 +63,10 @@ class DeconzBase(Generic[_DeviceTypeT]):
@property @property
def device_info(self) -> DeviceInfo | None: def device_info(self) -> DeviceInfo | None:
"""Return a device description for device registry.""" """Return a device description for device registry."""
assert not isinstance(self._device, PydeconzScene) if TYPE_CHECKING:
assert isinstance(
self._device, (PydeconzGroup, PydeconzLightBase, PydeconzSensorBase)
)
if self.serial is None: if self.serial is None:
return None return None
@ -79,7 +94,7 @@ class DeconzDevice(DeconzBase[_DeviceTypeT], Entity):
gateway: DeconzGateway, gateway: DeconzGateway,
) -> None: ) -> None:
"""Set up device and add update callback to get data from websocket.""" """Set up device and add update callback to get data from websocket."""
super().__init__(device, gateway) # type: ignore[arg-type] super().__init__(device, gateway)
self.gateway.entities[self.TYPE].add(self.unique_id) self.gateway.entities[self.TYPE].add(self.unique_id)
self._attr_name = self._device.name self._attr_name = self._device.name
@ -120,6 +135,10 @@ class DeconzDevice(DeconzBase[_DeviceTypeT], Entity):
"""Return True if device is available.""" """Return True if device is available."""
if isinstance(self._device, PydeconzScene): if isinstance(self._device, PydeconzScene):
return self.gateway.available return self.gateway.available
if TYPE_CHECKING:
assert isinstance(
self._device, (PydeconzGroup, PydeconzLightBase, PydeconzSensorBase)
)
return self.gateway.available and self._device.reachable return self.gateway.available and self._device.reachable

View file

@ -49,7 +49,7 @@ async def async_setup_entry(
) )
class DeconzFan(DeconzDevice[Light], FanEntity): # type: ignore[type-var] class DeconzFan(DeconzDevice[Light], FanEntity):
"""Representation of a deCONZ fan.""" """Representation of a deCONZ fan."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -1,7 +1,7 @@
"""Support for deCONZ lights.""" """Support for deCONZ lights."""
from __future__ import annotations from __future__ import annotations
from typing import Any, TypedDict, TypeVar from typing import Any, TypedDict, TypeVar, Union
from pydeconz.interfaces.groups import GroupHandler from pydeconz.interfaces.groups import GroupHandler
from pydeconz.interfaces.lights import LightHandler from pydeconz.interfaces.lights import LightHandler
@ -47,7 +47,7 @@ DECONZ_TO_COLOR_MODE = {
LightColorMode.XY: ColorMode.XY, LightColorMode.XY: ColorMode.XY,
} }
_LightDeviceTypeT = TypeVar("_LightDeviceTypeT", Group, Light) _LightDeviceTypeT = TypeVar("_LightDeviceTypeT", bound=Union[Group, Light])
class SetStateAttributes(TypedDict, total=False): class SetStateAttributes(TypedDict, total=False):
@ -121,16 +121,14 @@ async def async_setup_entry(
) )
class DeconzBaseLight( class DeconzBaseLight(DeconzDevice[_LightDeviceTypeT], LightEntity):
DeconzDevice[_LightDeviceTypeT], LightEntity # type: ignore[type-var]
):
"""Representation of a deCONZ light.""" """Representation of a deCONZ light."""
TYPE = DOMAIN TYPE = DOMAIN
def __init__(self, device: _LightDeviceTypeT, gateway: DeconzGateway) -> None: def __init__(self, device: _LightDeviceTypeT, gateway: DeconzGateway) -> None:
"""Set up light.""" """Set up light."""
super().__init__(device, gateway) # type: ignore[arg-type] super().__init__(device, gateway)
self.api: GroupHandler | LightHandler self.api: GroupHandler | LightHandler
if isinstance(self._device, Light): if isinstance(self._device, Light):

View file

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Any, TypeVar from typing import Any, TypeVar, Union
from pydeconz.models.event import EventType from pydeconz.models.event import EventType
from pydeconz.models.light.lock import Lock from pydeconz.models.light.lock import Lock
@ -16,7 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzDevice from .deconz_device import DeconzDevice
from .gateway import get_gateway_from_config_entry from .gateway import get_gateway_from_config_entry
_LockDeviceTypeT = TypeVar("_LockDeviceTypeT", DoorLock, Lock) _LockDeviceTypeT = TypeVar("_LockDeviceTypeT", bound=Union[DoorLock, Lock])
async def async_setup_entry( async def async_setup_entry(
@ -52,7 +52,7 @@ async def async_setup_entry(
) )
class DeconzLock(DeconzDevice[_LockDeviceTypeT], LockEntity): # type: ignore[type-var] class DeconzLock(DeconzDevice[_LockDeviceTypeT], LockEntity):
"""Representation of a deCONZ lock.""" """Representation of a deCONZ lock."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -81,7 +81,7 @@ async def async_setup_entry(
) )
class DeconzNumber(DeconzDevice[Presence], NumberEntity): # type: ignore[type-var] class DeconzNumber(DeconzDevice[Presence], NumberEntity):
"""Representation of a deCONZ number entity.""" """Representation of a deCONZ number entity."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -273,9 +273,7 @@ async def async_setup_entry(
) )
class DeconzSensor( class DeconzSensor(DeconzDevice[SensorResources], SensorEntity):
DeconzDevice[SensorResources], SensorEntity # type: ignore[type-var]
):
"""Representation of a deCONZ sensor.""" """Representation of a deCONZ sensor."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -41,7 +41,7 @@ async def async_setup_entry(
) )
class DeconzSiren(DeconzDevice[Siren], SirenEntity): # type: ignore[type-var] class DeconzSiren(DeconzDevice[Siren], SirenEntity):
"""Representation of a deCONZ siren.""" """Representation of a deCONZ siren."""
TYPE = DOMAIN TYPE = DOMAIN

View file

@ -43,7 +43,7 @@ async def async_setup_entry(
) )
class DeconzPowerPlug(DeconzDevice[Light], SwitchEntity): # type: ignore[type-var] class DeconzPowerPlug(DeconzDevice[Light], SwitchEntity):
"""Representation of a deCONZ power plug.""" """Representation of a deCONZ power plug."""
TYPE = DOMAIN TYPE = DOMAIN