From 09517668fe79f1269fc20040be039a9671b281ee Mon Sep 17 00:00:00 2001 From: Eduard van Valkenburg Date: Fri, 21 Apr 2023 10:51:49 +0200 Subject: [PATCH] Update pysiaalarm to 3.1.0 (#91500) * updated sia requirements * updates because of changes in package * linting and other small fixes * linting and other small fixes * small release on package that fixes issue with autospec --- homeassistant/components/sia/__init__.py | 3 ++- .../components/sia/alarm_control_panel.py | 4 +++- homeassistant/components/sia/binary_sensor.py | 4 +++- homeassistant/components/sia/hub.py | 20 +++++++++++-------- homeassistant/components/sia/manifest.json | 2 +- .../components/sia/sia_entity_base.py | 2 +- homeassistant/components/sia/utils.py | 18 ++++++++++------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 9 files changed, 35 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/sia/__init__.py b/homeassistant/components/sia/__init__.py index 31ae36f793d..befa2c5df92 100644 --- a/homeassistant/components/sia/__init__.py +++ b/homeassistant/components/sia/__init__.py @@ -16,7 +16,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = hub try: - await hub.sia_client.start(reuse_port=True) + if hub.sia_client: + await hub.sia_client.start(reuse_port=True) except OSError as exc: raise ConfigEntryNotReady( f"SIA Server at port {entry.data[CONF_PORT]} could not start." diff --git a/homeassistant/components/sia/alarm_control_panel.py b/homeassistant/components/sia/alarm_control_panel.py index 25d3f447a09..6a86ce81445 100644 --- a/homeassistant/components/sia/alarm_control_panel.py +++ b/homeassistant/components/sia/alarm_control_panel.py @@ -121,7 +121,9 @@ class SIAAlarmControlPanel(SIABaseEntity, AlarmControlPanelEntity): Return True if the event was relevant for this entity. """ - new_state = self.entity_description.code_consequences.get(sia_event.code) + new_state = None + if sia_event.code: + new_state = self.entity_description.code_consequences[sia_event.code] if new_state is None: return False _LOGGER.debug("New state will be %s", new_state) diff --git a/homeassistant/components/sia/binary_sensor.py b/homeassistant/components/sia/binary_sensor.py index d060af133a5..715fa26eee9 100644 --- a/homeassistant/components/sia/binary_sensor.py +++ b/homeassistant/components/sia/binary_sensor.py @@ -130,7 +130,9 @@ class SIABinarySensor(SIABaseEntity, BinarySensorEntity): Return True if the event was relevant for this entity. """ - new_state = self.entity_description.code_consequences.get(sia_event.code) + new_state = None + if sia_event.code: + new_state = self.entity_description.code_consequences[sia_event.code] if new_state is None: return False _LOGGER.debug("New state will be %s", new_state) diff --git a/homeassistant/components/sia/hub.py b/homeassistant/components/sia/hub.py index 2c2fb0d2be9..fb8d20e1830 100644 --- a/homeassistant/components/sia/hub.py +++ b/homeassistant/components/sia/hub.py @@ -47,7 +47,7 @@ class SIAHub: self._accounts: list[dict[str, Any]] = deepcopy(entry.data[CONF_ACCOUNTS]) self._protocol: str = entry.data[CONF_PROTOCOL] self.sia_accounts: list[SIAAccount] | None = None - self.sia_client: SIAClient = None + self.sia_client: SIAClient | None = None @callback def async_setup_hub(self) -> None: @@ -70,7 +70,8 @@ class SIAHub: async def async_shutdown(self, _: Event | None = None) -> None: """Shutdown the SIA server.""" - await self.sia_client.stop() + if self.sia_client: + await self.sia_client.stop() async def async_create_and_fire_event(self, event: SIAEvent) -> None: """Create a event on HA dispatcher and then on HA's bus, with the data from the SIAEvent. @@ -108,12 +109,15 @@ class SIAHub: if self.sia_client is not None: self.sia_client.accounts = self.sia_accounts return - self.sia_client = SIAClient( - host="", - port=self._port, - accounts=self.sia_accounts, - function=self.async_create_and_fire_event, - protocol=CommunicationsProtocol(self._protocol), + # the new client class method creates a subclass based on protocol, hence the type ignore + self.sia_client = ( + SIAClient( # pylint: disable=abstract-class-instantiated # type: ignore + host="", + port=self._port, + accounts=self.sia_accounts, + function=self.async_create_and_fire_event, + protocol=CommunicationsProtocol(self._protocol), + ) ) def _load_options(self) -> None: diff --git a/homeassistant/components/sia/manifest.json b/homeassistant/components/sia/manifest.json index 299b2b63bc8..8029aa24cee 100644 --- a/homeassistant/components/sia/manifest.json +++ b/homeassistant/components/sia/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/sia", "iot_class": "local_push", "loggers": ["pysiaalarm"], - "requirements": ["pysiaalarm==3.0.2"] + "requirements": ["pysiaalarm==3.1.1"] } diff --git a/homeassistant/components/sia/sia_entity_base.py b/homeassistant/components/sia/sia_entity_base.py index 439e67a5eb4..7ca48bdc46e 100644 --- a/homeassistant/components/sia/sia_entity_base.py +++ b/homeassistant/components/sia/sia_entity_base.py @@ -126,7 +126,7 @@ class SIABaseEntity(RestoreEntity): then update the availability and schedule the next unavailability check. """ _LOGGER.debug("Received event: %s", sia_event) - if int(sia_event.ri) not in (self.zone, SIA_HUB_ZONE): + if (int(sia_event.ri) if sia_event.ri else 0) not in (self.zone, SIA_HUB_ZONE): return relevant_event = self.update_state(sia_event) diff --git a/homeassistant/components/sia/utils.py b/homeassistant/components/sia/utils.py index cf52122a499..e9db69041d6 100644 --- a/homeassistant/components/sia/utils.py +++ b/homeassistant/components/sia/utils.py @@ -1,10 +1,11 @@ """Helper functions for the SIA integration.""" from __future__ import annotations -from datetime import timedelta +from datetime import datetime, timedelta from typing import Any from pysiaalarm import SIAEvent +from pysiaalarm.utils import MessageTypes from homeassistant.util.dt import utcnow @@ -50,21 +51,24 @@ def get_unavailability_interval(ping: int) -> float: def get_attr_from_sia_event(event: SIAEvent) -> dict[str, Any]: """Create the attributes dict from a SIAEvent.""" + timestamp = event.timestamp if event.timestamp else utcnow() return { ATTR_ZONE: event.ri, ATTR_CODE: event.code, ATTR_MESSAGE: event.message, ATTR_ID: event.id, - ATTR_TIMESTAMP: event.timestamp.isoformat() - if event.timestamp - else utcnow().isoformat(), + ATTR_TIMESTAMP: timestamp.isoformat() + if isinstance(timestamp, datetime) + else timestamp, } def get_event_data_from_sia_event(event: SIAEvent) -> dict[str, Any]: """Create a dict from the SIA Event for the HA Event.""" return { - "message_type": event.message_type.value, + "message_type": event.message_type.value + if isinstance(event.message_type, MessageTypes) + else event.message_type, "receiver": event.receiver, "line": event.line, "account": event.account, @@ -77,8 +81,8 @@ def get_event_data_from_sia_event(event: SIAEvent) -> dict[str, Any]: "message": event.message, "x_data": event.x_data, "timestamp": event.timestamp.isoformat() - if event.timestamp - else utcnow().isoformat(), + if isinstance(event.timestamp, datetime) + else event.timestamp, "event_qualifier": event.event_qualifier, "event_type": event.event_type, "partition": event.partition, diff --git a/requirements_all.txt b/requirements_all.txt index 263f1709b81..77efd341dff 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1954,7 +1954,7 @@ pysesame2==1.0.1 pysher==1.0.7 # homeassistant.components.sia -pysiaalarm==3.0.2 +pysiaalarm==3.1.1 # homeassistant.components.signal_messenger pysignalclirestapi==0.3.18 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 0798df37e55..ef75394703c 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1425,7 +1425,7 @@ pyserial-asyncio==0.6 pyserial==3.5 # homeassistant.components.sia -pysiaalarm==3.0.2 +pysiaalarm==3.1.1 # homeassistant.components.signal_messenger pysignalclirestapi==0.3.18