Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions plugwise_usb/nodes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09

# Try collecting energy-stats for _current_log_address
result = await self.energy_log_update(
self._current_log_address, save_cache=True
self._current_log_address, save_cache=False
)
if not result:
_LOGGER.debug(
Expand All @@ -432,7 +432,9 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
# Retry with previous log address as Circle node pointer to self._current_log_address
# could be rolled over while the last log is at previous address/slot
prev_log_address, _ = calc_log_address(self._current_log_address, 1, -4)
result = await self.energy_log_update(prev_log_address, save_cache=True)
result = await self.energy_log_update(
prev_log_address, save_cache=False
)
if not result:
_LOGGER.debug(
"async_energy_update | %s | Log rollover | energy_log_update from address %s failed",
Expand All @@ -441,6 +443,9 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
)
return None

if self._cache_enabled:
await self.save_cache()

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
if (
missing_addresses := self._energy_counters.log_addresses_missing
) is not None:
Expand Down Expand Up @@ -530,7 +535,7 @@ async def _get_initial_energy_logs(self) -> None:
total_addresses -= 1

if self._cache_enabled:
await self._energy_log_records_save_to_cache()
await self.save_cache()

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
async def get_missing_energy_logs(self) -> None:
"""Task to retrieve missing energy logs."""
Expand Down Expand Up @@ -566,12 +571,16 @@ async def get_missing_energy_logs(self) -> None:
break

if self._cache_enabled:
await self._energy_log_records_save_to_cache()
await self.save_cache()

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
async def energy_log_update(
self, address: int | None, save_cache: bool = True
) -> bool:
"""Request energy logs and return True only when at least one recent, non-empty record was stored; otherwise return False."""
"""Request energy logs from node and store them.

Return True if processing succeeded (records stored in memory), regardless of whether new entries were added.
Return False on transport or address errors.
"""
Comment thread
bouwew marked this conversation as resolved.
if address is None:
return False

Expand Down Expand Up @@ -609,20 +618,24 @@ async def energy_log_update(
self._energy_counters.add_empty_log(response.log_address, _slot)
continue

cache_updated = await self._energy_log_record_update_state(
updated = await self._energy_log_record_update_state(
response.log_address,
_slot,
log_timestamp.replace(tzinfo=UTC),
log_pulses,
import_only=True,
)
if updated:
cache_updated = True

self._energy_counters.update()
if cache_updated and save_cache:
_LOGGER.debug(
"Saving energy record update to cache for %s", self._mac_in_str
)
await self.save_cache()
if cache_updated:
await self._energy_log_records_save_to_cache()
if save_cache:
_LOGGER.debug(
"Saving energy record update to cache for %s", self._mac_in_str
)
await self.save_cache()

Comment thread
bouwew marked this conversation as resolved.
Outdated
return True

Expand Down Expand Up @@ -713,8 +726,6 @@ async def _energy_log_records_save_to_cache(self) -> None:
cached_logs = "|".join(records)
_LOGGER.debug("Saving energy logrecords to cache for %s", self._mac_in_str)
self._set_cache(CACHE_ENERGY_COLLECTION, cached_logs)
# Persist new cache entries to disk immediately
await self.save_cache(trigger_only=True)

async def _energy_log_record_update_state(
self,
Expand Down Expand Up @@ -743,13 +754,6 @@ async def _energy_log_record_update_state(
str(slot),
self._mac_in_str,
)
new_cache = (
f"{log_cache_record}|{cached_logs}"
if cached_logs
else log_cache_record
)
self._set_cache(CACHE_ENERGY_COLLECTION, new_cache)
await self.save_cache(trigger_only=True)
return True

_LOGGER.debug(
Expand All @@ -763,7 +767,6 @@ async def _energy_log_record_update_state(
str(slot),
self._mac_in_str,
)
self._set_cache(CACHE_ENERGY_COLLECTION, log_cache_record)
return True

@raise_not_loaded
Expand Down Expand Up @@ -1187,7 +1190,7 @@ async def _relay_init_update_state(self, state: bool) -> None:
NodeFeature.RELAY_INIT, self._relay_config
)
_LOGGER.debug(
"Saving relay_init state update to cachefor %s", self._mac_in_str
"Saving relay_init state update to cache for %s", self._mac_in_str
)
await self.save_cache()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "plugwise_usb"
version = "0.44.13"
version = "0.44.14a2"
license = "MIT"
keywords = ["home", "automation", "plugwise", "module", "usb"]
classifiers = [
Expand Down