Skip to content

Commit 6776f83

Browse files
committed
Implement suggestions
1 parent bd80b95 commit 6776f83

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ async def _get_initial_energy_logs(self) -> None:
468468
if not result:
469469
# Handle case with None-data in all address slots
470470
_LOGGER.debug(
471-
"Energy None-data of outdated data collected from log address %s, stopping collection",
471+
"All slots at log address %s are empty or outdated – stopping initial collection",
472472
log_address,
473473
)
474474
break
@@ -508,29 +508,26 @@ async def get_missing_energy_logs(self) -> None:
508508
await self._energy_log_records_save_to_cache()
509509

510510
async def energy_log_update(self, address: int | None) -> bool:
511-
"""Request energy log statistics from node. Returns true if successful."""
512-
result = False
511+
"""Request energy logs and return True only when at least one recent, non-empty record was stored; otherwise return False."""
512+
any_record_stored = False
513513
if address is None:
514-
return result
514+
return False
515515

516516
_LOGGER.debug(
517517
"Requesting EnergyLogs from node %s address %s",
518-
str(address),
519-
self.name,
518+
self._mac_in_str,
519+
address,
520520
)
521521
request = CircleEnergyLogsRequest(self._send, self._mac_in_bytes, address)
522522
if (response := await request.send()) is None:
523523
_LOGGER.debug(
524524
"Retrieving EnergyLogs data from node %s failed",
525525
self._mac_in_str,
526526
)
527-
return result
527+
return False
528528

529-
_LOGGER.debug(
530-
"EnergyLogs data from node %s, address=%s", self._mac_in_str, address
531-
)
529+
_LOGGER.debug("EnergyLogs from node %s, address=%s:", self._mac_in_str, address)
532530
await self._available_update_state(True, response.timestamp)
533-
energy_record_update = False
534531

535532
# Forward historical energy log information to energy counters
536533
# Each response message contains 4 log counters (slots) of the
@@ -541,11 +538,12 @@ async def energy_log_update(self, address: int | None) -> bool:
541538
_LOGGER.debug(
542539
"In slot=%s: pulses=%s, timestamp=%s", _slot, log_pulses, log_timestamp
543540
)
544-
if log_timestamp is None or log_pulses is None:
545-
self._energy_counters.add_empty_log(response.log_address, _slot)
546-
continue
547-
elif not self._check_timestamp_is_recent(address, _slot, log_timestamp):
541+
if (
542+
log_timestamp is None
543+
or log_pulses is None
548544
# Don't store an old log-record, store am empty record instead
545+
or not self._check_timestamp_is_recent(address, _slot, log_timestamp)
546+
):
549547
self._energy_counters.add_empty_log(response.log_address, _slot)
550548
continue
551549

@@ -556,7 +554,7 @@ async def energy_log_update(self, address: int | None) -> bool:
556554
log_pulses,
557555
import_only=True,
558556
):
559-
energy_record_update = True
557+
any_record_stored = True
560558
if not last_energy_timestamp_collected:
561559
# Collect the timestamp of the most recent response
562560
self._last_collected_energy_timestamp = log_timestamp.replace(
@@ -568,25 +566,26 @@ async def energy_log_update(self, address: int | None) -> bool:
568566
)
569567
last_energy_timestamp_collected = True
570568

571-
result = True
572569
self._energy_counters.update()
573-
if energy_record_update:
570+
if any_record_stored:
574571
_LOGGER.debug(
575572
"Saving energy record update to cache for %s", self._mac_in_str
576573
)
577574
await self.save_cache()
578575

579-
return result
576+
return any_record_stored
580577

581-
def _check_timestamp_is_recent(self, address, slot, timestamp) -> bool:
578+
def _check_timestamp_is_recent(
579+
self, address: int, slot: int, timestamp: datetime
580+
) -> bool:
582581
"""Check if the timestamp of the received log-record is recent.
583582
584583
A timestamp from within the last 24 hours is considered recent.
585584
"""
586-
if (
587-
(datetime.now(tz=UTC) - timestamp.replace(tzinfo=UTC)).total_seconds()
588-
// 3600
589-
) > DAY_IN_HOURS:
585+
age_seconds = (
586+
datetime.now(tz=UTC) - timestamp.replace(tzinfo=UTC)
587+
).total_seconds()
588+
if age_seconds > DAY_IN_HOURS * 3600:
590589
_LOGGER.warning(
591590
"EnergyLog from Node %s | address %s | slot %s | timestamp %s is outdated, ignoring...",
592591
self._mac_in_str,

0 commit comments

Comments
 (0)