2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure alarm control panels."""
|
2021-03-05 23:37:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
import asyncio
|
2021-03-15 22:59:41 +00:00
|
|
|
from typing import Any, Callable, Iterable
|
2015-09-13 05:42:38 +00:00
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.components.alarm_control_panel import (
|
|
|
|
FORMAT_NUMBER,
|
|
|
|
AlarmControlPanelEntity,
|
|
|
|
)
|
2019-11-25 23:42:53 +00:00
|
|
|
from homeassistant.components.alarm_control_panel.const import (
|
|
|
|
SUPPORT_ALARM_ARM_AWAY,
|
|
|
|
SUPPORT_ALARM_ARM_HOME,
|
|
|
|
)
|
2021-03-15 19:30:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2015-09-13 18:21:02 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
|
|
|
STATE_ALARM_DISARMED,
|
2021-03-11 18:41:01 +00:00
|
|
|
STATE_ALARM_PENDING,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-11 18:41:01 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2015-09-13 05:42:38 +00:00
|
|
|
|
2021-03-15 19:30:44 +00:00
|
|
|
from .const import CONF_GIID, DOMAIN, LOGGER
|
2021-03-14 09:38:09 +00:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2015-09-13 05:42:38 +00:00
|
|
|
|
|
|
|
|
2021-03-15 19:30:44 +00:00
|
|
|
async def async_setup_entry(
|
2021-03-05 23:37:56 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-15 19:30:44 +00:00
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[Iterable[Entity]], None],
|
2021-03-05 23:37:56 +00:00
|
|
|
) -> None:
|
2021-03-15 19:30:44 +00:00
|
|
|
"""Set up Verisure alarm control panel from a config entry."""
|
|
|
|
async_add_entities([VerisureAlarm(coordinator=hass.data[DOMAIN][entry.entry_id])])
|
2015-09-13 05:42:38 +00:00
|
|
|
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a Verisure alarm status."""
|
2016-03-07 19:21:43 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(self, coordinator: VerisureDataUpdateCoordinator) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the Verisure alarm panel."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2015-09-13 05:42:38 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2021-03-16 15:30:04 +00:00
|
|
|
"""Return the name of the entity."""
|
2021-03-15 19:30:44 +00:00
|
|
|
return "Verisure Alarm"
|
2019-05-23 17:27:42 +00:00
|
|
|
|
2021-03-15 19:30:44 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2021-03-16 15:30:04 +00:00
|
|
|
"""Return the unique ID for this entity."""
|
2021-03-15 19:30:44 +00:00
|
|
|
return self.coordinator.entry.data[CONF_GIID]
|
2015-09-13 05:42:38 +00:00
|
|
|
|
2021-03-15 22:59:41 +00:00
|
|
|
@property
|
|
|
|
def device_info(self) -> dict[str, Any]:
|
|
|
|
"""Return device information about this entity."""
|
|
|
|
return {
|
|
|
|
"name": "Verisure Alarm",
|
|
|
|
"manufacturer": "Verisure",
|
|
|
|
"model": "VBox",
|
|
|
|
"identifiers": {(DOMAIN, self.coordinator.entry.data[CONF_GIID])},
|
|
|
|
}
|
|
|
|
|
2015-09-13 05:42:38 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def state(self) -> str | None:
|
2021-03-16 15:30:04 +00:00
|
|
|
"""Return the state of the entity."""
|
2021-03-14 09:38:09 +00:00
|
|
|
status = self.coordinator.data["alarm"]["statusType"]
|
2021-03-11 18:41:01 +00:00
|
|
|
if status == "DISARMED":
|
|
|
|
self._state = STATE_ALARM_DISARMED
|
|
|
|
elif status == "ARMED_HOME":
|
|
|
|
self._state = STATE_ALARM_ARMED_HOME
|
|
|
|
elif status == "ARMED_AWAY":
|
|
|
|
self._state = STATE_ALARM_ARMED_AWAY
|
|
|
|
elif status == "PENDING":
|
|
|
|
self._state = STATE_ALARM_PENDING
|
|
|
|
else:
|
|
|
|
LOGGER.error("Unknown alarm state %s", status)
|
|
|
|
|
2015-09-14 15:33:43 +00:00
|
|
|
return self._state
|
|
|
|
|
2019-11-25 23:42:53 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Return the list of supported features."""
|
|
|
|
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
|
|
|
|
|
2015-09-19 22:22:37 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def code_format(self) -> str:
|
2018-05-29 05:50:27 +00:00
|
|
|
"""Return one or more digits/characters."""
|
2021-03-05 23:37:56 +00:00
|
|
|
return FORMAT_NUMBER
|
2015-09-19 22:22:37 +00:00
|
|
|
|
2016-08-08 16:00:20 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def changed_by(self) -> str | None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the last change triggered by."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return self.coordinator.data["alarm"]["name"]
|
2021-03-11 18:41:01 +00:00
|
|
|
|
|
|
|
async def _async_set_arm_state(self, state: str, code: str | None = None) -> None:
|
|
|
|
"""Send set arm state command."""
|
|
|
|
arm_state = await self.hass.async_add_executor_job(
|
2021-03-14 09:38:09 +00:00
|
|
|
self.coordinator.verisure.set_arm_state, code, state
|
2021-03-11 18:41:01 +00:00
|
|
|
)
|
|
|
|
LOGGER.debug("Verisure set arm state %s", state)
|
|
|
|
transaction = {}
|
|
|
|
while "result" not in transaction:
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
transaction = await self.hass.async_add_executor_job(
|
2021-03-14 09:38:09 +00:00
|
|
|
self.coordinator.verisure.get_arm_state_transaction,
|
2021-03-11 18:41:01 +00:00
|
|
|
arm_state["armStateChangeTransactionId"],
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
|
|
|
async def async_alarm_disarm(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send disarm command."""
|
2021-03-11 18:41:01 +00:00
|
|
|
await self._async_set_arm_state("DISARMED", code)
|
2015-09-13 18:21:02 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
async def async_alarm_arm_home(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm home command."""
|
2021-03-11 18:41:01 +00:00
|
|
|
await self._async_set_arm_state("ARMED_HOME", code)
|
2015-09-13 18:21:02 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm away command."""
|
2021-03-11 18:41:01 +00:00
|
|
|
await self._async_set_arm_state("ARMED_AWAY", code)
|