Skip to content

Commit 1366366

Browse files
committed
Optimize code
1 parent 4f291b6 commit 1366366

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,13 @@ def _log_exists(self, address: int, slot: int) -> bool:
561561
return True
562562

563563
def _update_last_log_reference(
564-
self, address: int, slot: int, timestamp: datetime, is_consumption: bool
564+
self, address: int, slot: int, timestamp: datetime
565565
) -> None:
566566
"""Update references to last (most recent) log record."""
567567
if self._last_log_timestamp is None or self._last_log_timestamp < timestamp:
568568
self._last_log_address = address
569569
self._last_log_slot = slot
570570
self._last_log_timestamp = timestamp
571-
elif self._last_log_timestamp == timestamp and not is_consumption:
572-
self._last_log_address = address
573-
self._last_log_slot = slot
574-
self._last_log_timestamp = timestamp
575571

576572
def _update_last_consumption_log_reference(
577573
self, address: int, slot: int, timestamp: datetime
@@ -585,6 +581,18 @@ def _update_last_consumption_log_reference(
585581
self._last_log_consumption_address = address
586582
self._last_log_consumption_slot = slot
587583

584+
def _update_last_production_log_reference(
585+
self, address: int, slot: int, timestamp: datetime
586+
) -> None:
587+
"""Update references to last (most recent) log production record."""
588+
if (
589+
self._last_log_production_timestamp is None
590+
or self._last_log_production_timestamp <= timestamp
591+
):
592+
self._last_log_production_timestamp = timestamp
593+
self._last_log_production_address = address
594+
self._last_log_production_slot = slot
595+
588596
def _reset_log_references(self) -> None:
589597
"""Reset log references."""
590598
self._last_log_consumption_address = None
@@ -634,30 +642,14 @@ def _reset_log_references(self) -> None:
634642
self._first_log_production_address = address
635643
self._first_log_production_slot = slot
636644

637-
def _update_last_production_log_reference(
638-
self, address: int, slot: int, timestamp: datetime
639-
) -> None:
640-
"""Update references to last (most recent) log production record."""
641-
if (
642-
self._last_log_production_timestamp is None
643-
or self._last_log_production_timestamp <= timestamp
644-
):
645-
self._last_log_production_timestamp = timestamp
646-
self._last_log_production_address = address
647-
self._last_log_production_slot = slot
648-
649645
def _update_first_log_reference(
650-
self, address: int, slot: int, timestamp: datetime, is_consumption: bool
646+
self, address: int, slot: int, timestamp: datetime
651647
) -> None:
652648
"""Update references to first (oldest) log record."""
653649
if self._first_log_timestamp is None or self._first_log_timestamp > timestamp:
654650
self._first_log_address = address
655651
self._first_log_slot = slot
656652
self._first_log_timestamp = timestamp
657-
elif self._first_log_timestamp == timestamp and is_consumption:
658-
self._first_log_address = address
659-
self._first_log_slot = slot
660-
self._first_log_timestamp = timestamp
661653

662654
def _update_first_consumption_log_reference(
663655
self, address: int, slot: int, timestamp: datetime
@@ -691,8 +683,8 @@ def _update_log_references(self, address: int, slot: int) -> None:
691683
is_consumption = self._logs[address][slot].is_consumption
692684

693685
# Update log references
694-
self._update_first_log_reference(address, slot, log_time_stamp, is_consumption)
695-
self._update_last_log_reference(address, slot, log_time_stamp, is_consumption)
686+
self._update_first_log_reference(address, slot, log_time_stamp)
687+
self._update_last_log_reference(address, slot, log_time_stamp)
696688

697689
if is_consumption:
698690
self._update_first_consumption_log_reference(address, slot, log_time_stamp)

0 commit comments

Comments
 (0)