Skip to content

Commit d67b43b

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

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -998,57 +998,45 @@ def _missing_addresses_before(
998998

999999
return addresses
10001000

1001-
def _missing_addresses_after(
1002-
self, address: int, slot: int, target: datetime
1003-
) -> list[int]:
1004-
"""Return list of any missing address(es) after given log timestamp."""
1005-
addresses: list[int] = []
1001+
def _missing_addresses_after(
1002+
self, address: int, slot: int, target: datetime
1003+
) -> list[int]:
1004+
"""Return list of missing address(es) after a given log timestamp."""
1005+
if self._logs is None:
1006+
return []
10061007

1007-
if self._logs is None:
1008-
return addresses
1008+
addresses: list[int] = []
10091009

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)
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)
10181013

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:
1025-
address, slot = calc_log_address(address, slot, 1)
1026-
expected_timestamp += timedelta(hours=1)
1027-
if address not in addresses:
1028-
addresses.append(address)
1029-
return addresses
1014+
interval_cons = get_interval(self._log_interval_consumption, 1)
1015+
interval_prod = get_interval(self._log_interval_production, 1)
10301016

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)
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
10381021

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:
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:
10471027
if address not in addresses:
10481028
addresses.append(address)
1049-
if expected_timestamp_prod < expected_timestamp_cons:
1050-
expected_timestamp_prod += calc_interval_prod
1029+
timestamp_cons += timedelta(hours=1) # Always increment by 1 hour here
1030+
address, slot = calc_log_address(address, slot, 1)
1031+
else:
1032+
# Consider both consumption and production timestamps
1033+
while timestamp_cons < target or timestamp_prod < target:
1034+
if address not in addresses:
1035+
addresses.append(address)
1036+
if timestamp_prod < timestamp_cons:
1037+
timestamp_prod += interval_prod
10511038
else:
1052-
expected_timestamp_cons += calc_interval_cons
1039+
timestamp_cons += interval_cons
10531040
address, slot = calc_log_address(address, slot, 1)
1054-
return addresses
1041+
1042+
return addresses

0 commit comments

Comments
 (0)