hass-core/homeassistant/components/verisure/alarm_control_panel.py

121 lines
3.7 KiB
Python
Raw Normal View History

"""Support for Verisure alarm control panels."""
from __future__ import annotations
from time import sleep
from typing import Any, Callable
2015-09-13 07:42:38 +02:00
from homeassistant.components.alarm_control_panel import (
FORMAT_NUMBER,
AlarmControlPanelEntity,
)
from homeassistant.components.alarm_control_panel.const import (
SUPPORT_ALARM_ARM_AWAY,
SUPPORT_ALARM_ARM_HOME,
)
2015-09-13 20:21:02 +02:00
from homeassistant.const import (
2019-07-31 12:25:30 -07:00
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME,
STATE_ALARM_DISARMED,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
2015-09-13 07:42:38 +02:00
2020-12-30 09:55:18 +01:00
from . import HUB as hub
from .const import CONF_ALARM, CONF_CODE_DIGITS, CONF_GIID, LOGGER
2015-09-13 07:42:38 +02:00
def setup_platform(
hass: HomeAssistant,
config: dict[str, Any],
add_entities: Callable[[list[Entity], bool], None],
discovery_info: dict[str, Any] | None = None,
) -> None:
"""Set up the Verisure platform."""
2015-09-13 07:42:38 +02:00
alarms = []
if int(hub.config.get(CONF_ALARM, 1)):
hub.update_overview()
alarms.append(VerisureAlarm())
add_entities(alarms)
2015-09-13 07:42:38 +02:00
def set_arm_state(state: str, code: str | None = None) -> None:
"""Send set arm state command."""
transaction_id = hub.session.set_arm_state(code, state)[
2019-07-31 12:25:30 -07:00
"armStateChangeTransactionId"
]
2020-12-30 09:55:18 +01:00
LOGGER.info("verisure set arm state %s", state)
transaction = {}
2019-07-31 12:25:30 -07:00
while "result" not in transaction:
sleep(0.5)
transaction = hub.session.get_arm_state_transaction(transaction_id)
2020-12-30 09:55:18 +01:00
hub.update_overview()
class VerisureAlarm(AlarmControlPanelEntity):
"""Representation of a Verisure alarm status."""
2016-03-07 20:21:43 +01:00
def __init__(self):
"""Initialize the Verisure alarm panel."""
self._state = None
self._digits = hub.config.get(CONF_CODE_DIGITS)
self._changed_by = None
2015-09-13 07:42:38 +02:00
@property
def name(self) -> str:
2016-03-07 20:21:43 +01:00
"""Return the name of the device."""
giid = hub.config.get(CONF_GIID)
if giid is not None:
2019-07-31 12:25:30 -07:00
aliass = {i["giid"]: i["alias"] for i in hub.session.installations}
if giid in aliass:
2019-07-31 12:25:30 -07:00
return "{} alarm".format(aliass[giid])
2020-12-30 09:55:18 +01:00
LOGGER.error("Verisure installation giid not found: %s", giid)
2019-07-31 12:25:30 -07:00
return "{} alarm".format(hub.session.installations[0]["alias"])
2015-09-13 07:42:38 +02:00
@property
def state(self) -> str | None:
2016-03-07 20:21:43 +01:00
"""Return the state of the device."""
2015-09-14 17:33:43 +02:00
return self._state
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
@property
def code_format(self) -> str:
"""Return one or more digits/characters."""
return FORMAT_NUMBER
@property
def changed_by(self) -> str | None:
"""Return the last change triggered by."""
return self._changed_by
def update(self) -> None:
2016-03-07 16:45:21 +01:00
"""Update alarm status."""
hub.update_overview()
status = hub.get_first("$.armState.statusType")
2019-07-31 12:25:30 -07:00
if status == "DISARMED":
2015-09-13 07:42:38 +02:00
self._state = STATE_ALARM_DISARMED
2019-07-31 12:25:30 -07:00
elif status == "ARMED_HOME":
2015-09-13 07:42:38 +02:00
self._state = STATE_ALARM_ARMED_HOME
2019-07-31 12:25:30 -07:00
elif status == "ARMED_AWAY":
2015-09-13 07:42:38 +02:00
self._state = STATE_ALARM_ARMED_AWAY
2019-07-31 12:25:30 -07:00
elif status != "PENDING":
2020-12-30 09:55:18 +01:00
LOGGER.error("Unknown alarm state %s", status)
self._changed_by = hub.get_first("$.armState.name")
2015-09-13 20:21:02 +02:00
def alarm_disarm(self, code: str | None = None) -> None:
2016-03-07 16:45:21 +01:00
"""Send disarm command."""
2019-07-31 12:25:30 -07:00
set_arm_state("DISARMED", code)
2015-09-13 20:21:02 +02:00
def alarm_arm_home(self, code: str | None = None) -> None:
2016-03-07 16:45:21 +01:00
"""Send arm home command."""
2019-07-31 12:25:30 -07:00
set_arm_state("ARMED_HOME", code)
2015-09-13 20:21:02 +02:00
def alarm_arm_away(self, code: str | None = None) -> None:
2016-03-07 16:45:21 +01:00
"""Send arm away command."""
2019-07-31 12:25:30 -07:00
set_arm_state("ARMED_AWAY", code)