Skip to content

Commit 60efec5

Browse files
committed
Reduce complexity
1 parent ef9fe05 commit 60efec5

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ def __init__(self, mac: str) -> None:
8888
self._rollover_consumption = False
8989
self._rollover_production = False
9090

91+
self._first_next_log_processed = False
92+
self._first_prev_log_processed = False
9193
self._logs: dict[int, dict[int, PulseLogRecord]] | None = None
9294
self._log_addresses_missing: list[int] | None = None
9395
self._log_production: bool | None = None
94-
self._next_log_exists = False
95-
self._prev_log_exists = False
9696
self._pulses_consumption: int | None = None
9797
self._pulses_production: int | None = None
9898
self._pulses_timestamp: datetime | None = None
@@ -377,7 +377,8 @@ def _detect_rollover(
377377
self._mac,
378378
direction
379379
)
380-
return False
380+
381+
return False
381382

382383
def add_empty_log(self, address: int, slot: int) -> None:
383384
"""Add empty energy log record to mark any start of beginning of energy log collection."""
@@ -507,11 +508,24 @@ def _update_log_direction(
507508
if self._logs is None:
508509
return
509510

511+
prev_timestamp = self._check_prev_production(address, slot, timestamp)
512+
next_timestamp = self._check_next_production(address, slot, timestamp)
513+
if self._first_prev_log_processed and self._first_next_log_processed:
514+
# _log_production is True when 2 out of 3 consecutive slots have
515+
# the same timestamp, otherwise it is False
516+
self._log_production = (
517+
next_timestamp == timestamp and prev_timestamp != timestamp
518+
) or (next_timestamp == prev_timestamp and next_timestamp != timestamp)
519+
520+
def _check_prev_production(
521+
self, address: int, slot: int, timestamp: datetime
522+
) -> datetime | None:
523+
"""Check the previous slot for production pulses."""
510524
prev_address, prev_slot = calc_log_address(address, slot, -1)
511525
if self._log_exists(prev_address, prev_slot):
512526
prev_timestamp = self._logs[prev_address][prev_slot].timestamp
513-
if not self._prev_log_exists:
514-
self._prev_log_exists = True
527+
if not self._first_prev_log_processed:
528+
self._first_prev_log_processed = True
515529
if prev_timestamp == timestamp:
516530
# Given log is the second log with same timestamp,
517531
# mark direction as production
@@ -525,14 +539,20 @@ def _update_log_direction(
525539
self._reset_log_references()
526540
elif self._log_production is None:
527541
self._log_production = False
528-
elif self._prev_log_exists:
529-
self._prev_log_exists = False
542+
return prev_timestamp
543+
elif self._first_prev_log_processed:
544+
self._first_prev_log_processed = False
545+
return None
530546

547+
def _check_next_production(
548+
self, address: int, slot: int, timestamp: datetime
549+
) -> datetime | None:
550+
"""Check the next slot for production pulses."""
531551
next_address, next_slot = calc_log_address(address, slot, 1)
532552
if self._log_exists(next_address, next_slot):
533553
next_timestamp = self._logs[next_address][next_slot].timestamp
534-
if not self._next_log_exists:
535-
self._next_log_exists = True
554+
if not self._first_next_log_processed:
555+
self._first_next_log_processed = True
536556
if next_timestamp == timestamp:
537557
# Given log is the first log with same timestamp,
538558
# mark direction as production of next log
@@ -546,15 +566,10 @@ def _update_log_direction(
546566
self._logs[next_address][next_slot].is_consumption = True
547567
elif self._log_production is None:
548568
self._log_production = False
549-
elif self._next_log_exists:
550-
self._next_log_exists = False
551-
552-
if self._prev_log_exists and self._next_log_exists:
553-
# _log_production is True when 2 out of 3 consecutive slots have
554-
# the same timestamp, otherwise it is False
555-
self._log_production = (
556-
next_timestamp == timestamp and prev_timestamp != timestamp
557-
) or (next_timestamp == prev_timestamp and next_timestamp != timestamp)
569+
return next_timestamp
570+
elif self._first_next_log_processed:
571+
self._first_next_log_processed = False
572+
return None
558573

559574
def _update_log_interval(self) -> None:
560575
"""Update the detected log interval based on the most recent two logs."""

0 commit comments

Comments
 (0)