Skip to content

Commit c10bf4b

Browse files
committed
Refactor update_rollover()
1 parent 6577149 commit c10bf4b

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -335,72 +335,59 @@ def _update_rollover(self) -> None:
335335
if self._log_addresses_missing is not None and self._log_addresses_missing:
336336
return
337337

338+
self._rollover_consumption = self._detect_rollover(
339+
self._last_log_consumption_timestamp,
340+
self._next_log_consumption_timestamp,
341+
)
342+
if self._log_production:
343+
self._rollover_production = self._detect_rollover(
344+
self._last_log_production_timestamp,
345+
self._next_log_production_timestamp,
346+
False,
347+
)
348+
349+
def _detect_rollover(
350+
self,
351+
last_log_timestamp: datetime | None,
352+
next_log_timestamp: datetime | None,
353+
is_consumption=True,
354+
) -> bool:
355+
"""Helper function for _update_rollover()."""
338356
if (
339357
self._pulses_timestamp is None
340-
or self._last_log_consumption_timestamp is None
341-
or self._next_log_consumption_timestamp is None
358+
or last_log_timestamp is None
359+
or next_log_timestamp is None
342360
):
343361
# Unable to determine rollover
344362
return
345363

346-
if self._pulses_timestamp > self._next_log_consumption_timestamp:
347-
self._rollover_consumption = True
348-
_LOGGER.debug(
349-
"_update_rollover | %s | set consumption rollover => pulses newer",
350-
self._mac,
351-
)
352-
elif self._pulses_timestamp < self._last_log_consumption_timestamp:
353-
self._rollover_consumption = True
364+
direction = "consumption"
365+
if not is_consumption:
366+
direction = "production"
367+
368+
if self._pulses_timestamp > next_log_timestamp:
354369
_LOGGER.debug(
355-
"_update_rollover | %s | set consumption rollover => log newer",
370+
"_update_rollover | %s | set %s rollover => pulses newer",
356371
self._mac,
372+
direction,
357373
)
358-
elif (
359-
self._last_log_consumption_timestamp
360-
< self._pulses_timestamp
361-
< self._next_log_consumption_timestamp
362-
):
363-
if self._rollover_consumption:
364-
_LOGGER.debug("_update_rollover | %s | reset consumption", self._mac)
365-
self._rollover_consumption = False
366-
else:
367-
_LOGGER.debug("_update_rollover | %s | unexpected consumption", self._mac)
368-
369-
if not self._log_production:
370-
return
371-
372-
if (
373-
self._last_log_production_timestamp is None
374-
or self._next_log_production_timestamp is None
375-
):
376-
# Unable to determine rollover
377-
return
378-
379-
if not self._log_production:
380-
return
374+
return True
381375

382-
if self._pulses_timestamp > self._next_log_production_timestamp:
383-
self._rollover_production = True
376+
if self._pulses_timestamp < last_log_timestamp:
384377
_LOGGER.debug(
385-
"_update_rollover | %s | set production rollover => pulses newer",
378+
"_update_rollover | %s | set %s rollover => log newer",
386379
self._mac,
380+
direction,
387381
)
388-
elif self._pulses_timestamp < self._last_log_production_timestamp:
389-
self._rollover_production = True
382+
return True
383+
384+
if last_log_timestamp < self._pulses_timestamp < next_log_timestamp:
390385
_LOGGER.debug(
391-
"_update_rollover | %s | reset production rollover => log newer",
386+
"_update_rollover | %s | reset %s rollover",
392387
self._mac,
388+
direction
393389
)
394-
elif (
395-
self._last_log_production_timestamp
396-
< self._pulses_timestamp
397-
< self._next_log_production_timestamp
398-
):
399-
if self._rollover_production:
400-
_LOGGER.debug("_update_rollover | %s | reset production", self._mac)
401-
self._rollover_production = False
402-
else:
403-
_LOGGER.debug("_update_rollover | %s | unexpected production", self._mac)
390+
return False
404391

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

0 commit comments

Comments
 (0)