Improve config flow type hints in vesync (#124351)
This commit is contained in:
parent
d1681fac72
commit
0a94242337
1 changed files with 20 additions and 18 deletions
|
@ -1,40 +1,42 @@
|
|||
"""Config flow utilities."""
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import Any
|
||||
|
||||
from pyvesync import VeSync
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class VeSyncFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Instantiate config flow."""
|
||||
self._username = None
|
||||
self._password = None
|
||||
self.data_schema = OrderedDict()
|
||||
self.data_schema[vol.Required(CONF_USERNAME)] = str
|
||||
self.data_schema[vol.Required(CONF_PASSWORD)] = str
|
||||
|
||||
@callback
|
||||
def _show_form(self, errors=None):
|
||||
def _show_form(self, errors: dict[str, str] | None = None) -> ConfigFlowResult:
|
||||
"""Show form to the user."""
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema(self.data_schema),
|
||||
data_schema=DATA_SCHEMA,
|
||||
errors=errors if errors else {},
|
||||
)
|
||||
|
||||
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 start."""
|
||||
if self._async_current_entries():
|
||||
return self.async_abort(reason="single_instance_allowed")
|
||||
|
@ -42,15 +44,15 @@ class VeSyncFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||
if not user_input:
|
||||
return self._show_form()
|
||||
|
||||
self._username = user_input[CONF_USERNAME]
|
||||
self._password = user_input[CONF_PASSWORD]
|
||||
username = user_input[CONF_USERNAME]
|
||||
password = user_input[CONF_PASSWORD]
|
||||
|
||||
manager = VeSync(self._username, self._password)
|
||||
manager = VeSync(username, password)
|
||||
login = await self.hass.async_add_executor_job(manager.login)
|
||||
if not login:
|
||||
return self._show_form(errors={"base": "invalid_auth"})
|
||||
|
||||
return self.async_create_entry(
|
||||
title=self._username,
|
||||
data={CONF_USERNAME: self._username, CONF_PASSWORD: self._password},
|
||||
title=username,
|
||||
data={CONF_USERNAME: username, CONF_PASSWORD: password},
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue