mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Add typing to deconz_device (#67501)
This commit is contained in:
parent
c8351387d7
commit
6526b4eae5
@ -1,7 +1,10 @@
|
|||||||
"""Base class for deCONZ devices."""
|
"""Base class for deCONZ devices."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pydeconz.group import Scene as PydeconzScene
|
from pydeconz.group import Group as DeconzGroup, Scene as PydeconzScene
|
||||||
|
from pydeconz.light import DeconzLight
|
||||||
|
from pydeconz.sensor import DeconzSensor
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
||||||
@ -9,25 +12,30 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
|
|
||||||
from .const import DOMAIN as DECONZ_DOMAIN
|
from .const import DOMAIN as DECONZ_DOMAIN
|
||||||
|
from .gateway import DeconzGateway
|
||||||
|
|
||||||
|
|
||||||
class DeconzBase:
|
class DeconzBase:
|
||||||
"""Common base for deconz entities and events."""
|
"""Common base for deconz entities and events."""
|
||||||
|
|
||||||
def __init__(self, device, gateway):
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: DeconzGroup | DeconzLight | DeconzSensor,
|
||||||
|
gateway: DeconzGateway,
|
||||||
|
) -> 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."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self.gateway = gateway
|
self.gateway = gateway
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return a unique identifier for this device."""
|
"""Return a unique identifier for this device."""
|
||||||
return self._device.unique_id
|
return self._device.unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def serial(self):
|
def serial(self) -> str | None:
|
||||||
"""Return a serial number for this device."""
|
"""Return a serial number for this device."""
|
||||||
if self._device.unique_id is None 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]
|
||||||
@ -56,14 +64,18 @@ class DeconzDevice(DeconzBase, Entity):
|
|||||||
|
|
||||||
TYPE = ""
|
TYPE = ""
|
||||||
|
|
||||||
def __init__(self, device, gateway):
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: DeconzGroup | DeconzLight | DeconzSensor,
|
||||||
|
gateway: DeconzGateway,
|
||||||
|
) -> 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)
|
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
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to device events."""
|
"""Subscribe to device events."""
|
||||||
self._device.register_callback(self.async_update_callback)
|
self._device.register_callback(self.async_update_callback)
|
||||||
self.gateway.deconz_ids[self.entity_id] = self._device.deconz_id
|
self.gateway.deconz_ids[self.entity_id] = self._device.deconz_id
|
||||||
@ -82,12 +94,12 @@ class DeconzDevice(DeconzBase, Entity):
|
|||||||
self.gateway.entities[self.TYPE].remove(self.unique_id)
|
self.gateway.entities[self.TYPE].remove(self.unique_id)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_connection_state(self):
|
def async_update_connection_state(self) -> None:
|
||||||
"""Update the device's available state."""
|
"""Update the device's available state."""
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_callback(self):
|
def async_update_callback(self) -> None:
|
||||||
"""Update the device's state."""
|
"""Update the device's state."""
|
||||||
if self.gateway.ignore_state_updates:
|
if self.gateway.ignore_state_updates:
|
||||||
return
|
return
|
||||||
@ -95,7 +107,7 @@ class DeconzDevice(DeconzBase, Entity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self) -> bool:
|
||||||
"""Return True if device is available."""
|
"""Return True if device is available."""
|
||||||
return self.gateway.available and self._device.reachable
|
return self.gateway.available and self._device.reachable
|
||||||
|
|
||||||
@ -105,7 +117,7 @@ class DeconzSceneMixin(DeconzDevice):
|
|||||||
|
|
||||||
_device: PydeconzScene
|
_device: PydeconzScene
|
||||||
|
|
||||||
def __init__(self, device, gateway) -> None:
|
def __init__(self, device: PydeconzScene, gateway: DeconzGateway) -> None:
|
||||||
"""Set up a scene."""
|
"""Set up a scene."""
|
||||||
super().__init__(device, gateway)
|
super().__init__(device, gateway)
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any, cast
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
import async_timeout
|
import async_timeout
|
||||||
from pydeconz import DeconzSession, errors, group, light, sensor
|
from pydeconz import DeconzSession, errors, group, light, sensor
|
||||||
@ -37,9 +37,11 @@ from .const import (
|
|||||||
LOGGER,
|
LOGGER,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
)
|
)
|
||||||
from .deconz_event import DeconzAlarmEvent, DeconzEvent
|
|
||||||
from .errors import AuthenticationRequired, CannotConnect
|
from .errors import AuthenticationRequired, CannotConnect
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .deconz_event import DeconzAlarmEvent, DeconzEvent
|
||||||
|
|
||||||
|
|
||||||
class DeconzGateway:
|
class DeconzGateway:
|
||||||
"""Manages a single deCONZ gateway."""
|
"""Manages a single deCONZ gateway."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user