Skip to content

Commit 8053b46

Browse files
committed
Skip using _energy_log_records_save_to_cache()
use _energy_log_records_save_to_cache() instead
1 parent a0f61c6 commit 8053b46

File tree

1 file changed

+24
-62
lines changed

1 file changed

+24
-62
lines changed

plugwise_usb/nodes/circle.py

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

405+
if self._cache_enabled:
406+
_LOGGER.debug(
407+
"Saving initial energy_logs to cache for %s", self._mac_in_str
408+
)
409+
await self._energy_log_records_save_to_cache()
410+
await self.save_cache()
411+
405412
if (
406413
missing_addresses := self._energy_counters.log_addresses_missing
407414
) is not None:
@@ -515,7 +522,11 @@ async def _get_initial_energy_logs(self) -> None:
515522
total_addresses -= 1
516523

517524
if self._cache_enabled:
525+
_LOGGER.debug(
526+
"Saving initial energy_logs to cache for %s", self._mac_in_str
527+
)
518528
await self._energy_log_records_save_to_cache()
529+
await self.save_cache()
519530

520531
async def get_missing_energy_logs(self) -> None:
521532
"""Task to retrieve missing energy logs."""
@@ -543,7 +554,11 @@ async def get_missing_energy_logs(self) -> None:
543554
await task
544555

545556
if self._cache_enabled:
557+
_LOGGER.debug(
558+
"Saving missing energy_logs to cache for %s", self._mac_in_str
559+
)
546560
await self._energy_log_records_save_to_cache()
561+
await self.save_cache()
547562

548563
async def energy_log_update(self, address: int | None) -> tuple[bool, bool]:
549564
"""Request energy log statistics from node. Returns true if successful."""
@@ -568,7 +583,6 @@ async def energy_log_update(self, address: int | None) -> tuple[bool, bool]:
568583

569584
_LOGGER.debug("EnergyLogs data from %s, address=%s", self._mac_in_str, address)
570585
await self._available_update_state(True, response.timestamp)
571-
energy_record_update = False
572586

573587
# Forward historical energy log information to energy counters
574588
# Each response message contains 4 log counters (slots) of the
@@ -582,14 +596,14 @@ async def energy_log_update(self, address: int | None) -> tuple[bool, bool]:
582596
if log_timestamp is None or log_pulses is None:
583597
self._energy_counters.add_empty_log(response.log_address, _slot)
584598
empty_log = True
585-
elif await self._energy_log_record_update_state(
586-
response.log_address,
587-
_slot,
588-
log_timestamp.replace(tzinfo=UTC),
589-
log_pulses,
590-
import_only=True,
591-
):
592-
energy_record_update = True
599+
else:
600+
self._energy_counters.add_pulse_log(
601+
response.log_address,
602+
_slot,
603+
log_timestamp.replace(tzinfo=UTC),
604+
log_pulses,
605+
import_only=True,
606+
)
593607
if not last_energy_timestamp_collected:
594608
# Collect the timestamp of the most recent response
595609
self._last_collected_energy_timestamp = log_timestamp.replace(
@@ -603,12 +617,6 @@ async def energy_log_update(self, address: int | None) -> tuple[bool, bool]:
603617

604618
result = True
605619
self._energy_counters.update()
606-
if energy_record_update:
607-
_LOGGER.debug(
608-
"Saving energy record update to cache for %s", self._mac_in_str
609-
)
610-
await self.save_cache()
611-
612620
return result, empty_log
613621

614622
async def _energy_log_records_load_from_cache(self) -> bool:
@@ -688,55 +696,8 @@ async def _energy_log_records_save_to_cache(self) -> None:
688696
cached_logs += f"-{log.timestamp.hour}-{log.timestamp.minute}"
689697
cached_logs += f"-{log.timestamp.second}:{log.pulses}"
690698

691-
_LOGGER.debug("Saving energy logrecords to cache for %s", self._mac_in_str)
692699
self._set_cache(CACHE_ENERGY_COLLECTION, cached_logs)
693700

694-
async def _energy_log_record_update_state(
695-
self,
696-
address: int,
697-
slot: int,
698-
timestamp: datetime,
699-
pulses: int,
700-
import_only: bool = False,
701-
) -> bool:
702-
"""Process new energy log record. Returns true if record is new or changed."""
703-
self._energy_counters.add_pulse_log(
704-
address, slot, timestamp, pulses, import_only=import_only
705-
)
706-
if not self._cache_enabled:
707-
return False
708-
709-
log_cache_record = f"{address}:{slot}:{timestamp.year}"
710-
log_cache_record += f"-{timestamp.month}-{timestamp.day}"
711-
log_cache_record += f"-{timestamp.hour}-{timestamp.minute}"
712-
log_cache_record += f"-{timestamp.second}:{pulses}"
713-
if (cached_logs := self._get_cache(CACHE_ENERGY_COLLECTION)) is not None:
714-
if log_cache_record not in cached_logs:
715-
_LOGGER.debug(
716-
"Adding logrecord (%s, %s) to cache of %s",
717-
str(address),
718-
str(slot),
719-
self._mac_in_str,
720-
)
721-
self._set_cache(
722-
CACHE_ENERGY_COLLECTION, cached_logs + "|" + log_cache_record
723-
)
724-
return True
725-
726-
_LOGGER.debug(
727-
"Energy logrecord already present for %s, ignoring", self._mac_in_str
728-
)
729-
return False
730-
731-
_LOGGER.debug(
732-
"Cache is empty, adding new logrecord (%s, %s) for %s",
733-
str(address),
734-
str(slot),
735-
self._mac_in_str,
736-
)
737-
self._set_cache(CACHE_ENERGY_COLLECTION, log_cache_record)
738-
return True
739-
740701
@raise_not_loaded
741702
async def set_relay(self, state: bool) -> bool:
742703
"""Change the state of the relay."""
@@ -1081,6 +1042,7 @@ async def unload(self) -> None:
10811042

10821043
if self._cache_enabled:
10831044
await self._energy_log_records_save_to_cache()
1045+
await self.save_cache()
10841046

10851047
await super().unload()
10861048

0 commit comments

Comments
 (0)