From cd8adfc84ea21df2e096e7504d1db289f67f4fd8 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 7 Jan 2024 11:39:41 +0100 Subject: [PATCH] Improve flume typing (#107444) --- homeassistant/components/flume/__init__.py | 6 ++++- homeassistant/components/flume/config_flow.py | 25 +++++++++++++------ homeassistant/components/flume/coordinator.py | 14 +++++------ homeassistant/components/flume/entity.py | 2 +- homeassistant/components/flume/sensor.py | 3 ++- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/flume/__init__.py b/homeassistant/components/flume/__init__.py index a5911af3c8fa..3a8718a14e0c 100644 --- a/homeassistant/components/flume/__init__.py +++ b/homeassistant/components/flume/__init__.py @@ -1,4 +1,6 @@ """The flume integration.""" +from __future__ import annotations + from pyflume import FlumeAuth, FlumeDeviceList from requests import Session from requests.exceptions import RequestException @@ -41,7 +43,9 @@ LIST_NOTIFICATIONS_SERVICE_SCHEMA = vol.All( ) -def _setup_entry(hass: HomeAssistant, entry: ConfigEntry): +def _setup_entry( + hass: HomeAssistant, entry: ConfigEntry +) -> tuple[FlumeAuth, FlumeDeviceList, Session]: """Config entry set up in executor.""" config = entry.data diff --git a/homeassistant/components/flume/config_flow.py b/homeassistant/components/flume/config_flow.py index e31519738d1a..df2a697ed8d4 100644 --- a/homeassistant/components/flume/config_flow.py +++ b/homeassistant/components/flume/config_flow.py @@ -1,4 +1,6 @@ """Config flow for flume integration.""" +from __future__ import annotations + from collections.abc import Mapping import logging import os @@ -36,7 +38,9 @@ DATA_SCHEMA = vol.Schema( ) -def _validate_input(hass: core.HomeAssistant, data: dict, clear_token_file: bool): +def _validate_input( + hass: core.HomeAssistant, data: dict[str, Any], clear_token_file: bool +) -> FlumeDeviceList: """Validate in the executor.""" flume_token_full_path = hass.config.path( f"{BASE_TOKEN_FILENAME}-{data[CONF_USERNAME]}" @@ -56,8 +60,8 @@ def _validate_input(hass: core.HomeAssistant, data: dict, clear_token_file: bool async def validate_input( - hass: core.HomeAssistant, data: dict, clear_token_file: bool = False -): + hass: core.HomeAssistant, data: dict[str, Any], clear_token_file: bool = False +) -> dict[str, Any]: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -85,11 +89,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Init flume config flow.""" - self._reauth_unique_id = None + self._reauth_unique_id: str | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle the initial step.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: await self.async_set_unique_id(user_input[CONF_USERNAME]) self._abort_if_unique_id_configured() @@ -111,10 +117,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._reauth_unique_id = self.context["unique_id"] return await self.async_step_reauth_confirm() - async def async_step_reauth_confirm(self, user_input=None): + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle reauth input.""" - errors = {} + errors: dict[str, str] = {} existing_entry = await self.async_set_unique_id(self._reauth_unique_id) + assert existing_entry if user_input is not None: new_data = {**existing_entry.data, CONF_PASSWORD: user_input[CONF_PASSWORD]} try: diff --git a/homeassistant/components/flume/coordinator.py b/homeassistant/components/flume/coordinator.py index 1f590b0cd162..b5d37b8027f6 100644 --- a/homeassistant/components/flume/coordinator.py +++ b/homeassistant/components/flume/coordinator.py @@ -4,7 +4,7 @@ from __future__ import annotations from typing import Any import pyflume -from pyflume import FlumeDeviceList +from pyflume import FlumeAuth, FlumeData, FlumeDeviceList from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -21,7 +21,7 @@ from .const import ( class FlumeDeviceDataUpdateCoordinator(DataUpdateCoordinator[None]): """Data update coordinator for an individual flume device.""" - def __init__(self, hass: HomeAssistant, flume_device) -> None: + def __init__(self, hass: HomeAssistant, flume_device: FlumeData) -> None: """Initialize the Coordinator.""" super().__init__( hass, @@ -79,7 +79,7 @@ class FlumeDeviceConnectionUpdateCoordinator(DataUpdateCoordinator[None]): class FlumeNotificationDataUpdateCoordinator(DataUpdateCoordinator[None]): """Data update coordinator for flume notifications.""" - def __init__(self, hass: HomeAssistant, auth) -> None: + def __init__(self, hass: HomeAssistant, auth: FlumeAuth) -> None: """Initialize the Coordinator.""" super().__init__( hass, @@ -88,15 +88,15 @@ class FlumeNotificationDataUpdateCoordinator(DataUpdateCoordinator[None]): update_interval=NOTIFICATION_SCAN_INTERVAL, ) self.auth = auth - self.active_notifications_by_device: dict = {} - self.notifications: list[dict[str, Any]] + self.active_notifications_by_device: dict[str, set[str]] = {} + self.notifications: list[dict[str, Any]] = [] - def _update_lists(self): + def _update_lists(self) -> None: """Query flume for notification list.""" # Get notifications (read or unread). # The related binary sensors (leak detected, high flow, low battery) # will be active until the notification is deleted in the Flume app. - self.notifications: list[dict[str, Any]] = pyflume.FlumeNotificationList( + self.notifications = pyflume.FlumeNotificationList( self.auth, read=None ).notification_list _LOGGER.debug("Notifications %s", self.notifications) diff --git a/homeassistant/components/flume/entity.py b/homeassistant/components/flume/entity.py index f17e58529c4a..a6d13b1f2911 100644 --- a/homeassistant/components/flume/entity.py +++ b/homeassistant/components/flume/entity.py @@ -58,7 +58,7 @@ class FlumeEntity(CoordinatorEntity[_FlumeCoordinatorT]): configuration_url="https://portal.flumewater.com", ) - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Request an update when added.""" await super().async_added_to_hass() # We do not ask for an update with async_add_entities() diff --git a/homeassistant/components/flume/sensor.py b/homeassistant/components/flume/sensor.py index fc08fee476c5..d47533012134 100644 --- a/homeassistant/components/flume/sensor.py +++ b/homeassistant/components/flume/sensor.py @@ -12,6 +12,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfVolume from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType from .const import ( DEVICE_SCAN_INTERVAL, @@ -139,7 +140,7 @@ class FlumeSensor(FlumeEntity[FlumeDeviceDataUpdateCoordinator], SensorEntity): """Representation of the Flume sensor.""" @property - def native_value(self): + def native_value(self) -> StateType: """Return the state of the sensor.""" sensor_key = self.entity_description.key if sensor_key not in self.coordinator.flume_device.values: