1 """Config flow to configure the SimpliSafe component."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from typing
import Any, NamedTuple
8 from simplipy
import API
9 from simplipy.errors
import InvalidCredentialsError, SimplipyError
10 from simplipy.util.auth
import (
11 get_auth0_code_challenge,
12 get_auth0_code_verifier,
15 import voluptuous
as vol
27 from .const
import DOMAIN, LOGGER
29 CONF_AUTH_CODE =
"auth_code"
31 STEP_USER_SCHEMA = vol.Schema(
33 vol.Required(CONF_AUTH_CODE): cv.string,
39 """Define a named tuple to handle SimpliSafe OAuth strings."""
47 """Get a SimpliSafe OAuth code verifier and auth URL."""
48 code_verifier = get_auth0_code_verifier()
49 code_challenge = get_auth0_code_challenge(code_verifier)
50 auth_url = get_auth_url(code_challenge)
55 """Handle a SimpliSafe config flow."""
60 """Initialize the config flow."""
62 self.
_reauth_reauth: bool =
False
67 config_entry: ConfigEntry,
68 ) -> SimpliSafeOptionsFlowHandler:
69 """Define the config flow to handle options."""
73 self, entry_data: Mapping[str, Any]
74 ) -> ConfigFlowResult:
75 """Handle configuration by re-auth."""
80 self, user_input: dict[str, Any] |
None =
None
81 ) -> ConfigFlowResult:
82 """Handle the start of the config flow."""
83 if user_input
is None:
86 data_schema=STEP_USER_SCHEMA,
87 description_placeholders={CONF_URL: self._oauth_values.auth_url},
90 auth_code = user_input[CONF_AUTH_CODE]
92 if auth_code.startswith(
"="):
95 LOGGER.debug(
'Stripping "=" from the start of the authorization code')
96 auth_code = auth_code[1:]
98 if len(auth_code) != 45:
103 data_schema=STEP_USER_SCHEMA,
104 errors={CONF_AUTH_CODE:
"invalid_auth_code_length"},
105 description_placeholders={CONF_URL: self._oauth_values.auth_url},
109 session = aiohttp_client.async_get_clientsession(self.hass)
111 simplisafe = await API.async_from_auth(
113 self._oauth_values.code_verifier,
116 except InvalidCredentialsError:
117 errors = {CONF_AUTH_CODE:
"invalid_auth"}
118 except SimplipyError
as err:
119 LOGGER.error(
"Unknown error while logging into SimpliSafe: %s", err)
120 errors = {
"base":
"unknown"}
125 data_schema=STEP_USER_SCHEMA,
127 description_placeholders={CONF_URL: self._oauth_values.auth_url},
130 simplisafe_user_id =
str(simplisafe.user_id)
131 data = {CONF_USERNAME: simplisafe_user_id, CONF_TOKEN: simplisafe.refresh_token}
135 if not existing_entry:
140 self.hass.config_entries.async_update_entry(
141 existing_entry, unique_id=simplisafe_user_id, data=data
143 self.hass.async_create_task(
144 self.hass.config_entries.async_reload(existing_entry.entry_id)
154 """Handle a SimpliSafe options flow."""
157 self, user_input: dict[str, Any] |
None =
None
158 ) -> ConfigFlowResult:
159 """Manage the options."""
160 if user_input
is not None:
165 data_schema=vol.Schema(
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
SimpliSafeOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_init(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|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_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
SimpliSafeOAuthValues async_get_simplisafe_oauth_values()