Skip to content

Commit 5d56fec

Browse files
committed
Further improvements, add comment, debug-logging
1 parent d9d7e33 commit 5d56fec

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
@@ -443,7 +443,7 @@ async def get_missing_energy_logs(self) -> None:
443443

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

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

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

568589
self._energy_counters.update()

0 commit comments

Comments
 (0)