Skip to content

Commit 45eb219

Browse files
committed
Refactor missing_addresses_after()
1 parent 8c30245 commit 45eb219

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,54 +1001,42 @@ def _missing_addresses_before(
10011001
def _missing_addresses_after(
10021002
self, address: int, slot: int, target: datetime
10031003
) -> list[int]:
1004-
"""Return list of any missing address(es) after given log timestamp."""
1004+
"""Return list of missing address(es) after a given log timestamp."""
1005+
if self._logs is None:
1006+
return []
1007+
10051008
addresses: list[int] = []
10061009

1007-
if self._logs is None:
1008-
return addresses
1010+
# Determine consumption and production intervals
1011+
def get_interval(config_value, default_hours):
1012+
return timedelta(minutes=config_value) if config_value and config_value > 0 else timedelta(hours=default_hours)
10091013

1010-
# default interval
1011-
calc_interval_cons = timedelta(hours=1)
1012-
if (
1013-
self._log_interval_consumption is not None
1014-
and self._log_interval_consumption > 0
1015-
):
1016-
# Use consumption interval
1017-
calc_interval_cons = timedelta(minutes=self._log_interval_consumption)
1014+
interval_cons = get_interval(self._log_interval_consumption, 1)
1015+
interval_prod = get_interval(self._log_interval_production, 1)
10181016

1019-
if not self._log_production: # False
1020-
expected_timestamp = (
1021-
self._logs[address][slot].timestamp + calc_interval_cons
1022-
)
1023-
address, slot = calc_log_address(address, slot, 1)
1024-
while expected_timestamp < target:
1017+
# Initial timestamps
1018+
base_timestamp = self._logs[address][slot].timestamp
1019+
timestamp_cons = base_timestamp + interval_cons
1020+
timestamp_prod = base_timestamp + interval_prod
1021+
1022+
address, slot = calc_log_address(address, slot, 1)
1023+
1024+
if not self._log_production:
1025+
# Only consumption interval is considered
1026+
while timestamp_cons < target:
1027+
if address not in addresses:
1028+
addresses.append(address)
1029+
timestamp_cons += timedelta(hours=1) # Always increment by 1 hour here
10251030
address, slot = calc_log_address(address, slot, 1)
1026-
expected_timestamp += timedelta(hours=1)
1031+
else:
1032+
# Consider both consumption and production timestamps
1033+
while timestamp_cons < target or timestamp_prod < target:
10271034
if address not in addresses:
10281035
addresses.append(address)
1029-
return addresses
1030-
1031-
# Production logging active
1032-
calc_interval_prod = timedelta(hours=1)
1033-
if (
1034-
self._log_interval_production is not None
1035-
and self._log_interval_production > 0
1036-
):
1037-
calc_interval_prod = timedelta(minutes=self._log_interval_production)
1036+
if timestamp_prod < timestamp_cons:
1037+
timestamp_prod += interval_prod
1038+
else:
1039+
timestamp_cons += interval_cons
1040+
address, slot = calc_log_address(address, slot, 1)
10381041

1039-
expected_timestamp_cons = (
1040-
self._logs[address][slot].timestamp + calc_interval_cons
1041-
)
1042-
expected_timestamp_prod = (
1043-
self._logs[address][slot].timestamp + calc_interval_prod
1044-
)
1045-
address, slot = calc_log_address(address, slot, 1)
1046-
while expected_timestamp_cons < target or expected_timestamp_prod < target:
1047-
if address not in addresses:
1048-
addresses.append(address)
1049-
if expected_timestamp_prod < expected_timestamp_cons:
1050-
expected_timestamp_prod += calc_interval_prod
1051-
else:
1052-
expected_timestamp_cons += calc_interval_cons
1053-
address, slot = calc_log_address(address, slot, 1)
10541042
return addresses

0 commit comments

Comments
 (0)