Skip to content

Commit

Permalink
Unpack alert dt before check.
Browse files Browse the repository at this point in the history
Unpack 'to_seconds' and check it with a comparison to zero, instead of using 'check_parameter'.
  • Loading branch information
denpamusic committed Oct 20, 2023
1 parent a33e3c7 commit 7cabb39
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions pyplumio/structures/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
]
},
Expand Down Expand Up @@ -402,7 +402,7 @@ def fixture_messages() -> dict[FrameType, bytearray]:
)
),
FrameType.RESPONSE_ALERTS: bytearray.fromhex(
"6400021a5493382B9B94382B009C97372BD398372B"
"6400021a5493382B9B94382B009C97372B00000000"
),
FrameType.RESPONSE_SCHEDULES: bytearray.fromhex(
"""100101000005001E0000FFFFFFFE0000FFFFFFFE0000FFFFFFFE0000FFFFFFFE0000FFFFF
Expand Down

0 comments on commit 7cabb39

Please sign in to comment.