Skip to content

Commit 73d5236

Browse files
committed
Implement more suggestions
1 parent 4e752ea commit 73d5236

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
402402
return None
403403

404404
# Always request the most recent energy log records at initial startup, check if the current
405-
# address is acutally reported by the node even when all slots at that address are empty.
405+
# address is actually reported by the node even when all slots at that address are empty.
406406
if not self._last_energy_log_requested:
407407
self._last_energy_log_requested, _ = await self.energy_log_update(
408408
self._current_log_address, save_cache=False
@@ -418,7 +418,7 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
418418
return None
419419

420420
# Try collecting energy-stats from _current_log_address
421-
result, _ = await self.energy_log_update(
421+
result, slots_empty_cur = await self.energy_log_update(
422422
self._current_log_address, save_cache=False
423423
)
424424
if not result:
@@ -432,7 +432,7 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
432432
# Retry with previous log address as Circle node pointer to self._current_log_address
433433
# could be rolled over while the last log is at previous address
434434
prev_log_address, _ = calc_log_address(self._current_log_address, 1, -4)
435-
result, _ = await self.energy_log_update(prev_log_address, save_cache=False)
435+
result, slots_empty_prev = await self.energy_log_update(prev_log_address, save_cache=False)
436436
if not result:
437437
_LOGGER.debug(
438438
"async_energy_update | %s | Log rollover | energy_log_update from address %s failed",
@@ -441,7 +441,7 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
441441
)
442442
return None
443443

444-
if self._cache_enabled:
444+
if self._cache_enabled and (not slots_empty_cur or not slots_empty_prev):
445445
await self.save_cache()
446446

447447
if (
@@ -583,9 +583,9 @@ async def energy_log_update(
583583
) -> tuple[bool, bool]:
584584
"""Request energy logs from node and store them.
585585
586-
Return first bool as True if processing succeeded: records stored in memory, also with empty slots.
587-
Return fist bool as False on transport or address errors.
588-
Return second bool as False when all slots are empty otherwise as True
586+
Return first bool as True if processing succeeded (records stored in memory, possibly all-empty).
587+
Return first bool as False on transport or address errors.
588+
Return second bool as False when all slots are empty or outdated; otherwise True.
589589
"""
590590
result = False
591591
slots_empty = True
@@ -613,7 +613,6 @@ async def energy_log_update(
613613
# Each response message contains 4 log counters (slots) of the
614614
# energy pulses collected during the previous hour of given timestamp
615615
cache_updated = False
616-
slot_updated = False
617616
for _slot in range(4, 0, -1):
618617
log_timestamp, log_pulses = response.log_data[_slot]
619618
_LOGGER.debug(
@@ -631,18 +630,14 @@ async def energy_log_update(
631630
log_pulses,
632631
import_only=True,
633632
)
634-
slot_updated = True
635-
636-
cache_updated |= slot_updated
633+
cache_updated = True
637634

638635
self._energy_counters.update()
639636
if cache_updated:
640637
slots_empty = False
641638
await self._energy_log_records_save_to_cache()
642639
if save_cache:
643-
_LOGGER.debug(
644-
"Saving and storing energy cache for %s", self._mac_in_str
645-
)
640+
_LOGGER.debug("Saving energy cache for %s", self._mac_in_str)
646641
await self.save_cache()
647642
return result, slots_empty
648643

0 commit comments

Comments
 (0)