Add typing in dynalite and activate mypy (#53238)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
jan iversen 2021-07-20 18:28:31 +02:00 committed by GitHub
parent 9b705ad6df
commit 193d1b945b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 15 deletions

View file

@ -52,6 +52,7 @@ from .const import (
SERVICE_REQUEST_AREA_PRESET,
SERVICE_REQUEST_CHANNEL_LEVEL,
)
from .convert_config import convert_config
def num_string(value: int | str) -> str:
@ -263,7 +264,7 @@ async def async_entry_changed(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a bridge from a config entry."""
LOGGER.debug("Setting up entry %s", entry.data)
bridge = DynaliteBridge(hass, entry.data)
bridge = DynaliteBridge(hass, convert_config(entry.data))
# need to do it before the listener
hass.data[DOMAIN][entry.entry_id] = bridge
entry.async_on_unload(entry.add_update_listener(async_entry_changed))

View file

@ -1,6 +1,7 @@
"""Code to handle a Dynalite bridge."""
from __future__ import annotations
from types import MappingProxyType
from typing import Any, Callable
from dynalite_devices_lib.dynalite_devices import (
@ -27,9 +28,8 @@ class DynaliteBridge:
def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Initialize the system based on host parameter."""
self.hass = hass
self.area = {}
self.async_add_devices = {}
self.waiting_devices = {}
self.async_add_devices: dict[str, Callable] = {}
self.waiting_devices: dict[str, list[str]] = {}
self.host = config[CONF_HOST]
# Configure the dynalite devices
self.dynalite_devices = DynaliteDevices(
@ -37,7 +37,7 @@ class DynaliteBridge:
update_device_func=self.update_device,
notification_func=self.handle_notification,
)
self.dynalite_devices.configure(convert_config(config))
self.dynalite_devices.configure(config)
async def async_setup(self) -> bool:
"""Set up a Dynalite bridge."""
@ -45,7 +45,7 @@ class DynaliteBridge:
LOGGER.debug("Setting up bridge - host %s", self.host)
return await self.dynalite_devices.async_setup()
def reload_config(self, config: dict[str, Any]) -> None:
def reload_config(self, config: MappingProxyType[str, Any]) -> None:
"""Reconfigure a bridge when config changes."""
LOGGER.debug("Reloading bridge - host %s, config %s", self.host, config)
self.dynalite_devices.configure(convert_config(config))

View file

@ -8,6 +8,7 @@ from homeassistant.const import CONF_HOST
from .bridge import DynaliteBridge
from .const import DOMAIN, LOGGER
from .convert_config import convert_config
class DynaliteFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
@ -25,11 +26,13 @@ class DynaliteFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
host = import_info[CONF_HOST]
for entry in self._async_current_entries():
if entry.data[CONF_HOST] == host:
if entry.data != import_info:
self.hass.config_entries.async_update_entry(entry, data=import_info)
self.hass.config_entries.async_update_entry(
entry, data=dict(import_info)
)
return self.async_abort(reason="already_configured")
# New entry
bridge = DynaliteBridge(self.hass, import_info)
bridge = DynaliteBridge(self.hass, convert_config(import_info))
if not await bridge.async_setup():
LOGGER.error("Unable to setup bridge - import info=%s", import_info)
return self.async_abort(reason="no_connection")

View file

@ -1,6 +1,7 @@
"""Convert the HA config to the dynalite config."""
from __future__ import annotations
from types import MappingProxyType
from typing import Any
from dynalite_devices_lib import const as dyn_const
@ -136,7 +137,9 @@ def convert_template(config: dict[str, Any]) -> dict[str, Any]:
return convert_with_map(config, my_map)
def convert_config(config: dict[str, Any]) -> dict[str, Any]:
def convert_config(
config: dict[str, Any] | MappingProxyType[str, Any]
) -> dict[str, Any]:
"""Convert a config dict by replacing component consts with library consts."""
my_map = {
CONF_NAME: dyn_const.CONF_NAME,

View file

@ -43,7 +43,7 @@ class DynaliteBase(Entity):
"""Initialize the base class."""
self._device = device
self._bridge = bridge
self._unsub_dispatchers = []
self._unsub_dispatchers: list[Callable[[], None]] = []
@property
def name(self) -> str:

View file

@ -1139,9 +1139,6 @@ ignore_errors = true
[mypy-homeassistant.components.doorbird.*]
ignore_errors = true
[mypy-homeassistant.components.dynalite.*]
ignore_errors = true
[mypy-homeassistant.components.edl21.*]
ignore_errors = true

View file

@ -38,7 +38,6 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.dhcp.*",
"homeassistant.components.directv.*",
"homeassistant.components.doorbird.*",
"homeassistant.components.dynalite.*",
"homeassistant.components.edl21.*",
"homeassistant.components.elkm1.*",
"homeassistant.components.emonitor.*",