Skip to content

Commit 679d021

Browse files
committed
Improve refactoring of _update_rollover()
1 parent 00c384c commit 679d021

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -335,58 +335,60 @@ def _update_rollover(self) -> None:
335335
return
336336

337337
self._rollover_consumption = self._detect_rollover(
338+
self._rollover_consumption,
338339
self._last_log_consumption_timestamp,
339340
self._next_log_consumption_timestamp,
340341
)
341342
if self._log_production:
342343
self._rollover_production = self._detect_rollover(
344+
self._rollover_production,
343345
self._last_log_production_timestamp,
344346
self._next_log_production_timestamp,
345347
False,
346348
)
347349

348350
def _detect_rollover(
349351
self,
352+
rollover: bool,
350353
last_log_timestamp: datetime | None,
351354
next_log_timestamp: datetime | None,
352355
is_consumption=True,
353356
) -> bool:
354357
"""Helper function for _update_rollover()."""
358+
355359
if (
356-
self._pulses_timestamp is None
357-
or last_log_timestamp is None
358-
or next_log_timestamp is None
360+
self._pulses_timestamp is not None
361+
and last_log_timestamp is not None
362+
and next_log_timestamp is not None
359363
):
360-
# Unable to determine rollover
361-
return
362-
363-
direction = "consumption"
364-
if not is_consumption:
365-
direction = "production"
366-
367-
if self._pulses_timestamp > next_log_timestamp:
368-
_LOGGER.debug(
369-
"_update_rollover | %s | set %s rollover => pulses newer",
370-
self._mac,
371-
direction,
372-
)
373-
return True
364+
direction = "consumption"
365+
if not is_consumption:
366+
direction = "production"
374367

375-
if self._pulses_timestamp < last_log_timestamp:
376-
_LOGGER.debug(
377-
"_update_rollover | %s | set %s rollover => log newer",
378-
self._mac,
379-
direction,
380-
)
381-
return True
368+
if self._pulses_timestamp > next_log_timestamp:
369+
_LOGGER.debug(
370+
"_update_rollover | %s | set %s rollover => pulses newer",
371+
self._mac,
372+
direction,
373+
)
374+
return True
382375

383-
if last_log_timestamp <= self._pulses_timestamp <= next_log_timestamp:
384-
_LOGGER.debug(
385-
"_update_rollover | %s | reset %s rollover",
386-
self._mac,
387-
direction
388-
)
389-
return False
376+
if self._pulses_timestamp < last_log_timestamp:
377+
_LOGGER.debug(
378+
"_update_rollover | %s | set %s rollover => log newer",
379+
self._mac,
380+
direction,
381+
)
382+
return True
383+
384+
if last_log_timestamp <= self._pulses_timestamp <= next_log_timestamp:
385+
if rollover:
386+
_LOGGER.debug(
387+
"_update_rollover | %s | reset %s rollover",
388+
self._mac,
389+
direction
390+
)
391+
return False
390392

391393
def add_empty_log(self, address: int, slot: int) -> None:
392394
"""Add empty energy log record to mark any start of beginning of energy log collection."""

0 commit comments

Comments
 (0)