2019-02-13 20:21:14 +00:00
|
|
|
"""Support for SimpliSafe alarm systems."""
|
2019-03-29 08:38:58 +00:00
|
|
|
import asyncio
|
2018-10-12 17:07:47 +00:00
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-07-29 21:52:30 +00:00
|
|
|
from simplipy import API
|
|
|
|
from simplipy.errors import InvalidCredentialsError, SimplipyError
|
2018-10-12 17:07:47 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_TOKEN,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-07-29 21:52:30 +00:00
|
|
|
from homeassistant.helpers import (
|
2019-07-31 19:25:30 +00:00
|
|
|
aiohttp_client,
|
|
|
|
config_validation as cv,
|
|
|
|
device_registry as dr,
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
2019-07-19 04:07:07 +00:00
|
|
|
from homeassistant.helpers.service import verify_domain_control
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
from .config_flow import configured_instances
|
|
|
|
from .const import DATA_CLIENT, DEFAULT_SCAN_INTERVAL, DOMAIN, TOPIC_UPDATE
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_PIN_LABEL = "label"
|
|
|
|
ATTR_PIN_LABEL_OR_VALUE = "label_or_pin"
|
|
|
|
ATTR_PIN_VALUE = "pin"
|
|
|
|
ATTR_SYSTEM_ID = "system_id"
|
|
|
|
|
|
|
|
CONF_ACCOUNTS = "accounts"
|
|
|
|
|
|
|
|
DATA_LISTENER = "listener"
|
|
|
|
|
|
|
|
SERVICE_REMOVE_PIN_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_SYSTEM_ID): cv.string,
|
|
|
|
vol.Required(ATTR_PIN_LABEL_OR_VALUE): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SERVICE_SET_PIN_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_SYSTEM_ID): cv.string,
|
|
|
|
vol.Required(ATTR_PIN_LABEL): cv.string,
|
|
|
|
vol.Required(ATTR_PIN_VALUE): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_save_refresh_token(hass, config_entry, token):
|
|
|
|
hass.config_entries.async_update_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry, data={**config_entry.data, CONF_TOKEN: token}
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
|
2019-07-17 22:13:03 +00:00
|
|
|
async def async_register_base_station(hass, system, config_entry_id):
|
|
|
|
"""Register a new bridge."""
|
|
|
|
device_registry = await dr.async_get_registry(hass)
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
identifiers={(DOMAIN, system.serial)},
|
|
|
|
manufacturer="SimpliSafe",
|
2019-07-17 22:13:03 +00:00
|
|
|
model=system.version,
|
|
|
|
name=system.address,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
context={"source": SOURCE_IMPORT},
|
2018-10-12 17:07:47 +00:00
|
|
|
data={
|
|
|
|
CONF_USERNAME: account[CONF_USERNAME],
|
|
|
|
CONF_PASSWORD: account[CONF_PASSWORD],
|
|
|
|
CONF_CODE: account.get(CONF_CODE),
|
|
|
|
CONF_SCAN_INTERVAL: account[CONF_SCAN_INTERVAL],
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up SimpliSafe as config entry."""
|
2019-07-19 04:07:07 +00:00
|
|
|
_verify_domain_control = verify_domain_control(hass, DOMAIN)
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
api = await API.login_via_token(config_entry.data[CONF_TOKEN], websession)
|
2018-10-14 20:26:44 +00:00
|
|
|
except InvalidCredentialsError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Invalid credentials provided")
|
2018-10-14 20:26:44 +00:00
|
|
|
return False
|
2018-10-12 17:07:47 +00:00
|
|
|
except SimplipyError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Config entry failed: %s", err)
|
2018-10-12 17:07:47 +00:00
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2019-07-29 21:52:30 +00:00
|
|
|
_async_save_refresh_token(hass, config_entry, api.refresh_token)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2019-07-29 21:52:30 +00:00
|
|
|
systems = await api.get_systems()
|
|
|
|
simplisafe = SimpliSafe(hass, config_entry, systems)
|
2019-07-30 23:23:42 +00:00
|
|
|
await simplisafe.async_update()
|
2019-07-29 21:52:30 +00:00
|
|
|
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = simplisafe
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry, "alarm_control_panel"
|
|
|
|
)
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
async def refresh(event_time):
|
|
|
|
"""Refresh data from the SimpliSafe account."""
|
2019-07-29 21:52:30 +00:00
|
|
|
await simplisafe.async_update()
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Updated data for all SimpliSafe systems")
|
2019-07-30 23:23:42 +00:00
|
|
|
async_dispatcher_send(hass, TOPIC_UPDATE)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DOMAIN][DATA_LISTENER][config_entry.entry_id] = async_track_time_interval(
|
|
|
|
hass, refresh, timedelta(seconds=config_entry.data[CONF_SCAN_INTERVAL])
|
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2019-07-17 22:13:03 +00:00
|
|
|
# Register the base station for each system:
|
|
|
|
for system in systems.values():
|
|
|
|
hass.async_create_task(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_register_base_station(hass, system, config_entry.entry_id)
|
|
|
|
)
|
2019-07-17 22:13:03 +00:00
|
|
|
|
2019-07-19 04:07:07 +00:00
|
|
|
@_verify_domain_control
|
|
|
|
async def remove_pin(call):
|
|
|
|
"""Remove a PIN."""
|
|
|
|
system = systems[int(call.data[ATTR_SYSTEM_ID])]
|
|
|
|
await system.remove_pin(call.data[ATTR_PIN_LABEL_OR_VALUE])
|
|
|
|
|
|
|
|
@_verify_domain_control
|
|
|
|
async def set_pin(call):
|
|
|
|
"""Set a PIN."""
|
|
|
|
system = systems[int(call.data[ATTR_SYSTEM_ID])]
|
2019-07-31 19:25:30 +00:00
|
|
|
await system.set_pin(call.data[ATTR_PIN_LABEL], call.data[ATTR_PIN_VALUE])
|
2019-07-19 04:07:07 +00:00
|
|
|
|
|
|
|
for service, method, schema in [
|
2019-07-31 19:25:30 +00:00
|
|
|
("remove_pin", remove_pin, SERVICE_REMOVE_PIN_SCHEMA),
|
|
|
|
("set_pin", set_pin, SERVICE_SET_PIN_SCHEMA),
|
2019-07-19 04:07:07 +00:00
|
|
|
]:
|
|
|
|
hass.services.async_register(DOMAIN, service, method, schema=schema)
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a SimpliSafe config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, "alarm_control_panel")
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id)
|
|
|
|
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(entry.entry_id)
|
|
|
|
remove_listener()
|
|
|
|
|
|
|
|
return True
|
2019-07-29 21:52:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SimpliSafe:
|
|
|
|
"""Define a SimpliSafe API object."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config_entry, systems):
|
|
|
|
"""Initialize."""
|
|
|
|
self._config_entry = config_entry
|
|
|
|
self._hass = hass
|
2019-07-30 23:23:42 +00:00
|
|
|
self.last_event_data = {}
|
2019-07-29 21:52:30 +00:00
|
|
|
self.systems = systems
|
|
|
|
|
2019-07-30 23:23:42 +00:00
|
|
|
async def _update_system(self, system):
|
|
|
|
"""Update a system."""
|
|
|
|
try:
|
|
|
|
await system.update()
|
|
|
|
latest_event = await system.get_latest_event()
|
|
|
|
except SimplipyError as err:
|
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
'SimpliSafe error while updating "%s": %s', system.address, err
|
|
|
|
)
|
2019-07-30 23:23:42 +00:00
|
|
|
return
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error('Unknown error while updating "%s": %s', system.address, err)
|
2019-07-30 23:23:42 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.last_event_data[system.system_id] = latest_event
|
|
|
|
|
|
|
|
if system.api.refresh_token_dirty:
|
|
|
|
_async_save_refresh_token(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hass, self._config_entry, system.api.refresh_token
|
|
|
|
)
|
2019-07-30 23:23:42 +00:00
|
|
|
|
2019-07-29 21:52:30 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Get updated data from SimpliSafe."""
|
2019-07-31 19:25:30 +00:00
|
|
|
tasks = [self._update_system(system) for system in self.systems.values()]
|
2019-07-30 23:23:42 +00:00
|
|
|
|
|
|
|
await asyncio.gather(*tasks)
|