Add zha typing [classmethods] (#75472)

This commit is contained in:
Marc Mueller 2022-07-20 12:03:30 +02:00 committed by GitHub
parent 5ef92e5e95
commit a3b2b5c328
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 45 deletions

View file

@ -4,7 +4,7 @@ from __future__ import annotations
import abc import abc
import functools import functools
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, TypeVar
import zigpy.exceptions import zigpy.exceptions
from zigpy.zcl.foundation import Status from zigpy.zcl.foundation import Status
@ -27,6 +27,8 @@ if TYPE_CHECKING:
from .core.device import ZHADevice from .core.device import ZHADevice
_ZHAIdentifyButtonSelfT = TypeVar("_ZHAIdentifyButtonSelfT", bound="ZHAIdentifyButton")
MULTI_MATCH = functools.partial(ZHA_ENTITIES.multipass_match, Platform.BUTTON) MULTI_MATCH = functools.partial(ZHA_ENTITIES.multipass_match, Platform.BUTTON)
CONFIG_DIAGNOSTIC_MATCH = functools.partial( CONFIG_DIAGNOSTIC_MATCH = functools.partial(
ZHA_ENTITIES.config_diagnostic_match, Platform.BUTTON ZHA_ENTITIES.config_diagnostic_match, Platform.BUTTON
@ -66,7 +68,7 @@ class ZHAButton(ZhaEntity, ButtonEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this button.""" """Init this button."""
super().__init__(unique_id, zha_device, channels, **kwargs) super().__init__(unique_id, zha_device, channels, **kwargs)
@ -89,12 +91,12 @@ class ZHAIdentifyButton(ZHAButton):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ZHAIdentifyButtonSelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ZHAIdentifyButtonSelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -126,7 +128,7 @@ class ZHAAttributeButton(ZhaEntity, ButtonEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this button.""" """Init this button."""
super().__init__(unique_id, zha_device, channels, **kwargs) super().__init__(unique_id, zha_device, channels, **kwargs)

View file

@ -9,7 +9,7 @@ from functools import cached_property
import logging import logging
import random import random
import time import time
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, TypeVar
from zigpy import types from zigpy import types
import zigpy.device import zigpy.device
@ -86,6 +86,8 @@ _LOGGER = logging.getLogger(__name__)
_UPDATE_ALIVE_INTERVAL = (60, 90) _UPDATE_ALIVE_INTERVAL = (60, 90)
_CHECKIN_GRACE_PERIODS = 2 _CHECKIN_GRACE_PERIODS = 2
_ZHADeviceSelfT = TypeVar("_ZHADeviceSelfT", bound="ZHADevice")
class DeviceStatus(Enum): class DeviceStatus(Enum):
"""Status of a device.""" """Status of a device."""
@ -340,12 +342,12 @@ class ZHADevice(LogMixin):
@classmethod @classmethod
def new( def new(
cls, cls: type[_ZHADeviceSelfT],
hass: HomeAssistant, hass: HomeAssistant,
zigpy_dev: zigpy.device.Device, zigpy_dev: zigpy.device.Device,
gateway: ZHAGateway, gateway: ZHAGateway,
restored: bool = False, restored: bool = False,
): ) -> _ZHADeviceSelfT:
"""Create new device.""" """Create new device."""
zha_dev = cls(hass, zigpy_dev, gateway) zha_dev = cls(hass, zigpy_dev, gateway)
zha_dev.channels = channels.Channels.new(zha_dev) zha_dev.channels = channels.Channels.new(zha_dev)

View file

@ -5,7 +5,7 @@ import asyncio
from collections.abc import Callable from collections.abc import Callable
import functools import functools
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, TypeVar
from homeassistant.const import ATTR_NAME from homeassistant.const import ATTR_NAME
from homeassistant.core import CALLBACK_TYPE, Event, callback from homeassistant.core import CALLBACK_TYPE, Event, callback
@ -35,6 +35,9 @@ if TYPE_CHECKING:
from .core.channels.base import ZigbeeChannel from .core.channels.base import ZigbeeChannel
from .core.device import ZHADevice from .core.device import ZHADevice
_ZhaEntitySelfT = TypeVar("_ZhaEntitySelfT", bound="ZhaEntity")
_ZhaGroupEntitySelfT = TypeVar("_ZhaGroupEntitySelfT", bound="ZhaGroupEntity")
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ENTITY_SUFFIX = "entity_suffix" ENTITY_SUFFIX = "entity_suffix"
@ -155,7 +158,7 @@ class BaseZhaEntity(LogMixin, entity.Entity):
class ZhaEntity(BaseZhaEntity, RestoreEntity): class ZhaEntity(BaseZhaEntity, RestoreEntity):
"""A base class for non group ZHA entities.""" """A base class for non group ZHA entities."""
def __init_subclass__(cls, id_suffix: str | None = None, **kwargs) -> None: def __init_subclass__(cls, id_suffix: str | None = None, **kwargs: Any) -> None:
"""Initialize subclass. """Initialize subclass.
:param id_suffix: suffix to add to the unique_id of the entity. Used for multi :param id_suffix: suffix to add to the unique_id of the entity. Used for multi
@ -187,12 +190,12 @@ class ZhaEntity(BaseZhaEntity, RestoreEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ZhaEntitySelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ZhaEntitySelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -257,7 +260,12 @@ class ZhaGroupEntity(BaseZhaEntity):
"""A base class for ZHA group entities.""" """A base class for ZHA group entities."""
def __init__( def __init__(
self, entity_ids: list[str], unique_id: str, group_id: int, zha_device, **kwargs self,
entity_ids: list[str],
unique_id: str,
group_id: int,
zha_device: ZHADevice,
**kwargs: Any,
) -> None: ) -> None:
"""Initialize a light group.""" """Initialize a light group."""
super().__init__(unique_id, zha_device, **kwargs) super().__init__(unique_id, zha_device, **kwargs)
@ -279,8 +287,13 @@ class ZhaGroupEntity(BaseZhaEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, entity_ids: list[str], unique_id: str, group_id: int, zha_device, **kwargs cls: type[_ZhaGroupEntitySelfT],
) -> ZhaGroupEntity | None: entity_ids: list[str],
unique_id: str,
group_id: int,
zha_device: ZHADevice,
**kwargs: Any,
) -> _ZhaGroupEntitySelfT | None:
"""Group Entity Factory. """Group Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import functools import functools
import logging import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any, TypeVar
import zigpy.exceptions import zigpy.exceptions
from zigpy.zcl.foundation import Status from zigpy.zcl.foundation import Status
@ -33,6 +33,10 @@ if TYPE_CHECKING:
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_ZHANumberConfigurationEntitySelfT = TypeVar(
"_ZHANumberConfigurationEntitySelfT", bound="ZHANumberConfigurationEntity"
)
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.NUMBER) STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.NUMBER)
CONFIG_DIAGNOSTIC_MATCH = functools.partial( CONFIG_DIAGNOSTIC_MATCH = functools.partial(
ZHA_ENTITIES.config_diagnostic_match, Platform.NUMBER ZHA_ENTITIES.config_diagnostic_match, Platform.NUMBER
@ -368,12 +372,12 @@ class ZHANumberConfigurationEntity(ZhaEntity, NumberEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ZHANumberConfigurationEntitySelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ZHANumberConfigurationEntitySelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -397,7 +401,7 @@ class ZHANumberConfigurationEntity(ZhaEntity, NumberEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this number configuration entity.""" """Init this number configuration entity."""
self._channel: ZigbeeChannel = channels[0] self._channel: ZigbeeChannel = channels[0]

View file

@ -4,7 +4,7 @@ from __future__ import annotations
from enum import Enum from enum import Enum
import functools import functools
import logging import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any, TypeVar
from zigpy import types from zigpy import types
from zigpy.zcl.clusters.general import OnOff from zigpy.zcl.clusters.general import OnOff
@ -34,6 +34,10 @@ if TYPE_CHECKING:
from .core.device import ZHADevice from .core.device import ZHADevice
_ZCLEnumSelectEntitySelfT = TypeVar(
"_ZCLEnumSelectEntitySelfT", bound="ZCLEnumSelectEntity"
)
CONFIG_DIAGNOSTIC_MATCH = functools.partial( CONFIG_DIAGNOSTIC_MATCH = functools.partial(
ZHA_ENTITIES.config_diagnostic_match, Platform.SELECT ZHA_ENTITIES.config_diagnostic_match, Platform.SELECT
) )
@ -72,7 +76,7 @@ class ZHAEnumSelectEntity(ZhaEntity, SelectEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this select entity.""" """Init this select entity."""
self._attr_name = self._enum.__name__ self._attr_name = self._enum.__name__
@ -154,12 +158,12 @@ class ZCLEnumSelectEntity(ZhaEntity, SelectEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ZCLEnumSelectEntitySelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ZCLEnumSelectEntitySelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -183,7 +187,7 @@ class ZCLEnumSelectEntity(ZhaEntity, SelectEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this select entity.""" """Init this select entity."""
self._attr_options = [entry.name.replace("_", " ") for entry in self._enum] self._attr_options = [entry.name.replace("_", " ") for entry in self._enum]

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import functools import functools
import numbers import numbers
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, TypeVar
from homeassistant.components.climate.const import HVACAction from homeassistant.components.climate.const import HVACAction
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -69,6 +69,13 @@ if TYPE_CHECKING:
from .core.channels.base import ZigbeeChannel from .core.channels.base import ZigbeeChannel
from .core.device import ZHADevice from .core.device import ZHADevice
_SensorSelfT = TypeVar("_SensorSelfT", bound="Sensor")
_BatterySelfT = TypeVar("_BatterySelfT", bound="Battery")
_ThermostatHVACActionSelfT = TypeVar(
"_ThermostatHVACActionSelfT", bound="ThermostatHVACAction"
)
_RSSISensorSelfT = TypeVar("_RSSISensorSelfT", bound="RSSISensor")
PARALLEL_UPDATES = 5 PARALLEL_UPDATES = 5
BATTERY_SIZES = { BATTERY_SIZES = {
@ -126,7 +133,7 @@ class Sensor(ZhaEntity, SensorEntity):
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> None: ) -> None:
"""Init this sensor.""" """Init this sensor."""
super().__init__(unique_id, zha_device, channels, **kwargs) super().__init__(unique_id, zha_device, channels, **kwargs)
@ -134,12 +141,12 @@ class Sensor(ZhaEntity, SensorEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_SensorSelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _SensorSelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -214,12 +221,12 @@ class Battery(Sensor):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_BatterySelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _BatterySelfT | None:
"""Entity Factory. """Entity Factory.
Unlike any other entity, PowerConfiguration cluster may not support Unlike any other entity, PowerConfiguration cluster may not support
@ -641,12 +648,12 @@ class ThermostatHVACAction(Sensor, id_suffix="hvac_action"):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ThermostatHVACActionSelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ThermostatHVACActionSelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None
@ -767,12 +774,12 @@ class RSSISensor(Sensor, id_suffix="rssi"):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_RSSISensorSelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs, **kwargs: Any,
) -> ZhaEntity | None: ) -> _RSSISensorSelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import functools import functools
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, TypeVar
import zigpy.exceptions import zigpy.exceptions
from zigpy.zcl.clusters.general import OnOff from zigpy.zcl.clusters.general import OnOff
@ -31,6 +31,10 @@ if TYPE_CHECKING:
from .core.channels.base import ZigbeeChannel from .core.channels.base import ZigbeeChannel
from .core.device import ZHADevice from .core.device import ZHADevice
_ZHASwitchConfigurationEntitySelfT = TypeVar(
"_ZHASwitchConfigurationEntitySelfT", bound="ZHASwitchConfigurationEntity"
)
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.SWITCH) STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.SWITCH)
GROUP_MATCH = functools.partial(ZHA_ENTITIES.group_match, Platform.SWITCH) GROUP_MATCH = functools.partial(ZHA_ENTITIES.group_match, Platform.SWITCH)
CONFIG_DIAGNOSTIC_MATCH = functools.partial( CONFIG_DIAGNOSTIC_MATCH = functools.partial(
@ -172,12 +176,12 @@ class ZHASwitchConfigurationEntity(ZhaEntity, SwitchEntity):
@classmethod @classmethod
def create_entity( def create_entity(
cls, cls: type[_ZHASwitchConfigurationEntitySelfT],
unique_id: str, unique_id: str,
zha_device: ZHADevice, zha_device: ZHADevice,
channels: list[ZigbeeChannel], channels: list[ZigbeeChannel],
**kwargs: Any, **kwargs: Any,
) -> ZhaEntity | None: ) -> _ZHASwitchConfigurationEntitySelfT | None:
"""Entity Factory. """Entity Factory.
Return entity if it is a supported configuration, otherwise return None Return entity if it is a supported configuration, otherwise return None