2019-07-09 08:29:06 +00:00
|
|
|
"""Config flow to configure the Notion integration."""
|
2021-07-07 22:39:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-06 19:40:00 +00:00
|
|
|
from aionotion import async_get_client
|
|
|
|
from aionotion.errors import NotionError
|
2019-07-09 08:29:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2021-07-07 22:39:52 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-07-09 08:29:06 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import DOMAIN
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
|
2020-02-25 05:36:58 +00:00
|
|
|
class NotionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Handle a Notion config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2021-07-07 22:39:52 +00:00
|
|
|
def __init__(self) -> None:
|
2020-02-25 05:36:58 +00:00
|
|
|
"""Initialize the config flow."""
|
|
|
|
self.data_schema = vol.Schema(
|
2019-07-31 19:25:30 +00:00
|
|
|
{vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-07-07 22:39:52 +00:00
|
|
|
async def _show_form(self, errors: dict[str, str] | None = None) -> FlowResult:
|
2020-02-25 05:36:58 +00:00
|
|
|
"""Show the form to the user."""
|
2019-07-09 08:29:06 +00:00
|
|
|
return self.async_show_form(
|
2020-02-25 05:36:58 +00:00
|
|
|
step_id="user", data_schema=self.data_schema, errors=errors or {}
|
2019-07-09 08:29:06 +00:00
|
|
|
)
|
|
|
|
|
2021-07-07 22:39:52 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, str] | None = None
|
|
|
|
) -> FlowResult:
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Handle the start of the config flow."""
|
|
|
|
if not user_input:
|
|
|
|
return await self._show_form()
|
|
|
|
|
2020-02-25 05:36:58 +00:00
|
|
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
|
|
|
self._abort_if_unique_id_configured()
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await async_get_client(
|
2019-07-31 19:25:30 +00:00
|
|
|
user_input[CONF_USERNAME], user_input[CONF_PASSWORD], session
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
except NotionError:
|
2020-10-05 13:13:37 +00:00
|
|
|
return await self._show_form({"base": "invalid_auth"})
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=user_input[CONF_USERNAME], data=user_input)
|