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(
DeconzDevice[AncillaryControl], AlarmControlPanelEntity # type: ignore[type-var]
):
class DeconzAlarmControlPanel(DeconzDevice[AncillaryControl], AlarmControlPanelEntity):
"""Representation of a deCONZ alarm control panel."""
TYPE = DOMAIN

View file

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

View file

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

View file

@ -2,7 +2,7 @@
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.light import LightBase as PydeconzLightBase
@ -18,7 +18,13 @@ from .const import DOMAIN as DECONZ_DOMAIN
from .gateway import DeconzGateway
_DeviceTypeT = TypeVar(
"_DeviceTypeT", PydeconzGroup, PydeconzLightBase, PydeconzScene, PydeconzSensorBase
"_DeviceTypeT",
bound=Union[
PydeconzGroup,
PydeconzLightBase,
PydeconzSensorBase,
PydeconzScene,
],
)
@ -37,13 +43,19 @@ class DeconzBase(Generic[_DeviceTypeT]):
@property
def unique_id(self) -> str:
"""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
@property
def serial(self) -> str | None:
"""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:
return None
return self._device.unique_id.split("-", 1)[0]
@ -51,7 +63,10 @@ class DeconzBase(Generic[_DeviceTypeT]):
@property
def device_info(self) -> DeviceInfo | None:
"""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:
return None
@ -79,7 +94,7 @@ class DeconzDevice(DeconzBase[_DeviceTypeT], Entity):
gateway: DeconzGateway,
) -> None:
"""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._attr_name = self._device.name
@ -120,6 +135,10 @@ class DeconzDevice(DeconzBase[_DeviceTypeT], Entity):
"""Return True if device is available."""
if isinstance(self._device, PydeconzScene):
return self.gateway.available
if TYPE_CHECKING:
assert isinstance(
self._device, (PydeconzGroup, PydeconzLightBase, PydeconzSensorBase)
)
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."""
TYPE = DOMAIN

View file

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

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from typing import Any, TypeVar
from typing import Any, TypeVar, Union
from pydeconz.models.event import EventType
from pydeconz.models.light.lock import Lock
@ -16,7 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzDevice
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(
@ -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."""
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."""
TYPE = DOMAIN

View file

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