1 """Config flow to configure the Notion integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from dataclasses
import dataclass, field
9 from aionotion.errors
import InvalidCredentialsError, NotionError
10 import voluptuous
as vol
16 from .const
import CONF_REFRESH_TOKEN, CONF_USER_UUID, DOMAIN, LOGGER
17 from .util
import async_get_client_with_credentials
19 AUTH_SCHEMA = vol.Schema(
21 vol.Required(CONF_USERNAME): str,
22 vol.Required(CONF_PASSWORD): str,
25 REAUTH_SCHEMA = vol.Schema(
27 vol.Required(CONF_PASSWORD): str,
32 @dataclass(frozen=True, kw_only=True)
34 """Define a validation result."""
36 user_uuid: str |
None =
None
37 refresh_token: str |
None =
None
38 errors: dict[str, Any] = field(default_factory=dict)
42 hass: HomeAssistant, username: str, password: str
43 ) -> CredentialsValidationResult:
44 """Validate a Notion username and password."""
49 except InvalidCredentialsError:
50 errors[
"base"] =
"invalid_auth"
51 except NotionError
as err:
52 LOGGER.error(
"Unknown Notion error while validation credentials: %s", err)
53 errors[
"base"] =
"unknown"
54 except Exception
as err:
55 LOGGER.exception(
"Unknown error while validation credentials: %s", err)
56 errors[
"base"] =
"unknown"
62 user_uuid=client.user_uuid, refresh_token=client.refresh_token
67 """Handle a Notion config flow."""
72 self, entry_data: Mapping[str, Any]
73 ) -> ConfigFlowResult:
74 """Handle configuration by re-auth."""
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle re-auth completion."""
85 step_id=
"reauth_confirm",
86 data_schema=REAUTH_SCHEMA,
87 description_placeholders={
88 CONF_USERNAME: reauth_entry.data[CONF_USERNAME]
93 self.hass, reauth_entry.data[CONF_USERNAME], user_input[CONF_PASSWORD]
96 if credentials_validation_result.errors:
98 step_id=
"reauth_confirm",
99 data_schema=REAUTH_SCHEMA,
100 errors=credentials_validation_result.errors,
101 description_placeholders={
102 CONF_USERNAME: reauth_entry.data[CONF_USERNAME]
109 CONF_REFRESH_TOKEN: credentials_validation_result.refresh_token
114 self, user_input: dict[str, str] |
None =
None
115 ) -> ConfigFlowResult:
116 """Handle the start of the config flow."""
124 self.hass, user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
127 if credentials_validation_result.errors:
130 data_schema=AUTH_SCHEMA,
131 errors=credentials_validation_result.errors,
135 title=user_input[CONF_USERNAME],
137 CONF_USERNAME: user_input[CONF_USERNAME],
138 CONF_USER_UUID: credentials_validation_result.user_uuid,
139 CONF_REFRESH_TOKEN: credentials_validation_result.refresh_token,
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
CredentialsValidationResult async_validate_credentials(HomeAssistant hass, str username, str password)
Client async_get_client_with_credentials(HomeAssistant hass, str email, str password)