From 86ef7bab28ecac460f554fbc2f5133288b1d6f85 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 6 Sep 2024 15:20:11 +0200 Subject: [PATCH] Improve config flow type hints in totalconnect (#125300) --- .../components/totalconnect/config_flow.py | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/totalconnect/config_flow.py b/homeassistant/components/totalconnect/config_flow.py index 63973fd44e9..2a4c4d421a1 100644 --- a/homeassistant/components/totalconnect/config_flow.py +++ b/homeassistant/components/totalconnect/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any from total_connect_client.client import TotalConnectClient from total_connect_client.exceptions import AuthenticationError @@ -17,6 +17,7 @@ from homeassistant.config_entries import ( ) from homeassistant.const import CONF_LOCATION, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback +from homeassistant.helpers.typing import VolDictType from .const import AUTO_BYPASS, CONF_USERCODES, DOMAIN @@ -28,15 +29,16 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 + client: TotalConnectClient + def __init__(self) -> None: """Initialize the config flow.""" - self.username = None - self.password = None - self.usercodes: dict[str, Any] = {} - self.client = None + self.username: str | None = None + self.password: str | None = None + self.usercodes: dict[int, str | None] = {} async def async_step_user( - self, user_input: dict[str, Any] | None = None + self, user_input: dict[str, str] | None = None ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} @@ -70,18 +72,20 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): step_id="user", data_schema=data_schema, errors=errors ) - async def async_step_locations(self, user_entry=None): + async def async_step_locations( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle the user locations and associated usercodes.""" errors = {} - if user_entry is not None: + if user_input is not None: for location_id in self.usercodes: if self.usercodes[location_id] is None: valid = await self.hass.async_add_executor_job( self.client.locations[location_id].set_usercode, - user_entry[CONF_USERCODES], + user_input[CONF_USERCODES], ) if valid: - self.usercodes[location_id] = user_entry[CONF_USERCODES] + self.usercodes[location_id] = user_input[CONF_USERCODES] else: errors[CONF_LOCATION] = "usercode" break @@ -111,11 +115,11 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): self.usercodes[location_id] = None # show the next location that needs a usercode - location_codes = {} + location_codes: VolDictType = {} location_for_user = "" for location_id in self.usercodes: if self.usercodes[location_id] is None: - location_for_user = location_id + location_for_user = str(location_id) location_codes[ vol.Required( CONF_USERCODES, @@ -141,7 +145,9 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): 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, str] | None = None + ) -> ConfigFlowResult: """Dialog that informs the user that reauth is required.""" errors = {} if user_input is None: @@ -166,6 +172,8 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): ) existing_entry = await self.async_set_unique_id(self.username) + if TYPE_CHECKING: + assert existing_entry is not None new_entry = { CONF_USERNAME: self.username, CONF_PASSWORD: user_input[CONF_PASSWORD], @@ -195,7 +203,9 @@ class TotalConnectOptionsFlowHandler(OptionsFlow): """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, bool] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input)