Skip to content

Commit e6b2225

Browse files
committed
Use a 2nd previous slot timestamp to detect returning to consumption only
1 parent 76b50cd commit e6b2225

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ def _update_log_direction(
506506

507507
prev_address, prev_slot = calc_log_address(address, slot, -1)
508508
if self._log_exists(prev_address, prev_slot):
509-
if self._logs[prev_address][prev_slot].timestamp == timestamp:
509+
timestamp_2 = self._logs[prev_address][prev_slot].timestamp
510+
if timestamp_2 == timestamp:
510511
# Given log is the second log with same timestamp,
511512
# mark direction as production
512513
self._logs[address][slot].is_consumption = False
@@ -536,6 +537,13 @@ def _update_log_direction(
536537
elif self._log_production is None:
537538
self._log_production = False
538539

540+
prev_prev_address, prev_prev_slot = calc_log_address(address, slot, -2)
541+
if self._log_exists(prev_prev_address, prev_prev_slot):
542+
timestamp_3 = self._logs[prev_prev_address][prev_prev_slot].timestamp
543+
self._log_production = (
544+
timestamp_2 == timestamp and timestamp_3 != timestamp
545+
) or (timestamp_2 == timestamp_3 and timestamp_2 != timestamp)
546+
539547
def _update_log_interval(self) -> None:
540548
"""Update the detected log interval based on the most recent two logs."""
541549
if self._logs is None or self._log_production is None:

0 commit comments

Comments
 (0)