From 7cabb39dd0015e4c0e47c25e54b1786dcdd117f2 Mon Sep 17 00:00:00 2001 From: denpamusic Date: Fri, 20 Oct 2023 03:27:56 +0300 Subject: [PATCH] Unpack alert dt before check. Unpack 'to_seconds' and check it with a comparison to zero, instead of using 'check_parameter'. --- pyplumio/structures/alerts.py | 16 +++++++--------- tests/conftest.py | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pyplumio/structures/alerts.py b/pyplumio/structures/alerts.py index 04c1874d..20af4ba4 100644 --- a/pyplumio/structures/alerts.py +++ b/pyplumio/structures/alerts.py @@ -7,7 +7,6 @@ from pyplumio import util from pyplumio.const import AlertType -from pyplumio.helpers.parameter import check_parameter from pyplumio.helpers.typing import EventDataType from pyplumio.structures import StructureDecoder, ensure_device_data @@ -66,14 +65,13 @@ def _unpack_alert(self, message: bytearray) -> Alert: except ValueError: pass - from_dt_bytes = message[self._offset + 1 : self._offset + 5] - to_dt_bytes = message[self._offset + 5 : self._offset + ALERT_SIZE] - from_dt = _convert_to_datetime(util.unpack_uint(from_dt_bytes)[0]) - to_dt = ( - _convert_to_datetime(util.unpack_uint(to_dt_bytes)[0]) - if check_parameter(to_dt_bytes) - else None - ) + from_seconds = util.unpack_uint(message[self._offset + 1 : self._offset + 5])[0] + to_seconds = util.unpack_uint( + message[self._offset + 5 : self._offset + ALERT_SIZE] + )[0] + + from_dt = _convert_to_datetime(from_seconds) + to_dt = _convert_to_datetime(to_seconds) if to_seconds > 0 else None self._offset += ALERT_SIZE return Alert(code, from_dt, to_dt) diff --git a/tests/conftest.py b/tests/conftest.py index 1cf994ae..cb98a2b8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -228,7 +228,7 @@ def fixture_data() -> dict[FrameType, EventDataType]: Alert( code=AlertType.POWER_LOSS, from_dt=datetime(2022, 7, 22, 22, 33), - to_dt=datetime(2022, 7, 22, 22, 38, 11), + to_dt=None, ), ] }, @@ -402,7 +402,7 @@ def fixture_messages() -> dict[FrameType, bytearray]: ) ), FrameType.RESPONSE_ALERTS: bytearray.fromhex( - "6400021a5493382B9B94382B009C97372BD398372B" + "6400021a5493382B9B94382B009C97372B00000000" ), FrameType.RESPONSE_SCHEDULES: bytearray.fromhex( """100101000005001E0000FFFFFFFE0000FFFFFFFE0000FFFFFFFE0000FFFFFFFE0000FFFFF