From fbe507a9c1a8479d3abdffc2283017e88e2cc230 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Sun, 13 Jun 2021 16:45:35 +0200 Subject: [PATCH] Strict types - first part (#51479) --- homeassistant/components/fritz/__init__.py | 6 +-- .../components/fritz/binary_sensor.py | 9 ++-- .../components/fritz/device_tracker.py | 54 +++++++++++-------- homeassistant/components/fritz/services.py | 10 ++-- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/fritz/__init__.py b/homeassistant/components/fritz/__init__.py index 35e924c807c..6d0030685b2 100644 --- a/homeassistant/components/fritz/__init__.py +++ b/homeassistant/components/fritz/__init__.py @@ -11,7 +11,7 @@ from homeassistant.const import ( CONF_USERNAME, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import Event, HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from .common import FritzBoxTools, FritzData @@ -47,7 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DATA_FRITZ] = FritzData() @callback - def _async_unload(event): + def _async_unload(event: Event) -> None: fritz_tools.async_unload() entry.async_on_unload( @@ -83,7 +83,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return unload_ok -async def update_listener(hass: HomeAssistant, entry: ConfigEntry): +async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Update when config_entry options update.""" if entry.options: await hass.config_entries.async_reload(entry.entry_id) diff --git a/homeassistant/components/fritz/binary_sensor.py b/homeassistant/components/fritz/binary_sensor.py index bc8fa204ee5..2154a397584 100644 --- a/homeassistant/components/fritz/binary_sensor.py +++ b/homeassistant/components/fritz/binary_sensor.py @@ -9,6 +9,7 @@ from homeassistant.components.binary_sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback from .common import FritzBoxBaseEntity, FritzBoxTools from .const import DOMAIN @@ -17,7 +18,7 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up entry.""" _LOGGER.debug("Setting up FRITZ!Box binary sensors") @@ -44,12 +45,12 @@ class FritzBoxConnectivitySensor(FritzBoxBaseEntity, BinarySensorEntity): super().__init__(fritzbox_tools, device_friendly_name) @property - def name(self): + def name(self) -> str: """Return name.""" return self._name @property - def device_class(self): + def device_class(self) -> str: """Return device class.""" return DEVICE_CLASS_CONNECTIVITY @@ -59,7 +60,7 @@ class FritzBoxConnectivitySensor(FritzBoxBaseEntity, BinarySensorEntity): return self._is_on @property - def unique_id(self): + def unique_id(self) -> str: """Return unique id.""" return self._unique_id diff --git a/homeassistant/components/fritz/device_tracker.py b/homeassistant/components/fritz/device_tracker.py index dbf6bc0df93..0e75a781c5d 100644 --- a/homeassistant/components/fritz/device_tracker.py +++ b/homeassistant/components/fritz/device_tracker.py @@ -17,9 +17,11 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import DeviceInfo +from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType -from .common import FritzBoxTools, FritzDevice +from .common import Device, FritzBoxTools, FritzData, FritzDevice from .const import DATA_FRITZ, DEFAULT_DEVICE_NAME, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -41,7 +43,7 @@ PLATFORM_SCHEMA = vol.All( ) -async def async_get_scanner(hass: HomeAssistant, config: ConfigType): +async def async_get_scanner(hass: HomeAssistant, config: ConfigType) -> None: """Import legacy FRITZ!Box configuration.""" _LOGGER.debug("Import legacy FRITZ!Box configuration from YAML") @@ -63,15 +65,15 @@ async def async_get_scanner(hass: HomeAssistant, config: ConfigType): async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up device tracker for FRITZ!Box component.""" _LOGGER.debug("Starting FRITZ!Box device tracker") - router = hass.data[DOMAIN][entry.entry_id] - data_fritz = hass.data[DATA_FRITZ] + router: FritzBoxTools = hass.data[DOMAIN][entry.entry_id] + data_fritz: FritzData = hass.data[DATA_FRITZ] @callback - def update_router(): + def update_router() -> None: """Update the values of the router.""" _async_add_entities(router, async_add_entities, data_fritz) @@ -83,10 +85,14 @@ async def async_setup_entry( @callback -def _async_add_entities(router, async_add_entities, data_fritz): +def _async_add_entities( + router: FritzBoxTools, + async_add_entities: AddEntitiesCallback, + data_fritz: FritzData, +) -> None: """Add new tracker entities from the router.""" - def _is_tracked(mac, device): + def _is_tracked(mac: str, device: Device) -> bool: for tracked in data_fritz.tracked.values(): if mac in tracked: return True @@ -118,27 +124,28 @@ class FritzBoxTracker(ScannerEntity): self._name = device.hostname or DEFAULT_DEVICE_NAME self._last_activity = device.last_activity self._active = False - self._attrs: dict = {} @property - def is_connected(self): + def is_connected(self) -> bool: """Return device status.""" return self._active @property - def name(self): + def name(self) -> str: """Return device name.""" return self._name @property - def unique_id(self): + def unique_id(self) -> str: """Return device unique id.""" return self._mac @property - def ip_address(self) -> str: + def ip_address(self) -> str | None: """Return the primary ip address of the device.""" - return self._router.devices[self._mac].ip_address + if self._mac: + return self._router.devices[self._mac].ip_address + return None @property def mac_address(self) -> str: @@ -146,9 +153,11 @@ class FritzBoxTracker(ScannerEntity): return self._mac @property - def hostname(self) -> str: + def hostname(self) -> str | None: """Return hostname of the device.""" - return self._router.devices[self._mac].hostname + if self._mac: + return self._router.devices[self._mac].hostname + return None @property def source_type(self) -> str: @@ -156,7 +165,7 @@ class FritzBoxTracker(ScannerEntity): return SOURCE_TYPE_ROUTER @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return the device information.""" return { "connections": {(CONNECTION_NETWORK_MAC, self._mac)}, @@ -176,7 +185,7 @@ class FritzBoxTracker(ScannerEntity): return False @property - def icon(self): + def icon(self) -> str: """Return device icon.""" if self.is_connected: return "mdi:lan-connect" @@ -200,17 +209,20 @@ class FritzBoxTracker(ScannerEntity): @callback def async_process_update(self) -> None: """Update device.""" - device: FritzDevice = self._router.devices[self._mac] + if not self._mac: + return + + device = self._router.devices[self._mac] self._active = device.is_connected self._last_activity = device.last_activity @callback - def async_on_demand_update(self): + def async_on_demand_update(self) -> None: """Update state.""" self.async_process_update() self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register state update callback.""" self.async_process_update() self.async_on_remove( diff --git a/homeassistant/components/fritz/services.py b/homeassistant/components/fritz/services.py index 7ed5ecd3c40..fcfbd54b743 100644 --- a/homeassistant/components/fritz/services.py +++ b/homeassistant/components/fritz/services.py @@ -10,14 +10,14 @@ from .const import DOMAIN, FRITZ_SERVICES, SERVICE_REBOOT, SERVICE_RECONNECT _LOGGER = logging.getLogger(__name__) -async def async_setup_services(hass: HomeAssistant): +async def async_setup_services(hass: HomeAssistant) -> None: """Set up services for Fritz integration.""" for service in [SERVICE_REBOOT, SERVICE_RECONNECT]: if hass.services.has_service(DOMAIN, service): return - async def async_call_fritz_service(service_call): + async def async_call_fritz_service(service_call: ServiceCall) -> None: """Call correct Fritz service.""" if not ( @@ -40,10 +40,10 @@ async def async_setup_services(hass: HomeAssistant): async def _async_get_configured_fritz_tools( hass: HomeAssistant, service_call: ServiceCall -): +) -> list: """Get FritzBoxTools class from config entry.""" - list_entry_id = [] + list_entry_id: list = [] for entry_id in await async_extract_config_entry_ids(hass, service_call): config_entry = hass.config_entries.async_get_entry(entry_id) if config_entry and config_entry.domain == DOMAIN: @@ -51,7 +51,7 @@ async def _async_get_configured_fritz_tools( return list_entry_id -async def async_unload_services(hass: HomeAssistant): +async def async_unload_services(hass: HomeAssistant) -> None: """Unload services for Fritz integration.""" if not hass.data.get(FRITZ_SERVICES):