From 909dfcc436aba8f51cd506d2e3f252045171e025 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 25 Aug 2024 18:34:39 +0200 Subject: [PATCH] Improve config flow type hints (part 5) (#124349) --- .../components/totalconnect/config_flow.py | 8 ++++--- .../components/ukraine_alarm/config_flow.py | 17 ++++++++++----- .../components/wilight/config_flow.py | 4 ++-- .../components/xiaomi_aqara/config_flow.py | 21 ++++++++++++------- .../components/yeelight/config_flow.py | 13 ++++++++---- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/totalconnect/config_flow.py b/homeassistant/components/totalconnect/config_flow.py index 19d8f09933e..63973fd44e9 100644 --- a/homeassistant/components/totalconnect/config_flow.py +++ b/homeassistant/components/totalconnect/config_flow.py @@ -28,14 +28,16 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" self.username = None self.password = None - self.usercodes = {} + self.usercodes: dict[str, Any] = {} self.client = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} diff --git a/homeassistant/components/ukraine_alarm/config_flow.py b/homeassistant/components/ukraine_alarm/config_flow.py index bafe6d1fe11..faaa9240df3 100644 --- a/homeassistant/components/ukraine_alarm/config_flow.py +++ b/homeassistant/components/ukraine_alarm/config_flow.py @@ -3,12 +3,13 @@ from __future__ import annotations import logging +from typing import Any import aiohttp from uasiren.client import Client import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_NAME, CONF_REGION from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -22,12 +23,14 @@ class UkraineAlarmConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize a new UkraineAlarmConfigFlow.""" self.states = None - self.selected_region = None + self.selected_region: dict[str, Any] | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if len(self._async_current_entries()) == 5: @@ -91,7 +94,11 @@ class UkraineAlarmConfigFlow(ConfigFlow, domain=DOMAIN): ): self.selected_region = _find(source, user_input[CONF_REGION]) - if next_step and self.selected_region["regionChildIds"]: + if ( + next_step + and self.selected_region + and self.selected_region["regionChildIds"] + ): return await getattr(self, f"async_step_{next_step}")() return await self._async_finish_flow() diff --git a/homeassistant/components/wilight/config_flow.py b/homeassistant/components/wilight/config_flow.py index 52b3b426c20..babc011fc35 100644 --- a/homeassistant/components/wilight/config_flow.py +++ b/homeassistant/components/wilight/config_flow.py @@ -24,13 +24,13 @@ class WiLightFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the WiLight flow.""" self._host = None self._serial_number = None self._title = None self._model_name = None - self._wilight_components = [] + self._wilight_components: list[str] = [] self._components_text = "" def _wilight_update(self, host, serial_number, model_name): diff --git a/homeassistant/components/xiaomi_aqara/config_flow.py b/homeassistant/components/xiaomi_aqara/config_flow.py index 8f391c8ddf3..a89bb8447a3 100644 --- a/homeassistant/components/xiaomi_aqara/config_flow.py +++ b/homeassistant/components/xiaomi_aqara/config_flow.py @@ -2,6 +2,7 @@ import logging from socket import gaierror +from typing import TYPE_CHECKING, Any import voluptuous as vol from xiaomi_gateway import MULTICAST_PORT, XiaomiGateway, XiaomiGatewayDiscovery @@ -49,13 +50,13 @@ class XiaomiAqaraFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize.""" - self.host = None + self.host: str | None = None self.interface = DEFAULT_INTERFACE - self.sid = None - self.gateways = None - self.selected_gateway = None + self.sid: str | None = None + self.gateways: dict[str, XiaomiGateway] | None = None + self.selected_gateway: XiaomiGateway | None = None @callback def async_show_form_step_user(self, errors): @@ -66,9 +67,11 @@ class XiaomiAqaraFlowHandler(ConfigFlow, domain=DOMAIN): return self.async_show_form(step_id="user", data_schema=schema, errors=errors) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is None: return self.async_show_form_step_user(errors) @@ -96,6 +99,8 @@ class XiaomiAqaraFlowHandler(ConfigFlow, domain=DOMAIN): None, ) + if TYPE_CHECKING: + assert self.selected_gateway if self.selected_gateway.connection_error: errors[CONF_HOST] = "invalid_host" if self.selected_gateway.mac_error: @@ -115,6 +120,8 @@ class XiaomiAqaraFlowHandler(ConfigFlow, domain=DOMAIN): self.gateways = xiaomi.gateways + if TYPE_CHECKING: + assert self.gateways is not None if len(self.gateways) == 1: self.selected_gateway = list(self.gateways.values())[0] self.sid = self.selected_gateway.sid diff --git a/homeassistant/components/yeelight/config_flow.py b/homeassistant/components/yeelight/config_flow.py index d7bf4e25996..b4bb7da9a22 100644 --- a/homeassistant/components/yeelight/config_flow.py +++ b/homeassistant/components/yeelight/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse import voluptuous as vol @@ -57,11 +58,11 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN): """Return the options flow.""" return OptionsFlowHandler(config_entry) - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" - self._discovered_devices = {} + self._discovered_devices: dict[str, Any] = {} self._discovered_model = None - self._discovered_ip = None + self._discovered_ip: str | None = None async def async_step_homekit( self, discovery_info: zeroconf.ZeroconfServiceInfo @@ -162,7 +163,9 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN): step_id="discovery_confirm", description_placeholders=placeholders ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -176,6 +179,8 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "cannot_connect" else: self._abort_if_unique_id_configured() + if TYPE_CHECKING: + assert self.unique_id return self.async_create_entry( title=async_format_model_id(model, self.unique_id), data={