Followup PR for SIA integration (#51108)
* Updates based on Martin's review * fix strings and cleaned up constants
This commit is contained in:
parent
f0952d3ee8
commit
cede36d91c
9 changed files with 114 additions and 131 deletions
|
@ -2,18 +2,14 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Callable
|
||||
from typing import Any
|
||||
|
||||
from pysiaalarm import SIAEvent
|
||||
|
||||
from homeassistant.components.alarm_control_panel import (
|
||||
ENTITY_ID_FORMAT as ALARM_ENTITY_ID_FORMAT,
|
||||
AlarmControlPanelEntity,
|
||||
)
|
||||
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_PORT,
|
||||
CONF_ZONE,
|
||||
STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_ARMED_CUSTOM_BYPASS,
|
||||
STATE_ALARM_ARMED_NIGHT,
|
||||
|
@ -21,8 +17,10 @@ from homeassistant.const import (
|
|||
STATE_ALARM_TRIGGERED,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
|
||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
@ -33,7 +31,6 @@ from .const import (
|
|||
CONF_PING_INTERVAL,
|
||||
CONF_ZONES,
|
||||
DOMAIN,
|
||||
SIA_ENTITY_ID_FORMAT,
|
||||
SIA_EVENT,
|
||||
SIA_NAME_FORMAT,
|
||||
SIA_UNIQUE_ID_FORMAT_ALARM,
|
||||
|
@ -76,21 +73,17 @@ CODE_CONSEQUENCES: dict[str, StateType] = {
|
|||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: Callable[..., None],
|
||||
) -> bool:
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up SIA alarm_control_panel(s) from a config entry."""
|
||||
async_add_entities(
|
||||
[
|
||||
SIAAlarmControlPanel(entry, account_data, zone)
|
||||
for account_data in entry.data[CONF_ACCOUNTS]
|
||||
for zone in range(
|
||||
1,
|
||||
entry.options[CONF_ACCOUNTS][account_data[CONF_ACCOUNT]][CONF_ZONES]
|
||||
+ 1,
|
||||
)
|
||||
]
|
||||
SIAAlarmControlPanel(entry, account_data, zone)
|
||||
for account_data in entry.data[CONF_ACCOUNTS]
|
||||
for zone in range(
|
||||
1,
|
||||
entry.options[CONF_ACCOUNTS][account_data[CONF_ACCOUNT]][CONF_ZONES] + 1,
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
class SIAAlarmControlPanel(AlarmControlPanelEntity, RestoreEntity):
|
||||
|
@ -111,18 +104,7 @@ class SIAAlarmControlPanel(AlarmControlPanelEntity, RestoreEntity):
|
|||
self._account: str = self._account_data[CONF_ACCOUNT]
|
||||
self._ping_interval: int = self._account_data[CONF_PING_INTERVAL]
|
||||
|
||||
self.entity_id: str = ALARM_ENTITY_ID_FORMAT.format(
|
||||
SIA_ENTITY_ID_FORMAT.format(
|
||||
self._port, self._account, self._zone, DEVICE_CLASS_ALARM
|
||||
)
|
||||
)
|
||||
|
||||
self._attr: dict[str, Any] = {
|
||||
CONF_PORT: self._port,
|
||||
CONF_ACCOUNT: self._account,
|
||||
CONF_ZONE: self._zone,
|
||||
CONF_PING_INTERVAL: f"{self._ping_interval} minute(s)",
|
||||
}
|
||||
self._attr: dict[str, Any] = {}
|
||||
|
||||
self._available: bool = True
|
||||
self._state: StateType = None
|
||||
|
@ -134,16 +116,17 @@ class SIAAlarmControlPanel(AlarmControlPanelEntity, RestoreEntity):
|
|||
|
||||
Overridden from Entity.
|
||||
|
||||
1. start the event listener and add the callback to on_remove
|
||||
1. register the dispatcher and add the callback to on_remove
|
||||
2. get previous state from storage
|
||||
3. if previous state: restore
|
||||
4. if previous state is unavailable: set _available to False and return
|
||||
5. if available: create availability cb
|
||||
"""
|
||||
self.async_on_remove(
|
||||
self.hass.bus.async_listen(
|
||||
event_type=SIA_EVENT.format(self._port, self._account),
|
||||
listener=self.async_handle_event,
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
SIA_EVENT.format(self._port, self._account),
|
||||
self.async_handle_event,
|
||||
)
|
||||
)
|
||||
last_state = await self.async_get_last_state()
|
||||
|
@ -162,14 +145,11 @@ class SIAAlarmControlPanel(AlarmControlPanelEntity, RestoreEntity):
|
|||
if self._cancel_availability_cb:
|
||||
self._cancel_availability_cb()
|
||||
|
||||
async def async_handle_event(self, event: Event) -> None:
|
||||
"""Listen to events for this port and account and update state and attributes.
|
||||
async def async_handle_event(self, sia_event: SIAEvent) -> None:
|
||||
"""Listen to dispatcher events for this port and account and update state and attributes.
|
||||
|
||||
If the port and account combo receives any message it means it is online and can therefore be set to available.
|
||||
"""
|
||||
sia_event: SIAEvent = SIAEvent.from_dict( # pylint: disable=no-member
|
||||
event.data
|
||||
)
|
||||
_LOGGER.debug("Received event: %s", sia_event)
|
||||
if int(sia_event.ri) == self._zone:
|
||||
self._attr.update(get_attr_from_sia_event(sia_event))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue