mirror of https://github.com/home-assistant/core
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
"""Config flow utilities."""
|
|
|
|
from typing import Any
|
|
|
|
from pyvesync import VeSync
|
|
import voluptuous as vol
|
|
|
|
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
|
|
|
|
@callback
|
|
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=DATA_SCHEMA,
|
|
errors=errors if errors else {},
|
|
)
|
|
|
|
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")
|
|
|
|
if not user_input:
|
|
return self._show_form()
|
|
|
|
username = user_input[CONF_USERNAME]
|
|
password = user_input[CONF_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=username,
|
|
data={CONF_USERNAME: username, CONF_PASSWORD: password},
|
|
)
|