From f67bae2cdeca7263131a5d1475cc912c929adddf Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:33:15 +0100 Subject: [PATCH] Enable strict typing for aruba (#106839) --- .strict-typing | 1 + .../components/aruba/device_tracker.py | 37 ++++++++++--------- mypy.ini | 10 +++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.strict-typing b/.strict-typing index 2516beaf36a..64207dd98a1 100644 --- a/.strict-typing +++ b/.strict-typing @@ -76,6 +76,7 @@ homeassistant.components.aprs.* homeassistant.components.aqualogic.* homeassistant.components.aquostv.* homeassistant.components.aranet.* +homeassistant.components.aruba.* homeassistant.components.aseko_pool_live.* homeassistant.components.assist_pipeline.* homeassistant.components.asuswrt.* diff --git a/homeassistant/components/aruba/device_tracker.py b/homeassistant/components/aruba/device_tracker.py index 7b8c547fd53..1b449450cf8 100644 --- a/homeassistant/components/aruba/device_tracker.py +++ b/homeassistant/components/aruba/device_tracker.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging import re +from typing import Any import pexpect import voluptuous as vol @@ -44,33 +45,33 @@ def get_scanner(hass: HomeAssistant, config: ConfigType) -> ArubaDeviceScanner | class ArubaDeviceScanner(DeviceScanner): """Class which queries a Aruba Access Point for connected devices.""" - def __init__(self, config): + def __init__(self, config: dict[str, Any]) -> None: """Initialize the scanner.""" - self.host = config[CONF_HOST] - self.username = config[CONF_USERNAME] - self.password = config[CONF_PASSWORD] + self.host: str = config[CONF_HOST] + self.username: str = config[CONF_USERNAME] + self.password: str = config[CONF_PASSWORD] - self.last_results = {} + self.last_results: dict[str, dict[str, str]] = {} # Test the router is accessible. data = self.get_aruba_data() self.success_init = data is not None - def scan_devices(self): + def scan_devices(self) -> list[str]: """Scan for new devices and return a list with found device IDs.""" self._update_info() - return [client["mac"] for client in self.last_results] + return [client["mac"] for client in self.last_results.values()] - def get_device_name(self, device): + def get_device_name(self, device: str) -> str | None: """Return the name of the given device or None if we don't know.""" if not self.last_results: return None - for client in self.last_results: + for client in self.last_results.values(): if client["mac"] == device: return client["name"] return None - def _update_info(self): + def _update_info(self) -> bool: """Ensure the information from the Aruba Access Point is up to date. Return boolean if scanning successful. @@ -81,10 +82,10 @@ class ArubaDeviceScanner(DeviceScanner): if not (data := self.get_aruba_data()): return False - self.last_results = data.values() + self.last_results = data return True - def get_aruba_data(self): + def get_aruba_data(self) -> dict[str, dict[str, str]] | None: """Retrieve data from Aruba Access Point and return parsed result.""" connect = f"ssh {self.username}@{self.host} -o HostKeyAlgorithms=ssh-rsa" @@ -103,22 +104,22 @@ class ArubaDeviceScanner(DeviceScanner): ) if query == 1: _LOGGER.error("Timeout") - return + return None if query == 2: _LOGGER.error("Unexpected response from router") - return + return None if query == 3: ssh.sendline("yes") ssh.expect("password:") elif query == 4: _LOGGER.error("Host key changed") - return + return None elif query == 5: _LOGGER.error("Connection refused by server") - return + return None elif query == 6: _LOGGER.error("Connection timed out") - return + return None ssh.sendline(self.password) ssh.expect("#") ssh.sendline("show clients") @@ -126,7 +127,7 @@ class ArubaDeviceScanner(DeviceScanner): devices_result = ssh.before.split(b"\r\n") ssh.sendline("exit") - devices = {} + devices: dict[str, dict[str, str]] = {} for device in devices_result: if match := _DEVICES_REGEX.search(device.decode("utf-8")): devices[match.group("ip")] = { diff --git a/mypy.ini b/mypy.ini index 3e4385fddcb..57eebdbed1e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -520,6 +520,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.aruba.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.aseko_pool_live.*] check_untyped_defs = true disallow_incomplete_defs = true