75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
"""Config flow for Bring! integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from bring_api.bring import Bring
|
|
from bring_api.exceptions import BringAuthException, BringRequestException
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.helpers.selector import (
|
|
TextSelector,
|
|
TextSelectorConfig,
|
|
TextSelectorType,
|
|
)
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_EMAIL): TextSelector(
|
|
TextSelectorConfig(
|
|
type=TextSelectorType.EMAIL,
|
|
),
|
|
),
|
|
vol.Required(CONF_PASSWORD): TextSelector(
|
|
TextSelectorConfig(
|
|
type=TextSelectorType.PASSWORD,
|
|
),
|
|
),
|
|
}
|
|
)
|
|
|
|
|
|
class BringConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Bring!."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle the initial step."""
|
|
errors: dict[str, str] = {}
|
|
if user_input is not None:
|
|
session = async_get_clientsession(self.hass)
|
|
bring = Bring(session, user_input[CONF_EMAIL], user_input[CONF_PASSWORD])
|
|
|
|
try:
|
|
await bring.login()
|
|
await bring.load_lists()
|
|
except BringRequestException:
|
|
errors["base"] = "cannot_connect"
|
|
except BringAuthException:
|
|
errors["base"] = "invalid_auth"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
await self.async_set_unique_id(bring.uuid)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_EMAIL], data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
)
|