2018-10-12 17:07:47 +00:00
|
|
|
"""Config flow to configure the SimpliSafe component."""
|
2019-12-01 00:16:05 +00:00
|
|
|
from simplipy import API
|
|
|
|
from simplipy.errors import SimplipyError
|
2018-10-12 17:07:47 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2020-01-17 20:43:41 +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
|
2018-10-12 17:07:47 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
2020-02-25 05:02:20 +00:00
|
|
|
from .const import DOMAIN # pylint: disable=unused-import
|
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
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the config flow."""
|
2020-02-24 20:03:08 +00:00
|
|
|
self.data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
vol.Optional(CONF_CODE): str,
|
|
|
|
}
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
async def _show_form(self, errors=None):
|
|
|
|
"""Show the form to the user."""
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user",
|
2020-02-24 20:03:08 +00:00
|
|
|
data_schema=self.data_schema,
|
2018-10-12 17:07:47 +00:00
|
|
|
errors=errors if errors else {},
|
|
|
|
)
|
|
|
|
|
2020-03-13 05:00:00 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(config_entry):
|
|
|
|
"""Define the config flow to handle options."""
|
|
|
|
return SimpliSafeOptionsFlowHandler(config_entry)
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
async def async_step_import(self, import_config):
|
|
|
|
"""Import a config entry from configuration.yaml."""
|
|
|
|
return await self.async_step_user(import_config)
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the start of the config flow."""
|
|
|
|
if not user_input:
|
|
|
|
return await self._show_form()
|
|
|
|
|
2020-02-24 20:03:08 +00:00
|
|
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
|
|
|
self._abort_if_unique_id_configured()
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
websession = aiohttp_client.async_get_clientsession(self.hass)
|
|
|
|
|
|
|
|
try:
|
|
|
|
simplisafe = await API.login_via_credentials(
|
2020-04-27 16:41:33 +00:00
|
|
|
user_input[CONF_USERNAME], user_input[CONF_PASSWORD], session=websession
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
except SimplipyError:
|
2020-02-24 20:03:08 +00:00
|
|
|
return await self._show_form(errors={"base": "invalid_credentials"})
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_USERNAME],
|
2020-03-13 05:00:00 +00:00
|
|
|
data={
|
|
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
|
|
|
CONF_TOKEN: simplisafe.refresh_token,
|
|
|
|
CONF_CODE: user_input.get(CONF_CODE),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SimpliSafeOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle a SimpliSafe options flow."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry):
|
|
|
|
"""Initialize."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""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(
|
|
|
|
CONF_CODE, default=self.config_entry.options.get(CONF_CODE),
|
|
|
|
): str
|
|
|
|
}
|
|
|
|
),
|
2018-10-12 17:07:47 +00:00
|
|
|
)
|