Fix netgear typing (#67287)

This commit is contained in:
Martin Hjelmare 2022-02-26 23:00:33 +01:00 committed by GitHub
parent 7f4faafe38
commit d299915c1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 32 deletions

View file

@ -1,6 +1,9 @@
"""Support for Netgear routers."""
from __future__ import annotations
from datetime import timedelta
import logging
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL
@ -51,6 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload(entry.add_update_listener(update_listener))
assert entry.unique_id
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
@ -67,7 +71,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Fetch data from the router."""
return await router.async_update_device_trackers()
async def async_update_traffic_meter() -> dict:
async def async_update_traffic_meter() -> dict[str, Any] | None:
"""Fetch data from the router."""
return await router.async_get_traffic_meter()

View file

@ -1,5 +1,8 @@
"""Config flow to configure the Netgear integration."""
from __future__ import annotations
import logging
from typing import cast
from urllib.parse import urlparse
from pynetgear import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_USER
@ -119,11 +122,12 @@ class NetgearFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Initialize flow from ssdp."""
updated_data = {}
updated_data: dict[str, str | int | bool] = {}
device_url = urlparse(discovery_info.ssdp_location)
if device_url.hostname:
updated_data[CONF_HOST] = device_url.hostname
if hostname := device_url.hostname:
hostname = cast(str, hostname)
updated_data[CONF_HOST] = hostname
_LOGGER.debug("Netgear ssdp discovery info: %s", discovery_info)

View file

@ -59,7 +59,7 @@ class NetgearScannerEntity(NetgearBaseEntity, ScannerEntity):
self._hostname = self.get_hostname()
self._icon = DEVICE_ICONS.get(device["device_type"], "mdi:help-network")
def get_hostname(self):
def get_hostname(self) -> str | None:
"""Return the hostname of the given device or None if we don't know."""
if (hostname := self._device["name"]) == "--":
return None
@ -74,7 +74,7 @@ class NetgearScannerEntity(NetgearBaseEntity, ScannerEntity):
self._icon = DEVICE_ICONS.get(self._device["device_type"], "mdi:help-network")
@property
def is_connected(self):
def is_connected(self) -> bool:
"""Return true if the device is connected to the router."""
return self._active
@ -94,7 +94,7 @@ class NetgearScannerEntity(NetgearBaseEntity, ScannerEntity):
return self._mac
@property
def hostname(self) -> str:
def hostname(self) -> str | None:
"""Return the hostname."""
return self._hostname

View file

@ -5,6 +5,7 @@ from abc import abstractmethod
import asyncio
from datetime import timedelta
import logging
from typing import Any
from pynetgear import Netgear
@ -59,13 +60,14 @@ class NetgearRouter:
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Initialize a Netgear router."""
assert entry.unique_id
self.hass = hass
self.entry = entry
self.entry_id = entry.entry_id
self.unique_id = entry.unique_id
self._host = entry.data.get(CONF_HOST)
self._port = entry.data.get(CONF_PORT)
self._ssl = entry.data.get(CONF_SSL)
self._host: str = entry.data[CONF_HOST]
self._port: int = entry.data[CONF_PORT]
self._ssl: bool = entry.data[CONF_SSL]
self._username = entry.data.get(CONF_USERNAME)
self._password = entry.data[CONF_PASSWORD]
@ -85,9 +87,9 @@ class NetgearRouter:
self._api: Netgear = None
self._api_lock = asyncio.Lock()
self.devices = {}
self.devices: dict[str, Any] = {}
def _setup(self) -> None:
def _setup(self) -> bool:
"""Set up a Netgear router sync portion."""
self._api = get_api(
self._password,
@ -134,7 +136,7 @@ class NetgearRouter:
if device_entry.via_device_id is None:
continue # do not add the router itself
device_mac = dict(device_entry.connections).get(dr.CONNECTION_NETWORK_MAC)
device_mac = dict(device_entry.connections)[dr.CONNECTION_NETWORK_MAC]
self.devices[device_mac] = {
"mac": device_mac,
"name": device_entry.name,
@ -166,14 +168,14 @@ class NetgearRouter:
self._api.get_attached_devices_2
)
async def async_update_device_trackers(self, now=None) -> None:
async def async_update_device_trackers(self, now=None) -> bool:
"""Update Netgear devices."""
new_device = False
ntg_devices = await self.async_get_attached_devices()
now = dt_util.utcnow()
if ntg_devices is None:
return
return new_device
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug("Netgear scan result: \n%s", ntg_devices)
@ -197,7 +199,7 @@ class NetgearRouter:
return new_device
async def async_get_traffic_meter(self) -> None:
async def async_get_traffic_meter(self) -> dict[str, Any] | None:
"""Get the traffic meter data of the router."""
async with self._api_lock:
return await self.hass.async_add_executor_job(self._api.get_traffic_meter)

View file

@ -2480,18 +2480,6 @@ ignore_errors = true
[mypy-homeassistant.components.minecraft_server.sensor]
ignore_errors = true
[mypy-homeassistant.components.netgear]
ignore_errors = true
[mypy-homeassistant.components.netgear.config_flow]
ignore_errors = true
[mypy-homeassistant.components.netgear.device_tracker]
ignore_errors = true
[mypy-homeassistant.components.netgear.router]
ignore_errors = true
[mypy-homeassistant.components.nilu.air_quality]
ignore_errors = true

View file

@ -94,10 +94,6 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.minecraft_server",
"homeassistant.components.minecraft_server.helpers",
"homeassistant.components.minecraft_server.sensor",
"homeassistant.components.netgear",
"homeassistant.components.netgear.config_flow",
"homeassistant.components.netgear.device_tracker",
"homeassistant.components.netgear.router",
"homeassistant.components.nilu.air_quality",
"homeassistant.components.nzbget",
"homeassistant.components.nzbget.config_flow",