2019-02-13 20:21:14 +00:00
|
|
|
"""Support for SimpliSafe alarm control panels."""
|
2021-07-27 20:11:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
from simplipy.errors import SimplipyError
|
2019-07-30 23:23:42 +00:00
|
|
|
from simplipy.system import SystemStates
|
2021-11-15 10:23:25 +00:00
|
|
|
from simplipy.system.v3 import SystemV3
|
2021-10-25 22:45:13 +00:00
|
|
|
from simplipy.websocket import (
|
|
|
|
EVENT_ALARM_CANCELED,
|
|
|
|
EVENT_ALARM_TRIGGERED,
|
|
|
|
EVENT_ARMED_AWAY,
|
|
|
|
EVENT_ARMED_AWAY_BY_KEYPAD,
|
|
|
|
EVENT_ARMED_AWAY_BY_REMOTE,
|
|
|
|
EVENT_ARMED_HOME,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_KEYPAD,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_REMOTE,
|
|
|
|
EVENT_DISARMED_BY_MASTER_PIN,
|
|
|
|
EVENT_DISARMED_BY_REMOTE,
|
|
|
|
EVENT_HOME_EXIT_DELAY,
|
|
|
|
WebsocketEvent,
|
|
|
|
)
|
2019-07-30 23:23:42 +00:00
|
|
|
|
|
|
|
from homeassistant.components.alarm_control_panel import (
|
2020-04-25 16:05:28 +00:00
|
|
|
AlarmControlPanelEntity,
|
2022-04-07 07:38:14 +00:00
|
|
|
AlarmControlPanelEntityFeature,
|
2022-04-18 17:37:32 +00:00
|
|
|
CodeFormat,
|
2019-11-25 23:42:53 +00:00
|
|
|
)
|
2021-07-27 20:11:54 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2016-07-02 18:21:15 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE,
|
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
2020-01-26 01:41:49 +00:00
|
|
|
STATE_ALARM_ARMING,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_ALARM_DISARMED,
|
2021-11-22 19:24:51 +00:00
|
|
|
STATE_ALARM_PENDING,
|
2020-01-26 01:41:49 +00:00
|
|
|
STATE_ALARM_TRIGGERED,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-07-27 20:11:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-12-20 12:35:45 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-07-27 20:11:54 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
from . import SimpliSafe, SimpliSafeEntity
|
2020-02-13 19:30:38 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_ALARM_DURATION,
|
|
|
|
ATTR_ALARM_VOLUME,
|
|
|
|
ATTR_CHIME_VOLUME,
|
|
|
|
ATTR_ENTRY_DELAY_AWAY,
|
|
|
|
ATTR_ENTRY_DELAY_HOME,
|
|
|
|
ATTR_EXIT_DELAY_AWAY,
|
|
|
|
ATTR_EXIT_DELAY_HOME,
|
|
|
|
ATTR_LIGHT,
|
|
|
|
ATTR_VOICE_PROMPT_VOLUME,
|
|
|
|
DOMAIN,
|
2020-10-17 19:40:34 +00:00
|
|
|
LOGGER,
|
2020-02-13 19:30:38 +00:00
|
|
|
)
|
2021-12-02 19:07:14 +00:00
|
|
|
from .typing import SystemType
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BATTERY_BACKUP_POWER_LEVEL = "battery_backup_power_level"
|
|
|
|
ATTR_GSM_STRENGTH = "gsm_strength"
|
2020-02-13 19:30:38 +00:00
|
|
|
ATTR_PIN_NAME = "pin_name"
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_RF_JAMMING = "rf_jamming"
|
|
|
|
ATTR_WALL_POWER_LEVEL = "wall_power_level"
|
|
|
|
ATTR_WIFI_STRENGTH = "wifi_strength"
|
|
|
|
|
2021-10-25 22:45:13 +00:00
|
|
|
STATE_MAP_FROM_REST_API = {
|
2021-11-15 10:23:25 +00:00
|
|
|
SystemStates.ALARM: STATE_ALARM_TRIGGERED,
|
2021-11-22 19:24:51 +00:00
|
|
|
SystemStates.ALARM_COUNT: STATE_ALARM_PENDING,
|
2021-11-15 10:23:25 +00:00
|
|
|
SystemStates.AWAY: STATE_ALARM_ARMED_AWAY,
|
|
|
|
SystemStates.AWAY_COUNT: STATE_ALARM_ARMING,
|
|
|
|
SystemStates.EXIT_DELAY: STATE_ALARM_ARMING,
|
|
|
|
SystemStates.HOME: STATE_ALARM_ARMED_HOME,
|
|
|
|
SystemStates.OFF: STATE_ALARM_DISARMED,
|
2021-10-25 22:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATE_MAP_FROM_WEBSOCKET_EVENT = {
|
|
|
|
EVENT_ALARM_CANCELED: STATE_ALARM_DISARMED,
|
|
|
|
EVENT_ALARM_TRIGGERED: STATE_ALARM_TRIGGERED,
|
|
|
|
EVENT_ARMED_AWAY: STATE_ALARM_ARMED_AWAY,
|
|
|
|
EVENT_ARMED_AWAY_BY_KEYPAD: STATE_ALARM_ARMED_AWAY,
|
|
|
|
EVENT_ARMED_AWAY_BY_REMOTE: STATE_ALARM_ARMED_AWAY,
|
|
|
|
EVENT_ARMED_HOME: STATE_ALARM_ARMED_HOME,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_KEYPAD: STATE_ALARM_ARMING,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_REMOTE: STATE_ALARM_ARMING,
|
|
|
|
EVENT_DISARMED_BY_MASTER_PIN: STATE_ALARM_DISARMED,
|
|
|
|
EVENT_DISARMED_BY_REMOTE: STATE_ALARM_DISARMED,
|
|
|
|
EVENT_HOME_EXIT_DELAY: STATE_ALARM_ARMING,
|
|
|
|
}
|
|
|
|
|
|
|
|
WEBSOCKET_EVENTS_TO_LISTEN_FOR = (
|
|
|
|
EVENT_ALARM_CANCELED,
|
|
|
|
EVENT_ALARM_TRIGGERED,
|
|
|
|
EVENT_ARMED_AWAY,
|
|
|
|
EVENT_ARMED_AWAY_BY_KEYPAD,
|
|
|
|
EVENT_ARMED_AWAY_BY_REMOTE,
|
|
|
|
EVENT_ARMED_HOME,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_KEYPAD,
|
|
|
|
EVENT_AWAY_EXIT_DELAY_BY_REMOTE,
|
|
|
|
EVENT_DISARMED_BY_MASTER_PIN,
|
|
|
|
EVENT_DISARMED_BY_REMOTE,
|
|
|
|
EVENT_HOME_EXIT_DELAY,
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2018-10-12 17:07:47 +00:00
|
|
|
"""Set up a SimpliSafe alarm control panel based on a config entry."""
|
2021-11-01 08:04:00 +00:00
|
|
|
simplisafe = hass.data[DOMAIN][entry.entry_id]
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(
|
2020-03-13 05:00:00 +00:00
|
|
|
[SimpliSafeAlarm(simplisafe, system) for system in simplisafe.systems.values()],
|
2019-07-31 19:25:30 +00:00
|
|
|
True,
|
|
|
|
)
|
2018-07-19 17:13:46 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:05:28 +00:00
|
|
|
class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Representation of a SimpliSafe alarm."""
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2022-04-07 07:38:14 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
AlarmControlPanelEntityFeature.ARM_HOME
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_AWAY
|
|
|
|
)
|
|
|
|
|
2021-12-02 19:07:14 +00:00
|
|
|
def __init__(self, simplisafe: SimpliSafe, system: SystemType) -> None:
|
2016-07-12 14:46:29 +00:00
|
|
|
"""Initialize the SimpliSafe alarm."""
|
2021-10-25 22:45:13 +00:00
|
|
|
super().__init__(
|
|
|
|
simplisafe,
|
|
|
|
system,
|
|
|
|
additional_websocket_events=WEBSOCKET_EVENTS_TO_LISTEN_FOR,
|
|
|
|
)
|
|
|
|
|
2021-10-08 19:22:29 +00:00
|
|
|
if code := self._simplisafe.entry.options.get(CONF_CODE):
|
2021-08-03 14:56:15 +00:00
|
|
|
if code.isdigit():
|
2022-04-18 17:37:32 +00:00
|
|
|
self._attr_code_format = CodeFormat.NUMBER
|
2021-08-03 03:52:44 +00:00
|
|
|
else:
|
2022-04-18 17:37:32 +00:00
|
|
|
self._attr_code_format = CodeFormat.TEXT
|
2022-04-07 07:38:14 +00:00
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
self._last_event = None
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2021-10-28 19:52:06 +00:00
|
|
|
self._set_state_from_system_data()
|
2019-11-25 23:42:53 +00:00
|
|
|
|
2020-03-13 05:00:00 +00:00
|
|
|
@callback
|
2021-07-27 20:11:54 +00:00
|
|
|
def _is_code_valid(self, code: str | None, state: str) -> bool:
|
2020-03-13 05:00:00 +00:00
|
|
|
"""Validate that a code matches the required one."""
|
2021-10-08 19:22:29 +00:00
|
|
|
if not self._simplisafe.entry.options.get(CONF_CODE):
|
2020-03-13 05:00:00 +00:00
|
|
|
return True
|
|
|
|
|
2021-10-08 19:22:29 +00:00
|
|
|
if not code or code != self._simplisafe.entry.options[CONF_CODE]:
|
2020-10-17 19:40:34 +00:00
|
|
|
LOGGER.warning(
|
2020-03-13 05:00:00 +00:00
|
|
|
"Incorrect alarm code entered (target state: %s): %s", state, code
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2021-10-28 19:52:06 +00:00
|
|
|
@callback
|
|
|
|
def _set_state_from_system_data(self) -> None:
|
|
|
|
"""Set the state based on the latest REST API data."""
|
|
|
|
if self._system.alarm_going_off:
|
|
|
|
self._attr_state = STATE_ALARM_TRIGGERED
|
2021-11-22 04:32:03 +00:00
|
|
|
elif self._system.state == SystemStates.ERROR:
|
|
|
|
self.async_increment_error_count()
|
2021-10-28 19:52:06 +00:00
|
|
|
elif state := STATE_MAP_FROM_REST_API.get(self._system.state):
|
|
|
|
self._attr_state = state
|
2021-11-22 04:32:03 +00:00
|
|
|
self.async_reset_error_count()
|
2021-10-28 19:52:06 +00:00
|
|
|
else:
|
|
|
|
LOGGER.error("Unknown system state (REST API): %s", self._system.state)
|
2021-11-22 04:32:03 +00:00
|
|
|
self.async_increment_error_count()
|
2021-10-28 19:52:06 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
async def async_alarm_disarm(self, code: str | None = None) -> None:
|
2016-07-02 18:21:15 +00:00
|
|
|
"""Send disarm command."""
|
2020-03-13 05:00:00 +00:00
|
|
|
if not self._is_code_valid(code, STATE_ALARM_DISARMED):
|
2016-07-07 04:55:47 +00:00
|
|
|
return
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
try:
|
2021-10-19 20:09:48 +00:00
|
|
|
await self._system.async_set_off()
|
2020-02-13 19:30:38 +00:00
|
|
|
except SimplipyError as err:
|
2021-12-20 12:35:45 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f'Error while disarming "{self._system.system_id}": {err}'
|
|
|
|
) from err
|
2020-02-13 19:30:38 +00:00
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_state = STATE_ALARM_DISARMED
|
2021-03-11 14:22:35 +00:00
|
|
|
self.async_write_ha_state()
|
2018-10-04 03:03:29 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
async def async_alarm_arm_home(self, code: str | None = None) -> None:
|
2016-07-02 18:21:15 +00:00
|
|
|
"""Send arm home command."""
|
2020-03-13 05:00:00 +00:00
|
|
|
if not self._is_code_valid(code, STATE_ALARM_ARMED_HOME):
|
2016-07-07 04:55:47 +00:00
|
|
|
return
|
2016-07-02 18:21:15 +00:00
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
try:
|
2021-10-19 20:09:48 +00:00
|
|
|
await self._system.async_set_home()
|
2020-02-13 19:30:38 +00:00
|
|
|
except SimplipyError as err:
|
2021-12-20 12:35:45 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f'Error while arming (home) "{self._system.system_id}": {err}'
|
|
|
|
) from err
|
2020-02-13 19:30:38 +00:00
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_state = STATE_ALARM_ARMED_HOME
|
2021-03-11 14:22:35 +00:00
|
|
|
self.async_write_ha_state()
|
2018-10-04 03:03:29 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
2016-07-02 18:21:15 +00:00
|
|
|
"""Send arm away command."""
|
2020-03-13 05:00:00 +00:00
|
|
|
if not self._is_code_valid(code, STATE_ALARM_ARMED_AWAY):
|
2016-07-07 04:55:47 +00:00
|
|
|
return
|
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
try:
|
2021-10-19 20:09:48 +00:00
|
|
|
await self._system.async_set_away()
|
2020-02-13 19:30:38 +00:00
|
|
|
except SimplipyError as err:
|
2021-12-20 12:35:45 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f'Error while arming (away) "{self._system.system_id}": {err}'
|
|
|
|
) from err
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_state = STATE_ALARM_ARMING
|
2021-03-11 14:22:35 +00:00
|
|
|
self.async_write_ha_state()
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2020-02-13 19:30:38 +00:00
|
|
|
@callback
|
2021-07-27 20:11:54 +00:00
|
|
|
def async_update_from_rest_api(self) -> None:
|
2020-02-13 19:30:38 +00:00
|
|
|
"""Update the entity with the provided REST API data."""
|
2021-07-27 20:11:54 +00:00
|
|
|
if isinstance(self._system, SystemV3):
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2020-02-13 19:30:38 +00:00
|
|
|
{
|
|
|
|
ATTR_ALARM_DURATION: self._system.alarm_duration,
|
2021-11-15 10:23:25 +00:00
|
|
|
ATTR_ALARM_VOLUME: self._system.alarm_volume.name.lower(),
|
2020-02-13 19:30:38 +00:00
|
|
|
ATTR_BATTERY_BACKUP_POWER_LEVEL: self._system.battery_backup_power_level,
|
2021-11-15 10:23:25 +00:00
|
|
|
ATTR_CHIME_VOLUME: self._system.chime_volume.name.lower(),
|
2020-02-13 19:30:38 +00:00
|
|
|
ATTR_ENTRY_DELAY_AWAY: self._system.entry_delay_away,
|
|
|
|
ATTR_ENTRY_DELAY_HOME: self._system.entry_delay_home,
|
|
|
|
ATTR_EXIT_DELAY_AWAY: self._system.exit_delay_away,
|
|
|
|
ATTR_EXIT_DELAY_HOME: self._system.exit_delay_home,
|
|
|
|
ATTR_GSM_STRENGTH: self._system.gsm_strength,
|
|
|
|
ATTR_LIGHT: self._system.light,
|
|
|
|
ATTR_RF_JAMMING: self._system.rf_jamming,
|
2021-11-15 10:23:25 +00:00
|
|
|
ATTR_VOICE_PROMPT_VOLUME: self._system.voice_prompt_volume.name.lower(),
|
2020-02-13 19:30:38 +00:00
|
|
|
ATTR_WALL_POWER_LEVEL: self._system.wall_power_level,
|
|
|
|
ATTR_WIFI_STRENGTH: self._system.wifi_strength,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-28 19:52:06 +00:00
|
|
|
self._set_state_from_system_data()
|
2021-10-25 22:45:13 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_from_websocket_event(self, event: WebsocketEvent) -> None:
|
|
|
|
"""Update the entity when new data comes from the websocket."""
|
|
|
|
self._attr_changed_by = event.changed_by
|
2022-02-05 07:41:40 +00:00
|
|
|
|
|
|
|
assert event.event_type
|
|
|
|
|
2021-11-22 04:32:03 +00:00
|
|
|
if state := STATE_MAP_FROM_WEBSOCKET_EVENT.get(event.event_type):
|
|
|
|
self._attr_state = state
|
|
|
|
self.async_reset_error_count()
|
|
|
|
else:
|
|
|
|
LOGGER.error("Unknown alarm websocket event: %s", event.event_type)
|
|
|
|
self.async_increment_error_count()
|