From 8bed2e6459bfc1efb25d6a55aaea2eb1b9953cf9 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 28 Jun 2022 13:53:20 +0200 Subject: [PATCH] Remove zha from mypy ignore list (#73603) --- .../components/zha/core/channels/__init__.py | 4 +- .../components/zha/core/discovery.py | 8 +-- .../components/zha/core/registries.py | 61 ++++++++++--------- mypy.ini | 6 -- script/hassfest/mypy_config.py | 2 - 5 files changed, 38 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/zha/core/channels/__init__.py b/homeassistant/components/zha/core/channels/__init__.py index 2da7462f3eb66..9042856b45645 100644 --- a/homeassistant/components/zha/core/channels/__init__.py +++ b/homeassistant/components/zha/core/channels/__init__.py @@ -221,7 +221,7 @@ def is_mains_powered(self) -> bool | None: return self._channels.zha_device.is_mains_powered @property - def manufacturer(self) -> str | None: + def manufacturer(self) -> str: """Return device manufacturer.""" return self._channels.zha_device.manufacturer @@ -236,7 +236,7 @@ def hass(self) -> HomeAssistant: return self._channels.zha_device.hass @property - def model(self) -> str | None: + def model(self) -> str: """Return device model.""" return self._channels.zha_device.model diff --git a/homeassistant/components/zha/core/discovery.py b/homeassistant/components/zha/core/discovery.py index cdad57834eb43..6b690f4da085a 100644 --- a/homeassistant/components/zha/core/discovery.py +++ b/homeassistant/components/zha/core/discovery.py @@ -6,7 +6,7 @@ import logging from typing import TYPE_CHECKING -from homeassistant import const as ha_const +from homeassistant.const import CONF_TYPE, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, @@ -86,9 +86,7 @@ def discover_by_device_type(self, channel_pool: ChannelPool) -> None: unique_id = channel_pool.unique_id - component: str | None = self._device_configs.get(unique_id, {}).get( - ha_const.CONF_TYPE - ) + component: str | None = self._device_configs.get(unique_id, {}).get(CONF_TYPE) if component is None: ep_profile_id = channel_pool.endpoint.profile_id ep_device_type = channel_pool.endpoint.device_type @@ -136,7 +134,7 @@ def discover_by_cluster_id(self, channel_pool: ChannelPool) -> None: @staticmethod def probe_single_cluster( - component: str, + component: Platform | None, channel: base.ZigbeeChannel, ep_channels: ChannelPool, ) -> None: diff --git a/homeassistant/components/zha/core/registries.py b/homeassistant/components/zha/core/registries.py index 7e2114b5911b0..d271f2ecba362 100644 --- a/homeassistant/components/zha/core/registries.py +++ b/homeassistant/components/zha/core/registries.py @@ -110,7 +110,7 @@ ZIGBEE_CHANNEL_REGISTRY: DictRegistry[type[ZigbeeChannel]] = DictRegistry() -def set_or_callable(value): +def set_or_callable(value) -> frozenset[str] | Callable: """Convert single str or None to a set. Pass through callables and sets.""" if value is None: return frozenset() @@ -121,22 +121,26 @@ def set_or_callable(value): return frozenset([str(value)]) +def _get_empty_frozenset() -> frozenset[str]: + return frozenset() + + @attr.s(frozen=True) class MatchRule: """Match a ZHA Entity to a channel name or generic id.""" - channel_names: set[str] | str = attr.ib( + channel_names: frozenset[str] = attr.ib( factory=frozenset, converter=set_or_callable ) - generic_ids: set[str] | str = attr.ib(factory=frozenset, converter=set_or_callable) - manufacturers: Callable | set[str] | str = attr.ib( - factory=frozenset, converter=set_or_callable + generic_ids: frozenset[str] = attr.ib(factory=frozenset, converter=set_or_callable) + manufacturers: frozenset[str] | Callable = attr.ib( + factory=_get_empty_frozenset, converter=set_or_callable ) - models: Callable | set[str] | str = attr.ib( - factory=frozenset, converter=set_or_callable + models: frozenset[str] | Callable = attr.ib( + factory=_get_empty_frozenset, converter=set_or_callable ) - aux_channels: Callable | set[str] | str = attr.ib( - factory=frozenset, converter=set_or_callable + aux_channels: frozenset[str] | Callable = attr.ib( + factory=_get_empty_frozenset, converter=set_or_callable ) @property @@ -162,7 +166,8 @@ def weight(self) -> int: weight += 10 * len(self.channel_names) weight += 5 * len(self.generic_ids) - weight += 1 * len(self.aux_channels) + if isinstance(self.aux_channels, frozenset): + weight += 1 * len(self.aux_channels) return weight def claim_channels(self, channel_pool: list[ZigbeeChannel]) -> list[ZigbeeChannel]: @@ -321,11 +326,11 @@ def get_group_entity(self, component: str) -> type[ZhaGroupEntity] | None: def strict_match( self, component: str, - channel_names: set[str] | str = None, - generic_ids: set[str] | str = None, - manufacturers: Callable | set[str] | str = None, - models: Callable | set[str] | str = None, - aux_channels: Callable | set[str] | str = None, + channel_names: set[str] | str | None = None, + generic_ids: set[str] | str | None = None, + manufacturers: Callable | set[str] | str | None = None, + models: Callable | set[str] | str | None = None, + aux_channels: Callable | set[str] | str | None = None, ) -> Callable[[_ZhaEntityT], _ZhaEntityT]: """Decorate a strict match rule.""" @@ -346,11 +351,11 @@ def decorator(zha_ent: _ZhaEntityT) -> _ZhaEntityT: def multipass_match( self, component: str, - channel_names: set[str] | str = None, - generic_ids: set[str] | str = None, - manufacturers: Callable | set[str] | str = None, - models: Callable | set[str] | str = None, - aux_channels: Callable | set[str] | str = None, + channel_names: set[str] | str | None = None, + generic_ids: set[str] | str | None = None, + manufacturers: Callable | set[str] | str | None = None, + models: Callable | set[str] | str | None = None, + aux_channels: Callable | set[str] | str | None = None, stop_on_match_group: int | str | None = None, ) -> Callable[[_ZhaEntityT], _ZhaEntityT]: """Decorate a loose match rule.""" @@ -379,11 +384,11 @@ def decorator(zha_entity: _ZhaEntityT) -> _ZhaEntityT: def config_diagnostic_match( self, component: str, - channel_names: set[str] | str = None, - generic_ids: set[str] | str = None, - manufacturers: Callable | set[str] | str = None, - models: Callable | set[str] | str = None, - aux_channels: Callable | set[str] | str = None, + channel_names: set[str] | str | None = None, + generic_ids: set[str] | str | None = None, + manufacturers: Callable | set[str] | str | None = None, + models: Callable | set[str] | str | None = None, + aux_channels: Callable | set[str] | str | None = None, stop_on_match_group: int | str | None = None, ) -> Callable[[_ZhaEntityT], _ZhaEntityT]: """Decorate a loose match rule.""" @@ -432,9 +437,9 @@ def prevent_entity_creation(self, platform: Platform, ieee: EUI64, key: str): def clean_up(self) -> None: """Clean up post discovery.""" - self.single_device_matches: dict[ - Platform, dict[EUI64, list[str]] - ] = collections.defaultdict(lambda: collections.defaultdict(list)) + self.single_device_matches = collections.defaultdict( + lambda: collections.defaultdict(list) + ) ZHA_ENTITIES = ZHAEntityRegistry() diff --git a/mypy.ini b/mypy.ini index 0f8c8fc0b61fc..fb67983a31c3d 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2995,9 +2995,3 @@ ignore_errors = true [mypy-homeassistant.components.xiaomi_miio.switch] ignore_errors = true - -[mypy-homeassistant.components.zha.core.discovery] -ignore_errors = true - -[mypy-homeassistant.components.zha.core.registries] -ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 0fd62d49ae2a6..bbb628a76bbe6 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -144,8 +144,6 @@ "homeassistant.components.xiaomi_miio.light", "homeassistant.components.xiaomi_miio.sensor", "homeassistant.components.xiaomi_miio.switch", - "homeassistant.components.zha.core.discovery", - "homeassistant.components.zha.core.registries", ] # Component modules which should set no_implicit_reexport = true.