mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Pre-calculate Verisure alarm states (#48340)
* Pre-calculate Verisure alarm states * Correct super call
This commit is contained in:
parent
72281f4718
commit
da2fecb312
@ -13,17 +13,11 @@ from homeassistant.components.alarm_control_panel.const import (
|
||||
SUPPORT_ALARM_ARM_HOME,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_DISARMED,
|
||||
STATE_ALARM_PENDING,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import CONF_GIID, DOMAIN, LOGGER
|
||||
from .const import ALARM_STATE_TO_HA, CONF_GIID, DOMAIN, LOGGER
|
||||
from .coordinator import VerisureDataUpdateCoordinator
|
||||
|
||||
|
||||
@ -41,10 +35,8 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
||||
|
||||
coordinator: VerisureDataUpdateCoordinator
|
||||
|
||||
def __init__(self, coordinator: VerisureDataUpdateCoordinator) -> None:
|
||||
"""Initialize the Verisure alarm panel."""
|
||||
super().__init__(coordinator)
|
||||
self._state = None
|
||||
_changed_by: str | None = None
|
||||
_state: str | None = None
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@ -69,18 +61,6 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
||||
@property
|
||||
def state(self) -> str | None:
|
||||
"""Return the state of the entity."""
|
||||
status = self.coordinator.data["alarm"]["statusType"]
|
||||
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)
|
||||
|
||||
return self._state
|
||||
|
||||
@property
|
||||
@ -96,7 +76,7 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
||||
@property
|
||||
def changed_by(self) -> str | None:
|
||||
"""Return the last change triggered by."""
|
||||
return self.coordinator.data["alarm"]["name"]
|
||||
return self._changed_by
|
||||
|
||||
async def _async_set_arm_state(self, state: str, code: str | None = None) -> None:
|
||||
"""Send set arm state command."""
|
||||
@ -125,3 +105,17 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
||||
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
||||
"""Send arm away command."""
|
||||
await self._async_set_arm_state("ARMED_AWAY", code)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._state = ALARM_STATE_TO_HA.get(
|
||||
self.coordinator.data["alarm"]["statusType"]
|
||||
)
|
||||
self._changed_by = self.coordinator.data["alarm"]["name"]
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""When entity is added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
self._handle_coordinator_update()
|
||||
|
@ -2,6 +2,13 @@
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_DISARMED,
|
||||
STATE_ALARM_PENDING,
|
||||
)
|
||||
|
||||
DOMAIN = "verisure"
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
@ -31,6 +38,13 @@ DEVICE_TYPE_NAME = {
|
||||
"WATER1": "Water detector",
|
||||
}
|
||||
|
||||
ALARM_STATE_TO_HA = {
|
||||
"DISARMED": STATE_ALARM_DISARMED,
|
||||
"ARMED_HOME": STATE_ALARM_ARMED_HOME,
|
||||
"ARMED_AWAY": STATE_ALARM_ARMED_AWAY,
|
||||
"PENDING": STATE_ALARM_PENDING,
|
||||
}
|
||||
|
||||
# Legacy; to remove after YAML removal
|
||||
CONF_CODE_DIGITS = "code_digits"
|
||||
CONF_DEFAULT_LOCK_CODE = "default_lock_code"
|
||||
|
Loading…
x
Reference in New Issue
Block a user