Add config entry for SimpliSafe (#17148)
* Initial move into component * Base functionality in place * Starting tests in place * All config entry tests in place * Made default scan interval more obvious and removed extra logging * Inherit default scan interval from alarm_control_panel * Updated coveragerc and CODEOWNERS * Member-requested changes * Hound * Updated requirements * Updated tests * Member-requested changes * Owner-requested changes * Constant cleanup * Fixed config flow test * Owner-requested updates * Updated requirements * Using async_will_remove_from_hass * Corrected scan interval logic * Fixed tests * Owner-requested changes * Additional logging * Owner-requested changes
This commit is contained in:
1 parent
1f863830e1
commit
401e22fc0c
14 files changed
+461
-86
No files matched your search
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"config": {
|
||||
"error": {
|
||||
"identifier_exists": "Account already registered",
|
||||
"invalid_credentials": "Invalid credentials"
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"code": "Code (for Home Assistant)",
|
||||
"password": "Password",
|
||||
"username": "Email Address"
|
||||
},
|
||||
"title": "Fill in your information"
|
||||
}
|
||||
},
|
||||
"title": "SimpliSafe"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
"""
|
||||
Support for SimpliSafe alarm systems.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/simplisafe/
|
||||
"""
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import (
|
||||
CONF_CODE, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_USERNAME)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .config_flow import configured_instances
|
||||
from .const import DATA_CLIENT, DEFAULT_SCAN_INTERVAL, DOMAIN, TOPIC_UPDATE
|
||||
|
||||
REQUIREMENTS = ['simplisafe-python==3.1.7']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_ACCOUNTS = 'accounts'
|
||||
|
||||
DATA_LISTENER = 'listener'
|
||||
|
||||
ACCOUNT_CONFIG_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_CODE): cv.string,
|
||||
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL):
|
||||
cv.time_period
|
||||
})
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_ACCOUNTS):
|
||||
vol.All(cv.ensure_list, [ACCOUNT_CONFIG_SCHEMA]),
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
@callback
|
||||
def _async_save_refresh_token(hass, config_entry, token):
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, data={
|
||||
**config_entry.data, CONF_TOKEN: token
|
||||
})
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the SimpliSafe component."""
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][DATA_CLIENT] = {}
|
||||
hass.data[DOMAIN][DATA_LISTENER] = {}
|
||||
|
||||
if DOMAIN not in config:
|
||||
return True
|
||||
|
||||
conf = config[DOMAIN]
|
||||
|
||||
for account in conf[CONF_ACCOUNTS]:
|
||||
if account[CONF_USERNAME] in configured_instances(hass):
|
||||
continue
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={'source': SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_USERNAME: account[CONF_USERNAME],
|
||||
CONF_PASSWORD: account[CONF_PASSWORD],
|
||||
CONF_CODE: account.get(CONF_CODE),
|
||||
CONF_SCAN_INTERVAL: account[CONF_SCAN_INTERVAL],
|
||||
}))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
"""Set up SimpliSafe as config entry."""
|
||||
from simplipy import API
|
||||
from simplipy.errors import SimplipyError
|
||||
|
||||
websession = aiohttp_client.async_get_clientsession(hass)
|
||||
|
||||
try:
|
||||
simplisafe = await API.login_via_token(
|
||||
config_entry.data[CONF_TOKEN], websession)
|
||||
except SimplipyError as err:
|
||||
if 403 in str(err):
|
||||
_LOGGER.error('Invalid credentials provided')
|
||||
return False
|
||||
|
||||
_LOGGER.error('Config entry failed: %s', err)
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
_async_save_refresh_token(hass, config_entry, simplisafe.refresh_token)
|
||||
|
||||
systems = await simplisafe.get_systems()
|
||||
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = systems
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'alarm_control_panel'))
|
||||
|
||||
async def refresh(event_time):
|
||||
"""Refresh data from the SimpliSafe account."""
|
||||
for system in systems:
|
||||
_LOGGER.debug('Updating system data: %s', system.system_id)
|
||||
await system.update()
|
||||
async_dispatcher_send(hass, TOPIC_UPDATE.format(system.system_id))
|
||||
|
||||
if system.api.refresh_token_dirty:
|
||||
_async_save_refresh_token(
|
||||
hass, config_entry, system.api.refresh_token)
|
||||
|
||||
hass.data[DOMAIN][DATA_LISTENER][
|
||||
config_entry.entry_id] = async_track_time_interval(
|
||||
hass,
|
||||
refresh,
|
||||
timedelta(seconds=config_entry.data[CONF_SCAN_INTERVAL]))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass, entry):
|
||||
"""Unload a SimpliSafe config entry."""
|
||||
await hass.config_entries.async_forward_entry_unload(
|
||||
entry, 'alarm_control_panel')
|
||||
|
||||
hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id)
|
||||
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(entry.entry_id)
|
||||
remove_listener()
|
||||
|
||||
return True
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Config flow to configure the SimpliSafe component."""
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import (
|
||||
CONF_CODE, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_USERNAME)
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
|
||||
|
||||
@callback
|
||||
def configured_instances(hass):
|
||||
"""Return a set of configured SimpliSafe instances."""
|
||||
return set(
|
||||
entry.data[CONF_USERNAME]
|
||||
for entry in hass.config_entries.async_entries(DOMAIN))
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(DOMAIN)
|
||||
class SimpliSafeFlowHandler(config_entries.ConfigFlow):
|
||||
"""Handle a SimpliSafe config flow."""
|
||||
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the config flow."""
|
||||
self.data_schema = OrderedDict()
|
||||
self.data_schema[vol.Required(CONF_USERNAME)] = str
|
||||
self.data_schema[vol.Required(CONF_PASSWORD)] = str
|
||||
self.data_schema[vol.Optional(CONF_CODE)] = str
|
||||
|
||||
async def _show_form(self, errors=None):
|
||||
"""Show the form to the user."""
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
data_schema=vol.Schema(self.data_schema),
|
||||
errors=errors if errors else {},
|
||||
)
|
||||
|
||||
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."""
|
||||
from simplipy import API
|
||||
from simplipy.errors import SimplipyError
|
||||
|
||||
if not user_input:
|
||||
return await self._show_form()
|
||||
|
||||
if user_input[CONF_USERNAME] in configured_instances(self.hass):
|
||||
return await self._show_form({CONF_USERNAME: 'identifier_exists'})
|
||||
|
||||
username = user_input[CONF_USERNAME]
|
||||
websession = aiohttp_client.async_get_clientsession(self.hass)
|
||||
|
||||
try:
|
||||
simplisafe = await API.login_via_credentials(
|
||||
username, user_input[CONF_PASSWORD], websession)
|
||||
except SimplipyError:
|
||||
return await self._show_form({'base': 'invalid_credentials'})
|
||||
|
||||
scan_interval = user_input.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_USERNAME],
|
||||
data={
|
||||
CONF_USERNAME: username,
|
||||
CONF_TOKEN: simplisafe.refresh_token,
|
||||
CONF_SCAN_INTERVAL: scan_interval.seconds,
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
"""Define constants for the SimpliSafe component."""
|
||||
from datetime import timedelta
|
||||
|
||||
DOMAIN = 'simplisafe'
|
||||
|
||||
DATA_CLIENT = 'client'
|
||||
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
TOPIC_UPDATE = 'update_{0}'
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"config": {
|
||||
"title": "SimpliSafe",
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Fill in your information",
|
||||
"data": {
|
||||
"username": "Email Address",
|
||||
"password": "Password",
|
||||
"code": "Code (for Home Assistant)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"identifier_exists": "Account already registered",
|
||||
"invalid_credentials": "Invalid credentials"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user