Skip to content

Commit 350451a

Browse files
committed
Implement suggestions
1 parent 25f059f commit 350451a

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -510,28 +510,25 @@ async def get_missing_energy_logs(self) -> None:
510510

511511
async def energy_log_update(self, address: int | None) -> bool:
512512
"""Request energy log statistics from node. Returns true if successful."""
513-
result = False
513+
any_record_stored = False
514514
if address is None:
515-
return result
515+
return False
516516

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

530-
_LOGGER.debug(
531-
"EnergyLogs data from node %s, address=%s", self._mac_in_str, address
532-
)
530+
_LOGGER.debug("EnergyLogs from node %s, address=%s:", self._mac_in_str, address)
533531
await self._available_update_state(True, response.timestamp)
534-
energy_record_update = False
535532

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

@@ -557,7 +555,7 @@ async def energy_log_update(self, address: int | None) -> bool:
557555
log_pulses,
558556
import_only=True,
559557
):
560-
energy_record_update = True
558+
any_record_stored = True
561559
if not last_energy_timestamp_collected:
562560
# Collect the timestamp of the most recent response
563561
self._last_collected_energy_timestamp = log_timestamp.replace(
@@ -569,25 +567,26 @@ async def energy_log_update(self, address: int | None) -> bool:
569567
)
570568
last_energy_timestamp_collected = True
571569

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

580-
return result
577+
return any_record_stored
581578

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

0 commit comments

Comments
 (0)