core/homeassistant/components/sia/utils.py

75 lines
2.2 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.util.dt import utcnow
from .const import ATTR_CODE, ATTR_ID, ATTR_MESSAGE, ATTR_TIMESTAMP, ATTR_ZONE
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
PING_INTERVAL_MARGIN = 30
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
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 {
ATTR_ZONE: event.ri,
ATTR_CODE: event.code,
ATTR_MESSAGE: event.message,
ATTR_ID: event.id,
ATTR_TIMESTAMP: event.timestamp.isoformat(),
}
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,
"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,
"timestamp": event.timestamp.isoformat()
if event.timestamp
else utcnow().isoformat(),
2021-06-01 18:32:17 +00:00
"event_qualifier": event.event_qualifier,
"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,
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
}