56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""Config flow utilities."""
|
|
from collections import OrderedDict
|
|
|
|
from pyvesync import VeSync
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.core import callback
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
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):
|
|
"""Show form to the user."""
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema(self.data_schema),
|
|
errors=errors if errors else {},
|
|
)
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
"""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()
|
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
manager = VeSync(self._username, self._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},
|
|
)
|