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
|
|
|
|
|
2021-09-24 18:23:19 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
|
2019-12-06 19:40:00 +00:00
|
|
|
from aionotion import async_get_client
|
2021-09-24 18:23:19 +00:00
|
|
|
from aionotion.errors import InvalidCredentialsError, 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-09-24 18:23:19 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
|
|
|
from .const import DOMAIN, LOGGER
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-09-24 18:23:19 +00:00
|
|
|
AUTH_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
RE_AUTH_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
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."""
|
2021-09-24 18:23:19 +00:00
|
|
|
self._password: str | None = None
|
|
|
|
self._username: str | None = None
|
|
|
|
|
|
|
|
async def _async_verify(self, step_id: str, schema: vol.Schema) -> FlowResult:
|
|
|
|
"""Attempt to authenticate the provided credentials."""
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert self._username
|
|
|
|
assert self._password
|
|
|
|
|
2021-11-14 18:08:35 +00:00
|
|
|
errors = {}
|
2021-09-24 18:23:19 +00:00
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await async_get_client(self._username, self._password, session=session)
|
|
|
|
except InvalidCredentialsError:
|
2021-11-14 18:08:35 +00:00
|
|
|
errors["base"] = "invalid_auth"
|
2021-09-24 18:23:19 +00:00
|
|
|
except NotionError as err:
|
|
|
|
LOGGER.error("Unknown Notion error: %s", err)
|
2021-11-14 18:08:35 +00:00
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
if errors:
|
2021-09-24 18:23:19 +00:00
|
|
|
return self.async_show_form(
|
|
|
|
step_id=step_id,
|
|
|
|
data_schema=schema,
|
2021-11-14 18:08:35 +00:00
|
|
|
errors=errors,
|
2021-09-24 18:23:19 +00:00
|
|
|
description_placeholders={CONF_USERNAME: self._username},
|
|
|
|
)
|
|
|
|
|
|
|
|
data = {CONF_USERNAME: self._username, CONF_PASSWORD: self._password}
|
|
|
|
|
|
|
|
if existing_entry := await self.async_set_unique_id(self._username):
|
|
|
|
self.hass.config_entries.async_update_entry(existing_entry, data=data)
|
|
|
|
self.hass.async_create_task(
|
|
|
|
self.hass.config_entries.async_reload(existing_entry.entry_id)
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-09-24 18:23:19 +00:00
|
|
|
return self.async_create_entry(title=self._username, data=data)
|
|
|
|
|
|
|
|
async def async_step_reauth(self, config: ConfigType) -> FlowResult:
|
|
|
|
"""Handle configuration by re-auth."""
|
|
|
|
self._username = config[CONF_USERNAME]
|
|
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
|
|
|
|
async def async_step_reauth_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle re-auth completion."""
|
|
|
|
if not user_input:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reauth_confirm",
|
|
|
|
data_schema=RE_AUTH_SCHEMA,
|
|
|
|
description_placeholders={CONF_USERNAME: self._username},
|
|
|
|
)
|
|
|
|
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
|
|
|
|
return await self._async_verify("reauth_confirm", RE_AUTH_SCHEMA)
|
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:
|
2021-09-24 18:23:19 +00:00
|
|
|
return self.async_show_form(step_id="user", data_schema=AUTH_SCHEMA)
|
2019-07-09 08:29:06 +00:00
|
|
|
|
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
|
|
|
|
2021-09-24 18:23:19 +00:00
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-09-24 18:23:19 +00:00
|
|
|
return await self._async_verify("user", AUTH_SCHEMA)
|