Add typing to deconz_device (#67501)

This commit is contained in:
Robert Svensson 2022-03-12 05:35:25 +01:00 committed by GitHub
parent c8351387d7
commit 6526b4eae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 13 deletions

View File

@ -1,7 +1,10 @@
"""Base class for deCONZ devices."""
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.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 .const import DOMAIN as DECONZ_DOMAIN
from .gateway import DeconzGateway
class DeconzBase:
"""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."""
self._device = device
self.gateway = gateway
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return a unique identifier for this device."""
return self._device.unique_id
@property
def serial(self):
def serial(self) -> str | None:
"""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 self._device.unique_id.split("-", 1)[0]
@ -56,14 +64,18 @@ class DeconzDevice(DeconzBase, Entity):
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."""
super().__init__(device, gateway)
self.gateway.entities[self.TYPE].add(self.unique_id)
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."""
self._device.register_callback(self.async_update_callback)
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)
@callback
def async_update_connection_state(self):
def async_update_connection_state(self) -> None:
"""Update the device's available state."""
self.async_write_ha_state()
@callback
def async_update_callback(self):
def async_update_callback(self) -> None:
"""Update the device's state."""
if self.gateway.ignore_state_updates:
return
@ -95,7 +107,7 @@ class DeconzDevice(DeconzBase, Entity):
self.async_write_ha_state()
@property
def available(self):
def available(self) -> bool:
"""Return True if device is available."""
return self.gateway.available and self._device.reachable
@ -105,7 +117,7 @@ class DeconzSceneMixin(DeconzDevice):
_device: PydeconzScene
def __init__(self, device, gateway) -> None:
def __init__(self, device: PydeconzScene, gateway: DeconzGateway) -> None:
"""Set up a scene."""
super().__init__(device, gateway)

View File

@ -4,7 +4,7 @@ from __future__ import annotations
import asyncio
from types import MappingProxyType
from typing import Any, cast
from typing import TYPE_CHECKING, Any, cast
import async_timeout
from pydeconz import DeconzSession, errors, group, light, sensor
@ -37,9 +37,11 @@ from .const import (
LOGGER,
PLATFORMS,
)
from .deconz_event import DeconzAlarmEvent, DeconzEvent
from .errors import AuthenticationRequired, CannotConnect
if TYPE_CHECKING:
from .deconz_event import DeconzAlarmEvent, DeconzEvent
class DeconzGateway:
"""Manages a single deCONZ gateway."""