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
|
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
import asyncio
|
|
|
|
|
from typing import Any
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
import async_timeout
|
2021-10-19 20:09:48 +00:00
|
|
|
|
from simplipy import API
|
2022-04-27 08:16:28 +00:00
|
|
|
|
from simplipy.api import AuthStates
|
|
|
|
|
from simplipy.errors import InvalidCredentialsError, SimplipyError, Verify2FAPending
|
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
|
2022-04-27 08:16:28 +00:00
|
|
|
|
from homeassistant.const import CONF_CODE, CONF_PASSWORD, CONF_TOKEN, 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
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
from .const import DOMAIN, LOGGER
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
DEFAULT_EMAIL_2FA_SLEEP = 3
|
2022-05-18 23:27:58 +00:00
|
|
|
|
DEFAULT_EMAIL_2FA_TIMEOUT = 600
|
2021-12-09 22:41:13 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
STEP_REAUTH_SCHEMA = vol.Schema(
|
|
|
|
|
{
|
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
|
}
|
2021-12-09 22:41:13 +00:00
|
|
|
|
)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
STEP_SMS_2FA_SCHEMA = vol.Schema(
|
2020-12-12 09:43:38 +00:00
|
|
|
|
{
|
2022-04-27 08:16:28 +00:00
|
|
|
|
vol.Required(CONF_CODE): cv.string,
|
2020-12-12 09:43:38 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
STEP_USER_SCHEMA = vol.Schema(
|
|
|
|
|
{
|
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
|
}
|
|
|
|
|
)
|
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."""
|
2022-04-28 21:05:55 +00:00
|
|
|
|
self._email_2fa_task: asyncio.Task | None = None
|
2022-04-27 08:16:28 +00:00
|
|
|
|
self._password: str | None = None
|
2021-10-19 20:09:48 +00:00
|
|
|
|
self._reauth: bool = False
|
2022-04-27 08:16:28 +00:00
|
|
|
|
self._simplisafe: API | None = None
|
2021-07-27 20:11:54 +00:00
|
|
|
|
self._username: str | None = None
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
async def _async_authenticate(
|
2022-04-28 21:05:55 +00:00
|
|
|
|
self, originating_step_id: str, originating_step_schema: vol.Schema
|
2021-10-19 20:09:48 +00:00
|
|
|
|
) -> FlowResult:
|
2022-04-27 08:16:28 +00:00
|
|
|
|
"""Attempt to authenticate to the SimpliSafe API."""
|
|
|
|
|
assert self._password
|
|
|
|
|
assert self._username
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2021-12-09 22:11:41 +00:00
|
|
|
|
errors = {}
|
2021-10-19 20:09:48 +00:00
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2022-04-27 08:16:28 +00:00
|
|
|
|
self._simplisafe = await API.async_from_credentials(
|
|
|
|
|
self._username, self._password, session=session
|
2021-10-19 20:09:48 +00:00
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
except InvalidCredentialsError:
|
2021-12-09 22:11:41 +00:00
|
|
|
|
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-12-09 22:11:41 +00:00
|
|
|
|
errors = {"base": "unknown"}
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
2021-12-09 22:11:41 +00:00
|
|
|
|
if errors:
|
2022-04-27 08:16:28 +00:00
|
|
|
|
return self.async_show_form(
|
2022-04-28 21:05:55 +00:00
|
|
|
|
step_id=originating_step_id,
|
|
|
|
|
data_schema=originating_step_schema,
|
2022-04-27 08:16:28 +00:00
|
|
|
|
errors=errors,
|
|
|
|
|
description_placeholders={CONF_USERNAME: self._username},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert self._simplisafe
|
|
|
|
|
|
|
|
|
|
if self._simplisafe.auth_state == AuthStates.PENDING_2FA_SMS:
|
|
|
|
|
return await self.async_step_sms_2fa()
|
2022-04-28 21:05:55 +00:00
|
|
|
|
return await self.async_step_email_2fa()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@callback
|
|
|
|
|
def async_get_options_flow(
|
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
|
) -> SimpliSafeOptionsFlowHandler:
|
|
|
|
|
"""Define the config flow to handle options."""
|
|
|
|
|
return SimpliSafeOptionsFlowHandler(config_entry)
|
|
|
|
|
|
|
|
|
|
async def async_step_reauth(self, config: dict[str, Any]) -> FlowResult:
|
|
|
|
|
"""Handle configuration by re-auth."""
|
|
|
|
|
self._reauth = True
|
|
|
|
|
|
|
|
|
|
if CONF_USERNAME not in config:
|
|
|
|
|
# Old versions of the config flow may not have the username by this point;
|
|
|
|
|
# in that case, we reauth them by making them go through the user flow:
|
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
|
|
self._username = config[CONF_USERNAME]
|
|
|
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
|
|
|
|
|
|
async def _async_get_email_2fa(self) -> None:
|
|
|
|
|
"""Define a task to wait for email-based 2FA."""
|
|
|
|
|
assert self._simplisafe
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
async with async_timeout.timeout(DEFAULT_EMAIL_2FA_TIMEOUT):
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
await self._simplisafe.async_verify_2fa_email()
|
|
|
|
|
except Verify2FAPending:
|
|
|
|
|
LOGGER.info("Email-based 2FA pending; trying again")
|
|
|
|
|
await asyncio.sleep(DEFAULT_EMAIL_2FA_SLEEP)
|
|
|
|
|
else:
|
|
|
|
|
break
|
2022-04-28 21:05:55 +00:00
|
|
|
|
finally:
|
|
|
|
|
self.hass.async_create_task(
|
|
|
|
|
self.hass.config_entries.flow.async_configure(flow_id=self.flow_id)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def async_step_email_2fa(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle email-based two-factor authentication."""
|
|
|
|
|
if not self._email_2fa_task:
|
|
|
|
|
self._email_2fa_task = self.hass.async_create_task(
|
|
|
|
|
self._async_get_email_2fa()
|
|
|
|
|
)
|
|
|
|
|
return self.async_show_progress(
|
|
|
|
|
step_id="email_2fa", progress_action="email_2fa"
|
2022-04-27 08:16:28 +00:00
|
|
|
|
)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
2022-04-28 21:05:55 +00:00
|
|
|
|
try:
|
|
|
|
|
await self._email_2fa_task
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
return self.async_show_progress_done(next_step_id="email_2fa_error")
|
|
|
|
|
return self.async_show_progress_done(next_step_id="finish")
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
2022-04-28 21:05:55 +00:00
|
|
|
|
async def async_step_email_2fa_error(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle an error during email-based two-factor authentication."""
|
|
|
|
|
return self.async_abort(reason="email_2fa_timed_out")
|
|
|
|
|
|
|
|
|
|
async def async_step_finish(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle the final step."""
|
2022-04-27 08:16:28 +00:00
|
|
|
|
assert self._simplisafe
|
|
|
|
|
assert self._username
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
CONF_USERNAME: self._username,
|
|
|
|
|
CONF_TOKEN: self._simplisafe.refresh_token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user_id = str(self._simplisafe.user_id)
|
2021-10-19 20:09:48 +00:00
|
|
|
|
|
|
|
|
|
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 –
|
2022-04-27 08:16:28 +00:00
|
|
|
|
# only one can exist at a time, but the presence of either one is a
|
|
|
|
|
# candidate for re-auth:
|
|
|
|
|
if existing_entries := [
|
|
|
|
|
entry
|
|
|
|
|
for entry in self.hass.config_entries.async_entries()
|
2022-04-28 18:45:37 +00:00
|
|
|
|
if entry.domain == DOMAIN
|
|
|
|
|
and entry.unique_id in (self._username, user_id)
|
2022-04-27 08:16:28 +00:00
|
|
|
|
]:
|
|
|
|
|
existing_entry = existing_entries[0]
|
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
|
existing_entry, unique_id=user_id, title=self._username, data=data
|
|
|
|
|
)
|
|
|
|
|
self.hass.async_create_task(
|
|
|
|
|
self.hass.config_entries.async_reload(existing_entry.entry_id)
|
|
|
|
|
)
|
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
|
|
|
|
|
|
await self.async_set_unique_id(user_id)
|
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
return self.async_create_entry(title=self._username, data=data)
|
|
|
|
|
|
|
|
|
|
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=STEP_REAUTH_SCHEMA,
|
|
|
|
|
description_placeholders={CONF_USERNAME: self._username},
|
2020-07-24 02:02:29 +00:00
|
|
|
|
)
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
|
return await self._async_authenticate("reauth_confirm", STEP_REAUTH_SCHEMA)
|
|
|
|
|
|
|
|
|
|
async def async_step_sms_2fa(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle SMS-based two-factor authentication."""
|
|
|
|
|
if not user_input:
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="sms_2fa",
|
|
|
|
|
data_schema=STEP_SMS_2FA_SCHEMA,
|
2021-07-06 16:21:25 +00:00
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
|
assert self._simplisafe
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await self._simplisafe.async_verify_2fa_sms(user_input[CONF_CODE])
|
|
|
|
|
except InvalidCredentialsError:
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="sms_2fa",
|
|
|
|
|
data_schema=STEP_SMS_2FA_SCHEMA,
|
|
|
|
|
errors={CONF_CODE: "invalid_auth"},
|
|
|
|
|
)
|
|
|
|
|
|
2022-04-28 21:05:55 +00:00
|
|
|
|
return await self.async_step_finish()
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
|
) -> FlowResult:
|
|
|
|
|
"""Handle the start of the config flow."""
|
|
|
|
|
if user_input is None:
|
|
|
|
|
return self.async_show_form(step_id="user", data_schema=STEP_USER_SCHEMA)
|
|
|
|
|
|
|
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
|
return await self._async_authenticate("user", STEP_USER_SCHEMA)
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
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
|
|
|
|
)
|