Improve config flow type hints in sense (#124350)
This commit is contained in:
parent
0a94242337
commit
35d318818a
1 changed files with 18 additions and 11 deletions
|
@ -3,7 +3,7 @@
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from sense_energy import (
|
from sense_energy import (
|
||||||
ASyncSenseable,
|
ASyncSenseable,
|
||||||
|
@ -34,13 +34,12 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
"""Init Config ."""
|
"""Init Config ."""
|
||||||
self._gateway = None
|
self._gateway: ASyncSenseable | None = None
|
||||||
self._auth_data = {}
|
self._auth_data: dict[str, Any] = {}
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
async def validate_input(self, data):
|
async def validate_input(self, data: Mapping[str, Any]) -> None:
|
||||||
"""Validate the user input allows us to connect.
|
"""Validate the user input allows us to connect.
|
||||||
|
|
||||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
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,
|
client_session=client_session,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
assert self._gateway
|
||||||
self._gateway.rate_limit = ACTIVE_UPDATE_RATE
|
self._gateway.rate_limit = ACTIVE_UPDATE_RATE
|
||||||
await self._gateway.authenticate(
|
await self._gateway.authenticate(
|
||||||
self._auth_data[CONF_EMAIL], self._auth_data[CONF_PASSWORD]
|
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)
|
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."""
|
"""Validate the input and create the entry from the data."""
|
||||||
try:
|
try:
|
||||||
await self.validate_input(user_input)
|
await self.validate_input(user_input)
|
||||||
|
@ -118,9 +121,11 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
errors=errors,
|
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."""
|
"""Handle the initial step."""
|
||||||
errors = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if result := await self.validate_input_and_create_entry(user_input, errors):
|
if result := await self.validate_input_and_create_entry(user_input, errors):
|
||||||
return result
|
return result
|
||||||
|
@ -136,9 +141,11 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
self._auth_data = dict(entry_data)
|
self._auth_data = dict(entry_data)
|
||||||
return await self.async_step_reauth_validate(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."""
|
"""Handle reauth and validation."""
|
||||||
errors = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if result := await self.validate_input_and_create_entry(user_input, errors):
|
if result := await self.validate_input_and_create_entry(user_input, errors):
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue