Skip to content

Commit 976f53c

Browse files
committed
Further improvements, add comment, debug-logging
1 parent 47a964e commit 976f53c

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ async def get_missing_energy_logs(self) -> None:
442442

443443
if (missing_addresses := self._energy_counters.log_addresses_missing) is None:
444444
_LOGGER.debug(
445-
"Start with initial energy requests for the last 10 log addresses for node %s.",
445+
"Start collecting initial energy logs from the last 10 log addresses for node %s.",
446446
self._mac_in_str,
447447
)
448448
total_addresses = 11
@@ -459,17 +459,33 @@ async def get_missing_energy_logs(self) -> None:
459459

460460
# Check if the most recent timestamp of an earlier address is recent
461461
# (within 2/4 * log_interval plus 5 mins margin)
462-
if log_address != self._current_log_address:
463-
log_interval = self.energy_consumption_interval
464-
_LOGGER.debug("log_interval: %s", log_interval)
465-
_LOGGER.debug(
466-
"energy_production_interval: %s",
467-
self.energy_production_interval,
468-
)
469-
factor = 4
470-
if self.energy_production_interval is not None:
471-
factor = 2
462+
log_interval = self.energy_consumption_interval
463+
_LOGGER.debug("log_interval: %s", log_interval)
464+
_LOGGER.debug(
465+
"last_collected_energy_timestamp: %s",
466+
self._last_collected_energy_timestamp,
467+
)
468+
_LOGGER.debug(
469+
"energy_production_interval: %s",
470+
self.energy_production_interval,
471+
)
472+
factor = 4
473+
if self.energy_production_interval is not None:
474+
factor = 2
472475

476+
if log_address == self._current_log_address:
477+
if (
478+
self._last_collected_energy_timestamp is not None
479+
and log_interval is not None
480+
and (
481+
datetime.now(tz=UTC) - self._last_collected_energy_timestamp
482+
).total_seconds() // 60 > (factor * log_interval) + 5 # minutes
483+
):
484+
_LOGGER.debug(
485+
"Energy data collected from the current log address is outdated, stopping collection"
486+
)
487+
break
488+
else:
473489
_LOGGER.debug("prev_address_timestamp: %s", prev_address_timestamp)
474490
if (
475491
self._last_collected_energy_timestamp is not None
@@ -559,9 +575,14 @@ async def energy_log_update(self, address: int | None) -> bool:
559575
):
560576
energy_record_update = True
561577
if not last_energy_timestamp_collected:
578+
# Collect the timestamp of the most recent response
562579
self._last_collected_energy_timestamp = log_timestamp.replace(
563580
tzinfo=UTC
564581
)
582+
_LOGGER.debug(
583+
"Setting last_collected_energy_timestamp to %s",
584+
self._last_collected_energy_timestamp,
585+
)
565586
last_energy_timestamp_collected = True
566587

567588
self._energy_counters.update()

0 commit comments

Comments
 (0)