Fix mypy issues in zha core modules (#74028)

* Fix mypy issues in zha gateway, group and helpers

* Cleanup device

* Apply suggestion

* Raise ValueError

* Use hass.config.path
This commit is contained in:
epenet 2022-06-28 08:50:16 +02:00 committed by GitHub
parent 720768560d
commit fb10853358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 25 deletions

View File

@ -276,7 +276,7 @@ class ZHADevice(LogMixin):
@property @property
def skip_configuration(self) -> bool: def skip_configuration(self) -> bool:
"""Return true if the device should not issue configuration related commands.""" """Return true if the device should not issue configuration related commands."""
return self._zigpy_device.skip_configuration or self.is_coordinator return self._zigpy_device.skip_configuration or bool(self.is_coordinator)
@property @property
def gateway(self): def gateway(self):
@ -819,7 +819,7 @@ class ZHADevice(LogMixin):
fmt = f"{log_msg[1]} completed: %s" fmt = f"{log_msg[1]} completed: %s"
zdo.debug(fmt, *(log_msg[2] + (outcome,))) zdo.debug(fmt, *(log_msg[2] + (outcome,)))
def log(self, level: int, msg: str, *args: Any, **kwargs: dict) -> None: def log(self, level: int, msg: str, *args: Any, **kwargs: Any) -> None:
"""Log a message.""" """Log a message."""
msg = f"[%s](%s): {msg}" msg = f"[%s](%s): {msg}"
args = (self.nwk, self.model) + args args = (self.nwk, self.model) + args

View File

@ -8,7 +8,6 @@ from datetime import timedelta
from enum import Enum from enum import Enum
import itertools import itertools
import logging import logging
import os
import time import time
import traceback import traceback
from typing import TYPE_CHECKING, Any, NamedTuple, Union from typing import TYPE_CHECKING, Any, NamedTuple, Union
@ -163,7 +162,7 @@ class ZHAGateway:
app_config = self._config.get(CONF_ZIGPY, {}) app_config = self._config.get(CONF_ZIGPY, {})
database = self._config.get( database = self._config.get(
CONF_DATABASE, CONF_DATABASE,
os.path.join(self._hass.config.config_dir, DEFAULT_DATABASE_NAME), self._hass.config.path(DEFAULT_DATABASE_NAME),
) )
app_config[CONF_DATABASE] = database app_config[CONF_DATABASE] = database
app_config[CONF_DEVICE] = self.config_entry.data[CONF_DEVICE] app_config[CONF_DEVICE] = self.config_entry.data[CONF_DEVICE]
@ -333,7 +332,7 @@ class ZHAGateway:
def group_removed(self, zigpy_group: zigpy.group.Group) -> None: def group_removed(self, zigpy_group: zigpy.group.Group) -> None:
"""Handle zigpy group removed event.""" """Handle zigpy group removed event."""
self._send_group_gateway_message(zigpy_group, ZHA_GW_MSG_GROUP_REMOVED) self._send_group_gateway_message(zigpy_group, ZHA_GW_MSG_GROUP_REMOVED)
zha_group = self._groups.pop(zigpy_group.group_id, None) zha_group = self._groups.pop(zigpy_group.group_id)
zha_group.info("group_removed") zha_group.info("group_removed")
self._cleanup_group_entity_registry_entries(zigpy_group) self._cleanup_group_entity_registry_entries(zigpy_group)
@ -428,6 +427,7 @@ class ZHAGateway:
] ]
# then we get all group entity entries tied to the coordinator # then we get all group entity entries tied to the coordinator
assert self.coordinator_zha_device
all_group_entity_entries = er.async_entries_for_device( all_group_entity_entries = er.async_entries_for_device(
self.ha_entity_registry, self.ha_entity_registry,
self.coordinator_zha_device.device_id, self.coordinator_zha_device.device_id,

View File

@ -78,7 +78,7 @@ class ZHAGroupMember(LogMixin):
return member_info return member_info
@property @property
def associated_entities(self) -> list[GroupEntityReference]: def associated_entities(self) -> list[dict[str, Any]]:
"""Return the list of entities that were derived from this endpoint.""" """Return the list of entities that were derived from this endpoint."""
ha_entity_registry = self.device.gateway.ha_entity_registry ha_entity_registry = self.device.gateway.ha_entity_registry
zha_device_registry = self.device.gateway.device_registry zha_device_registry = self.device.gateway.device_registry
@ -150,9 +150,7 @@ class ZHAGroup(LogMixin):
def members(self) -> list[ZHAGroupMember]: def members(self) -> list[ZHAGroupMember]:
"""Return the ZHA devices that are members of this group.""" """Return the ZHA devices that are members of this group."""
return [ return [
ZHAGroupMember( ZHAGroupMember(self, self._zha_gateway.devices[member_ieee], endpoint_id)
self, self._zha_gateway.devices.get(member_ieee), endpoint_id
)
for (member_ieee, endpoint_id) in self._zigpy_group.members.keys() for (member_ieee, endpoint_id) in self._zigpy_group.members.keys()
if member_ieee in self._zha_gateway.devices if member_ieee in self._zha_gateway.devices
] ]

View File

@ -169,6 +169,8 @@ def async_get_zha_device(hass: HomeAssistant, device_id: str) -> ZHADevice:
"""Get a ZHA device for the given device registry id.""" """Get a ZHA device for the given device registry id."""
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
registry_device = device_registry.async_get(device_id) registry_device = device_registry.async_get(device_id)
if not registry_device:
raise ValueError(f"Device id `{device_id}` not found in registry.")
zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY] zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY]
ieee_address = list(list(registry_device.identifiers)[0])[1] ieee_address = list(list(registry_device.identifiers)[0])[1]
ieee = zigpy.types.EUI64.convert(ieee_address) ieee = zigpy.types.EUI64.convert(ieee_address)

View File

@ -2996,21 +2996,9 @@ ignore_errors = true
[mypy-homeassistant.components.xiaomi_miio.switch] [mypy-homeassistant.components.xiaomi_miio.switch]
ignore_errors = true ignore_errors = true
[mypy-homeassistant.components.zha.core.device]
ignore_errors = true
[mypy-homeassistant.components.zha.core.discovery] [mypy-homeassistant.components.zha.core.discovery]
ignore_errors = true ignore_errors = true
[mypy-homeassistant.components.zha.core.gateway]
ignore_errors = true
[mypy-homeassistant.components.zha.core.group]
ignore_errors = true
[mypy-homeassistant.components.zha.core.helpers]
ignore_errors = true
[mypy-homeassistant.components.zha.core.registries] [mypy-homeassistant.components.zha.core.registries]
ignore_errors = true ignore_errors = true

View File

@ -144,11 +144,7 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.xiaomi_miio.light", "homeassistant.components.xiaomi_miio.light",
"homeassistant.components.xiaomi_miio.sensor", "homeassistant.components.xiaomi_miio.sensor",
"homeassistant.components.xiaomi_miio.switch", "homeassistant.components.xiaomi_miio.switch",
"homeassistant.components.zha.core.device",
"homeassistant.components.zha.core.discovery", "homeassistant.components.zha.core.discovery",
"homeassistant.components.zha.core.gateway",
"homeassistant.components.zha.core.group",
"homeassistant.components.zha.core.helpers",
"homeassistant.components.zha.core.registries", "homeassistant.components.zha.core.registries",
"homeassistant.components.zha.core.store", "homeassistant.components.zha.core.store",
] ]