2021-05-24 06:48:28 +00:00
|
|
|
"""Helper functions for the SIA integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from pysiaalarm import SIAEvent
|
|
|
|
|
2021-07-14 18:01:16 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
2022-02-15 14:53:38 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_CODE,
|
|
|
|
ATTR_ID,
|
|
|
|
ATTR_MESSAGE,
|
|
|
|
ATTR_TIMESTAMP,
|
|
|
|
ATTR_ZONE,
|
|
|
|
KEY_ALARM,
|
|
|
|
SIA_HUB_ZONE,
|
|
|
|
)
|
2021-05-24 06:48:28 +00:00
|
|
|
|
2021-05-27 08:55:47 +00:00
|
|
|
PING_INTERVAL_MARGIN = 30
|
2021-05-24 06:48:28 +00:00
|
|
|
|
|
|
|
|
2022-02-15 14:53:38 +00:00
|
|
|
def get_unique_id_and_name(
|
|
|
|
entry_id: str,
|
|
|
|
port: int,
|
|
|
|
account: str,
|
|
|
|
zone: int,
|
|
|
|
entity_key: str,
|
|
|
|
) -> tuple[str, str]:
|
|
|
|
"""Return the unique_id and name for an entity."""
|
|
|
|
return (
|
|
|
|
(
|
|
|
|
f"{entry_id}_{account}_{zone}"
|
|
|
|
if entity_key == KEY_ALARM
|
|
|
|
else f"{entry_id}_{account}_{zone}_{entity_key}"
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f"{port} - {account} - {entity_key}"
|
|
|
|
if zone == SIA_HUB_ZONE
|
|
|
|
else f"{port} - {account} - zone {zone} - {entity_key}"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-24 06:48:28 +00:00
|
|
|
def get_unavailability_interval(ping: int) -> float:
|
|
|
|
"""Return the interval to the next unavailability check."""
|
|
|
|
return timedelta(minutes=ping, seconds=PING_INTERVAL_MARGIN).total_seconds()
|
|
|
|
|
|
|
|
|
|
|
|
def get_attr_from_sia_event(event: SIAEvent) -> dict[str, Any]:
|
|
|
|
"""Create the attributes dict from a SIAEvent."""
|
|
|
|
return {
|
2021-05-27 08:55:47 +00:00
|
|
|
ATTR_ZONE: event.ri,
|
|
|
|
ATTR_CODE: event.code,
|
|
|
|
ATTR_MESSAGE: event.message,
|
|
|
|
ATTR_ID: event.id,
|
2021-07-15 07:31:17 +00:00
|
|
|
ATTR_TIMESTAMP: event.timestamp.isoformat()
|
|
|
|
if event.timestamp
|
|
|
|
else utcnow().isoformat(),
|
2021-05-27 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_event_data_from_sia_event(event: SIAEvent) -> dict[str, Any]:
|
|
|
|
"""Create a dict from the SIA Event for the HA Event."""
|
|
|
|
return {
|
2021-06-01 18:32:17 +00:00
|
|
|
"message_type": event.message_type.value,
|
2021-05-27 08:55:47 +00:00
|
|
|
"receiver": event.receiver,
|
|
|
|
"line": event.line,
|
|
|
|
"account": event.account,
|
|
|
|
"sequence": event.sequence,
|
|
|
|
"content": event.content,
|
|
|
|
"ti": event.ti,
|
|
|
|
"id": event.id,
|
|
|
|
"ri": event.ri,
|
|
|
|
"code": event.code,
|
|
|
|
"message": event.message,
|
|
|
|
"x_data": event.x_data,
|
2021-07-14 18:01:16 +00:00
|
|
|
"timestamp": event.timestamp.isoformat()
|
|
|
|
if event.timestamp
|
|
|
|
else utcnow().isoformat(),
|
2021-06-01 18:32:17 +00:00
|
|
|
"event_qualifier": event.event_qualifier,
|
2021-05-27 08:55:47 +00:00
|
|
|
"event_type": event.event_type,
|
|
|
|
"partition": event.partition,
|
|
|
|
"extended_data": [
|
|
|
|
{
|
|
|
|
"identifier": xd.identifier,
|
|
|
|
"name": xd.name,
|
|
|
|
"description": xd.description,
|
|
|
|
"length": xd.length,
|
|
|
|
"characters": xd.characters,
|
|
|
|
"value": xd.value,
|
|
|
|
}
|
|
|
|
for xd in event.extended_data
|
|
|
|
]
|
|
|
|
if event.extended_data is not None
|
|
|
|
else None,
|
|
|
|
"sia_code": {
|
|
|
|
"code": event.sia_code.code,
|
|
|
|
"type": event.sia_code.type,
|
|
|
|
"description": event.sia_code.description,
|
|
|
|
"concerns": event.sia_code.concerns,
|
|
|
|
}
|
|
|
|
if event.sia_code is not None
|
|
|
|
else None,
|
2021-05-24 06:48:28 +00:00
|
|
|
}
|