Strict types - first part (#51479)

This commit is contained in:
Simone Chemelli 2021-06-13 16:45:35 +02:00 committed by GitHub
parent a31e6716d9
commit fbe507a9c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 33 deletions

View File

@ -11,7 +11,7 @@ from homeassistant.const import (
CONF_USERNAME, CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from .common import FritzBoxTools, FritzData from .common import FritzBoxTools, FritzData
@ -47,7 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DATA_FRITZ] = FritzData() hass.data[DATA_FRITZ] = FritzData()
@callback @callback
def _async_unload(event): def _async_unload(event: Event) -> None:
fritz_tools.async_unload() fritz_tools.async_unload()
entry.async_on_unload( entry.async_on_unload(
@ -83,7 +83,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok 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.""" """Update when config_entry options update."""
if entry.options: if entry.options:
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)

View File

@ -9,6 +9,7 @@ from homeassistant.components.binary_sensor import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import FritzBoxBaseEntity, FritzBoxTools from .common import FritzBoxBaseEntity, FritzBoxTools
from .const import DOMAIN from .const import DOMAIN
@ -17,7 +18,7 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
_LOGGER.debug("Setting up FRITZ!Box binary sensors") _LOGGER.debug("Setting up FRITZ!Box binary sensors")
@ -44,12 +45,12 @@ class FritzBoxConnectivitySensor(FritzBoxBaseEntity, BinarySensorEntity):
super().__init__(fritzbox_tools, device_friendly_name) super().__init__(fritzbox_tools, device_friendly_name)
@property @property
def name(self): def name(self) -> str:
"""Return name.""" """Return name."""
return self._name return self._name
@property @property
def device_class(self): def device_class(self) -> str:
"""Return device class.""" """Return device class."""
return DEVICE_CLASS_CONNECTIVITY return DEVICE_CLASS_CONNECTIVITY
@ -59,7 +60,7 @@ class FritzBoxConnectivitySensor(FritzBoxBaseEntity, BinarySensorEntity):
return self._is_on return self._is_on
@property @property
def unique_id(self): def unique_id(self) -> str:
"""Return unique id.""" """Return unique id."""
return self._unique_id return self._unique_id

View File

@ -17,9 +17,11 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect 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 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 from .const import DATA_FRITZ, DEFAULT_DEVICE_NAME, DOMAIN
_LOGGER = logging.getLogger(__name__) _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.""" """Import legacy FRITZ!Box configuration."""
_LOGGER.debug("Import legacy FRITZ!Box configuration from YAML") _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( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Set up device tracker for FRITZ!Box component.""" """Set up device tracker for FRITZ!Box component."""
_LOGGER.debug("Starting FRITZ!Box device tracker") _LOGGER.debug("Starting FRITZ!Box device tracker")
router = hass.data[DOMAIN][entry.entry_id] router: FritzBoxTools = hass.data[DOMAIN][entry.entry_id]
data_fritz = hass.data[DATA_FRITZ] data_fritz: FritzData = hass.data[DATA_FRITZ]
@callback @callback
def update_router(): def update_router() -> None:
"""Update the values of the router.""" """Update the values of the router."""
_async_add_entities(router, async_add_entities, data_fritz) _async_add_entities(router, async_add_entities, data_fritz)
@ -83,10 +85,14 @@ async def async_setup_entry(
@callback @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.""" """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(): for tracked in data_fritz.tracked.values():
if mac in tracked: if mac in tracked:
return True return True
@ -118,27 +124,28 @@ class FritzBoxTracker(ScannerEntity):
self._name = device.hostname or DEFAULT_DEVICE_NAME self._name = device.hostname or DEFAULT_DEVICE_NAME
self._last_activity = device.last_activity self._last_activity = device.last_activity
self._active = False self._active = False
self._attrs: dict = {}
@property @property
def is_connected(self): def is_connected(self) -> bool:
"""Return device status.""" """Return device status."""
return self._active return self._active
@property @property
def name(self): def name(self) -> str:
"""Return device name.""" """Return device name."""
return self._name return self._name
@property @property
def unique_id(self): def unique_id(self) -> str:
"""Return device unique id.""" """Return device unique id."""
return self._mac return self._mac
@property @property
def ip_address(self) -> str: def ip_address(self) -> str | None:
"""Return the primary ip address of the device.""" """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 @property
def mac_address(self) -> str: def mac_address(self) -> str:
@ -146,9 +153,11 @@ class FritzBoxTracker(ScannerEntity):
return self._mac return self._mac
@property @property
def hostname(self) -> str: def hostname(self) -> str | None:
"""Return hostname of the device.""" """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 @property
def source_type(self) -> str: def source_type(self) -> str:
@ -156,7 +165,7 @@ class FritzBoxTracker(ScannerEntity):
return SOURCE_TYPE_ROUTER return SOURCE_TYPE_ROUTER
@property @property
def device_info(self): def device_info(self) -> DeviceInfo:
"""Return the device information.""" """Return the device information."""
return { return {
"connections": {(CONNECTION_NETWORK_MAC, self._mac)}, "connections": {(CONNECTION_NETWORK_MAC, self._mac)},
@ -176,7 +185,7 @@ class FritzBoxTracker(ScannerEntity):
return False return False
@property @property
def icon(self): def icon(self) -> str:
"""Return device icon.""" """Return device icon."""
if self.is_connected: if self.is_connected:
return "mdi:lan-connect" return "mdi:lan-connect"
@ -200,17 +209,20 @@ class FritzBoxTracker(ScannerEntity):
@callback @callback
def async_process_update(self) -> None: def async_process_update(self) -> None:
"""Update device.""" """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._active = device.is_connected
self._last_activity = device.last_activity self._last_activity = device.last_activity
@callback @callback
def async_on_demand_update(self): def async_on_demand_update(self) -> None:
"""Update state.""" """Update state."""
self.async_process_update() self.async_process_update()
self.async_write_ha_state() self.async_write_ha_state()
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register state update callback.""" """Register state update callback."""
self.async_process_update() self.async_process_update()
self.async_on_remove( self.async_on_remove(

View File

@ -10,14 +10,14 @@ from .const import DOMAIN, FRITZ_SERVICES, SERVICE_REBOOT, SERVICE_RECONNECT
_LOGGER = logging.getLogger(__name__) _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.""" """Set up services for Fritz integration."""
for service in [SERVICE_REBOOT, SERVICE_RECONNECT]: for service in [SERVICE_REBOOT, SERVICE_RECONNECT]:
if hass.services.has_service(DOMAIN, service): if hass.services.has_service(DOMAIN, service):
return return
async def async_call_fritz_service(service_call): async def async_call_fritz_service(service_call: ServiceCall) -> None:
"""Call correct Fritz service.""" """Call correct Fritz service."""
if not ( if not (
@ -40,10 +40,10 @@ async def async_setup_services(hass: HomeAssistant):
async def _async_get_configured_fritz_tools( async def _async_get_configured_fritz_tools(
hass: HomeAssistant, service_call: ServiceCall hass: HomeAssistant, service_call: ServiceCall
): ) -> list:
"""Get FritzBoxTools class from config entry.""" """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): for entry_id in await async_extract_config_entry_ids(hass, service_call):
config_entry = hass.config_entries.async_get_entry(entry_id) config_entry = hass.config_entries.async_get_entry(entry_id)
if config_entry and config_entry.domain == DOMAIN: if config_entry and config_entry.domain == DOMAIN:
@ -51,7 +51,7 @@ async def _async_get_configured_fritz_tools(
return list_entry_id return list_entry_id
async def async_unload_services(hass: HomeAssistant): async def async_unload_services(hass: HomeAssistant) -> None:
"""Unload services for Fritz integration.""" """Unload services for Fritz integration."""
if not hass.data.get(FRITZ_SERVICES): if not hass.data.get(FRITZ_SERVICES):