Skip to content

Commit 38f41fb

Browse files
committed
Next try
1 parent 78d57b2 commit 38f41fb

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,18 @@ def _update_log_direction(
510510
slot,
511511
timestamp,
512512
)
513+
prev_exists = next_exists = False
513514
prev_address, prev_slot = calc_log_address(address, slot, -1)
514515
if self._log_exists(prev_address, prev_slot):
515-
timestamp_2 = self._logs[prev_address][prev_slot].timestamp
516+
prev_exists = True
517+
prev_timestamp = self._logs[prev_address][prev_slot].timestamp
516518
_LOGGER.debug(
517519
"_update_log_direction | pr_address=%s | pr_slot=%s | timestamp=%s",
518520
prev_address,
519521
prev_slot,
520-
timestamp_2,
522+
prev_timestamp,
521523
)
522-
if timestamp_2 == timestamp:
524+
if prev_timestamp == timestamp:
523525
# Given log is the second log with same timestamp,
524526
# mark direction as production
525527
self._logs[address][slot].is_consumption = False
@@ -533,23 +535,9 @@ def _update_log_direction(
533535
elif self._log_production is None:
534536
self._log_production = False
535537

536-
prev_prev_address, prev_prev_slot = calc_log_address(address, slot, -2)
537-
if self._log_exists(prev_prev_address, prev_prev_slot):
538-
timestamp_3 = self._logs[prev_prev_address][prev_prev_slot].timestamp
539-
_LOGGER.debug(
540-
"_update_log_direction | pr2_address=%s | pr2_slot=%s | timestamp=%s",
541-
prev_prev_address,
542-
prev_prev_slot,
543-
timestamp_3,
544-
)
545-
# _log_production is True when 2 out of 3 consecutive slots have
546-
# the same timestamp, otherwise it is False
547-
self._log_production = (
548-
timestamp_2 == timestamp and timestamp_3 != timestamp
549-
) or (timestamp_2 == timestamp_3 and timestamp_2 != timestamp)
550-
551538
next_address, next_slot = calc_log_address(address, slot, 1)
552539
if self._log_exists(next_address, next_slot):
540+
next_exists = True
553541
next_timestamp = self._logs[next_address][next_slot].timestamp
554542
_LOGGER.debug(
555543
"_update_log_direction | nxt_address=%s | nxt_slot=%s | timestamp=%s",
@@ -564,12 +552,20 @@ def _update_log_direction(
564552
if self._logs[next_address][next_slot].is_consumption:
565553
self._logs[next_address][next_slot].is_consumption = False
566554
self._reset_log_references()
555+
self._log_production = True
567556
elif self._log_production:
568557
self._logs[address][slot].is_consumption = False
569558
self._logs[next_address][next_slot].is_consumption = True
570559
elif self._log_production is None:
571560
self._log_production = False
572561

562+
if prev_exists and next_exists:
563+
# _log_production is True when 2 out of 3 consecutive slots have
564+
# the same timestamp, otherwise it is False
565+
self._log_production = (
566+
next_timestamp == timestamp and prev_timestamp != timestamp
567+
) or (next_timestamp == prev_timestamp and next_timestamp != timestamp)
568+
573569
def _update_log_interval(self) -> None:
574570
"""Update the detected log interval based on the most recent two logs."""
575571
if self._logs is None or self._log_production is None:

0 commit comments

Comments
 (0)