core/homeassistant/components/sia/utils.py

58 lines
1.8 KiB
Python
Raw Normal View History

Add SIA Alarm systems (#36625) * initial commit of SIA integration * translations * moved reactions to file, typed everything * fixed no-else-return 3 times * refactored config and fix coverage of test * fix requirements_test * elimated another platform * forgot some mentions of sensor * updated config flow steps, fixed restore and small edits * fixed pylint * updated config_flow with better schema, small fixes from review * final comment and small legibility enhancements * small fix for pylint * fixed init * fixes for botched rebase * fixed port string * updated common strings * rebuild component with eventbus * fixed pylint and tests * updates based on review by @bdraco * updates based on new version of package and reviews * small updates with latest package * added raise from * deleted async_setup from test * fixed tests * removed unused code from addititional account step * fixed typo in strings * clarification and update to update_data func * added iot_class to manifest * fixed entity and unique id setup * small fix in tests * improved unique_id semantics and load/unload functions * added typing in order to fix mypy * further fixes for typing * final fixes for mypy * adding None return types * fix hub DR identifier * rebased, added DeviceInfo * rewrite to clean up and make it easier to read * replaced functions with format for id and name * renamed tracker remover small fix in state.setter * improved readibility of state.setter * no more state.setter and small updates * mypy fix * fixed and improved config flow * added fixtures to test and other cleaner test code * removed timeband from config, will reintro in a options flow * removed timeband from tests * added options flow for zones and timestamps * removed type ignore * replaced mapping with collections.abc
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
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from .const import (
EVENT_ACCOUNT,
EVENT_CODE,
EVENT_ID,
EVENT_MESSAGE,
EVENT_TIMESTAMP,
EVENT_ZONE,
HUB_SENSOR_NAME,
HUB_ZONE,
PING_INTERVAL_MARGIN,
)
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_name(port: int, account: str, zone: int, entity_type: str) -> str:
"""Give back a entity_id and name according to the variables."""
if zone == HUB_ZONE:
return f"{port} - {account} - {'Last Heartbeat' if entity_type == DEVICE_CLASS_TIMESTAMP else 'Power'}"
return f"{port} - {account} - zone {zone} - {entity_type}"
def get_entity_id(port: int, account: str, zone: int, entity_type: str) -> str:
"""Give back a entity_id according to the variables."""
if zone == HUB_ZONE:
return f"{port}_{account}_{HUB_SENSOR_NAME if entity_type == DEVICE_CLASS_TIMESTAMP else entity_type}"
return f"{port}_{account}_{zone}_{entity_type}"
def get_unique_id(entry_id: str, account: str, zone: int, domain: str) -> str:
"""Return the unique id."""
return f"{entry_id}_{account}_{zone}_{domain}"
def get_attr_from_sia_event(event: SIAEvent) -> dict[str, Any]:
"""Create the attributes dict from a SIAEvent."""
return {
EVENT_ACCOUNT: event.account,
EVENT_ZONE: event.ri,
EVENT_CODE: event.code,
EVENT_MESSAGE: event.message,
EVENT_ID: event.id,
EVENT_TIMESTAMP: event.timestamp,
}