2018-10-12 17:07:47 +00:00
|
|
|
|
"""Config flow to configure the SimpliSafe component."""
|
2021-07-27 20:11:54 +00:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
from typing import TYPE_CHECKING, Any, NamedTuple
|
|
|
|
|
|
|
|
|
|
from simplipy import API
|
|
|
|
|
from simplipy.errors import InvalidCredentialsError, SimplipyError
|
|
|
|
|
from simplipy.util.auth import (
|
|
|
|
|
get_auth0_code_challenge,
|
|
|
|
|
get_auth0_code_verifier,
|
|
|
|
|
get_auth_url,
|
2020-07-24 02:02:29 +00:00
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-07-27 20:11:54 +00:00
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-19 20:09:48 +00:00
|
|
|
|
from homeassistant.const import CONF_CODE, CONF_TOKEN, CONF_URL, CONF_USERNAME
|
2020-03-13 05:00:00 +00:00
|
|
|
|
from homeassistant.core import callback
|
2021-07-27 20:11:54 +00:00
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2021-10-19 20:09:48 +00:00
|
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
2021-07-27 20:11:54 +00:00
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
from .const import CONF_USER_ID, DOMAIN, LOGGER
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
CONF_AUTH_CODE = "auth_code"
|
|
|
|
|
|
|
|
|
|
STEP_INPUT_AUTH_CODE_SCHEMA = vol.Schema(
|
2020-12-12 09:43:38 +00:00
|
|
|
|
{
|
2021-10-19 20:09:48 +00:00
|
|
|
|
vol.Required(CONF_AUTH_CODE): cv.string,
|
2020-12-12 09:43:38 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimpliSafeOAuthValues(NamedTuple):
|
|
|
|
|
"""Define a named tuple to handle SimpliSafe OAuth strings."""
|
|
|
|
|
|
|
|
|
|
code_verifier: str
|
|
|
|
|
auth_url: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
|
def async_get_simplisafe_oauth_values() -> SimpliSafeOAuthValues:
|
|
|
|
|
"""Get a SimpliSafe OAuth code verifier and auth URL."""
|
|
|
|
|
code_verifier = get_auth0_code_verifier()
|
|
|
|
|
code_challenge = get_auth0_code_challenge(code_verifier)
|
|
|
|
|
auth_url = get_auth_url(code_challenge)
|
|
|
|
|
return SimpliSafeOAuthValues(code_verifier, auth_url)
|
2020-12-12 09:43:38 +00:00
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2020-02-24 20:03:08 +00:00
|
|
|
|
class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
2018-10-12 17:07:47 +00:00
|
|
|
|
"""Handle a SimpliSafe config flow."""
|
|
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
|
def __init__(self) -> None:
|
2018-10-12 17:07:47 +00:00
|
|
|
|
"""Initialize the config flow."""
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._errors: dict[str, Any] = {}
|
|
|
|
|
self._oauth_values: SimpliSafeOAuthValues = async_get_simplisafe_oauth_values()
|
|
|
|
|
self._reauth: bool = False
|
2021-07-27 20:11:54 +00:00
|
|
|
|
self._username: str | None = None
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2020-03-13 05:00:00 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
@callback
|
2021-07-27 20:11:54 +00:00
|
|
|
|
def async_get_options_flow(
|
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
|
) -> SimpliSafeOptionsFlowHandler:
|
2020-03-13 05:00:00 +00:00
|
|
|
|
"""Define the config flow to handle options."""
|
|
|
|
|
return SimpliSafeOptionsFlowHandler(config_entry)
|
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
async def async_step_input_auth_code(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle the input of a SimpliSafe OAuth authorization code."""
|
|
|
|
|
if user_input is None:
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="input_auth_code", data_schema=STEP_INPUT_AUTH_CODE_SCHEMA
|
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
assert self._oauth_values
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._errors = {}
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2021-10-19 20:09:48 +00:00
|
|
|
|
simplisafe = await API.async_from_auth(
|
|
|
|
|
user_input[CONF_AUTH_CODE],
|
|
|
|
|
self._oauth_values.code_verifier,
|
|
|
|
|
session=session,
|
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
except InvalidCredentialsError:
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._errors = {"base": "invalid_auth"}
|
2020-07-24 02:02:29 +00:00
|
|
|
|
except SimplipyError as err:
|
2020-10-17 19:40:34 +00:00
|
|
|
|
LOGGER.error("Unknown error while logging into SimpliSafe: %s", err)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._errors = {"base": "unknown"}
|
|
|
|
|
|
|
|
|
|
if self._errors:
|
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
|
|
data = {CONF_USER_ID: simplisafe.user_id, CONF_TOKEN: simplisafe.refresh_token}
|
|
|
|
|
unique_id = str(simplisafe.user_id)
|
|
|
|
|
|
|
|
|
|
if self._reauth:
|
|
|
|
|
# "Old" config entries utilized the user's email address (username) as the
|
|
|
|
|
# unique ID, whereas "new" config entries utilize the SimpliSafe user ID –
|
|
|
|
|
# either one is a candidate for re-auth:
|
|
|
|
|
existing_entry = await self.async_set_unique_id(self._username or unique_id)
|
|
|
|
|
if not existing_entry:
|
|
|
|
|
# If we don't have an entry that matches this user ID, the user logged
|
|
|
|
|
# in with different credentials:
|
|
|
|
|
return self.async_abort(reason="wrong_account")
|
|
|
|
|
|
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
|
existing_entry, unique_id=unique_id, data=data
|
2020-07-24 02:02:29 +00:00
|
|
|
|
)
|
2021-07-06 16:21:25 +00:00
|
|
|
|
self.hass.async_create_task(
|
|
|
|
|
self.hass.config_entries.async_reload(existing_entry.entry_id)
|
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
await self.async_set_unique_id(unique_id)
|
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
return self.async_create_entry(title=unique_id, data=data)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
|
async def async_step_reauth(self, config: ConfigType) -> FlowResult:
|
2020-07-24 02:02:29 +00:00
|
|
|
|
"""Handle configuration by re-auth."""
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._username = config.get(CONF_USERNAME)
|
|
|
|
|
self._reauth = True
|
|
|
|
|
return await self.async_step_user()
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
|
async def async_step_user(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
2018-10-12 17:07:47 +00:00
|
|
|
|
"""Handle the start of the config flow."""
|
2021-10-19 20:09:48 +00:00
|
|
|
|
if user_input is None:
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="user",
|
|
|
|
|
errors=self._errors,
|
|
|
|
|
description_placeholders={CONF_URL: self._oauth_values.auth_url},
|
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
|
return await self.async_step_input_auth_code()
|
2020-03-13 05:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimpliSafeOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
|
"""Handle a SimpliSafe options flow."""
|
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
|
def __init__(self, config_entry: ConfigEntry) -> None:
|
2020-03-13 05:00:00 +00:00
|
|
|
|
"""Initialize."""
|
|
|
|
|
self.config_entry = config_entry
|
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
|
async def async_step_init(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
2020-03-13 05:00:00 +00:00
|
|
|
|
"""Manage the options."""
|
|
|
|
|
if user_input is not None:
|
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="init",
|
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
|
{
|
|
|
|
|
vol.Optional(
|
2020-08-27 11:56:20 +00:00
|
|
|
|
CONF_CODE,
|
2020-12-12 09:43:38 +00:00
|
|
|
|
description={
|
|
|
|
|
"suggested_value": self.config_entry.options.get(CONF_CODE)
|
|
|
|
|
},
|
2020-03-13 05:00:00 +00:00
|
|
|
|
): str
|
|
|
|
|
}
|
|
|
|
|
),
|
2018-10-12 17:07:47 +00:00
|
|
|
|
)
|