Improve config flow type hints in sense (#124350)

This commit is contained in:
epenet 2024-08-28 12:59:12 +02:00 committed by GitHub
parent 0a94242337
commit 35d318818a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,7 @@
from collections.abc import Mapping
from functools import partial
import logging
from typing import Any
from typing import TYPE_CHECKING, Any
from sense_energy import (
ASyncSenseable,
@ -34,13 +34,12 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1
def __init__(self):
def __init__(self) -> None:
"""Init Config ."""
self._gateway = None
self._auth_data = {}
super().__init__()
self._gateway: ASyncSenseable | None = None
self._auth_data: dict[str, Any] = {}
async def validate_input(self, data):
async def validate_input(self, data: Mapping[str, Any]) -> None:
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
@ -59,6 +58,8 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
client_session=client_session,
)
)
if TYPE_CHECKING:
assert self._gateway
self._gateway.rate_limit = ACTIVE_UPDATE_RATE
await self._gateway.authenticate(
self._auth_data[CONF_EMAIL], self._auth_data[CONF_PASSWORD]
@ -79,7 +80,9 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_update_reload_and_abort(existing_entry, data=self._auth_data)
async def validate_input_and_create_entry(self, user_input, errors):
async def validate_input_and_create_entry(
self, user_input: Mapping[str, Any], errors: dict[str, str]
) -> ConfigFlowResult | None:
"""Validate the input and create the entry from the data."""
try:
await self.validate_input(user_input)
@ -118,9 +121,11 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
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 the initial step."""
errors = {}
errors: dict[str, str] = {}
if user_input is not None:
if result := await self.validate_input_and_create_entry(user_input, errors):
return result
@ -136,9 +141,11 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
self._auth_data = dict(entry_data)
return await self.async_step_reauth_validate(entry_data)
async def async_step_reauth_validate(self, user_input=None):
async def async_step_reauth_validate(
self, user_input: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle reauth and validation."""
errors = {}
errors: dict[str, str] = {}
if user_input is not None:
if result := await self.validate_input_and_create_entry(user_input, errors):
return result