Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.42.1

- Implement code improvements, extend debug message [#253](https://github.com/plugwise/python-plugwise-usb/pull/247)

## v0.42.0

- Implement resetting of energy logs
Expand Down
95 changes: 40 additions & 55 deletions plugwise_usb/nodes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,46 +302,39 @@
)
return self._power

def _log_no_energy_stats_update(self) -> None:
"""Return log-message based on conditions."""
if (

Check warning on line 307 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L307

Added line #L307 was not covered by tests
self._initialization_delay_expired is not None
and datetime.now(tz=UTC) < self._initialization_delay_expired
):
_LOGGER.info(

Check warning on line 311 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L311

Added line #L311 was not covered by tests
"Unable to return energy statistics for %s during initialization, because it is not responding",
self.name,
)
else:
_LOGGER.warning(

Check warning on line 316 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L316

Added line #L316 was not covered by tests
"Unable to return energy statistics for %s, because it is not responding",
self.name,
)


@raise_not_loaded
@raise_calibration_missing
async def energy_update(self) -> EnergyStatistics | None:
"""Return updated energy usage statistics."""
if self._current_log_address is None:
_LOGGER.debug(
"Unable to update energy logs for node %s because last_log_address is unknown.",
"Unable to update energy logs for node %s because the current log address is unknown.",
self._mac_in_str,
)
if await self.node_info_update() is None:
if (
self._initialization_delay_expired is not None
and datetime.now(tz=UTC) < self._initialization_delay_expired
):
_LOGGER.info(
"Unable to return energy statistics for %s during initialization, because it is not responding",
self.name,
)
else:
_LOGGER.warning(
"Unable to return energy statistics for %s, because it is not responding",
self.name,
)
self._log_no_energy_stats_update()

Check warning on line 332 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L332

Added line #L332 was not covered by tests
return None
# request node info update every 30 minutes.
# Request node info update every 30 minutes.
elif not self.skip_update(self._node_info, 1800):
if await self.node_info_update() is None:
if (
self._initialization_delay_expired is not None
and datetime.now(tz=UTC) < self._initialization_delay_expired
):
_LOGGER.info(
"Unable to return energy statistics for %s during initialization, because it is not responding",
self.name,
)
else:
_LOGGER.warning(
"Unable to return energy statistics for %s, because it is not responding",
self.name,
)
self._log_no_energy_stats_update()

Check warning on line 337 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L337

Added line #L337 was not covered by tests
return None

# Always request last energy log records at initial startup
Expand All @@ -351,24 +344,23 @@
)

if self._energy_counters.log_rollover:
# Try updating node_info
if await self.node_info_update() is None:
_LOGGER.debug(
"async_energy_update | %s | Log rollover | node_info_update failed",
self._mac_in_str,
)
return None

# Try collecting energy-stats for _current_log_address
if not await self.energy_log_update(self._current_log_address):
_LOGGER.debug(
"async_energy_update | %s | Log rollover | energy_log_update failed",
self._mac_in_str,
)
return None

if (
self._energy_counters.log_rollover
and self._current_log_address is not None
):
if self._current_log_address is not None:

Check warning on line 363 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L363

Added line #L363 was not covered by tests
# 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(
Expand All @@ -392,6 +384,7 @@
self._mac_in_str,
)
return self._energy_counters.energy_statistics

if len(missing_addresses) == 1:
if await self.energy_log_update(missing_addresses[0]):
await self.power_update()
Expand All @@ -414,11 +407,7 @@
self._retrieve_energy_logs_task = create_task(
self.get_missing_energy_logs()
)
else:
_LOGGER.debug(
"Skip creating task to update energy logs for node %s",
self._mac_in_str,
)

if (
self._initialization_delay_expired is not None
and datetime.now(tz=UTC) < self._initialization_delay_expired
Expand All @@ -441,7 +430,7 @@
if self._current_log_address is None:
return None

if self._energy_counters.log_addresses_missing is None:
if (missing_addresses := self._energy_counters.log_addresses_missing) is None:

Check warning on line 433 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L433

Added line #L433 was not covered by tests
_LOGGER.debug(
"Start with initial energy request for the last 10 log addresses for node %s.",
self._mac_in_str,
Expand All @@ -459,23 +448,19 @@
return

_LOGGER.debug("Task created to get missing logs of %s", self._mac_in_str)
if (
missing_addresses := self._energy_counters.log_addresses_missing
) is not None:
_LOGGER.debug(
"Task Request %s missing energy logs for node %s | %s",
str(len(missing_addresses)),
self._mac_in_str,
str(missing_addresses),
)

missing_addresses = sorted(missing_addresses, reverse=True)
tasks = [
create_task(self.energy_log_update(address))
for address in missing_addresses
]
for task in tasks:
await task
_LOGGER.debug(

Check warning on line 451 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L451

Added line #L451 was not covered by tests
"Task Request %s missing energy logs for node %s | %s",
str(len(missing_addresses)),
self._mac_in_str,
str(missing_addresses),
)
missing_addresses = sorted(missing_addresses, reverse=True)
tasks = [

Check warning on line 458 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L457-L458

Added lines #L457 - L458 were not covered by tests
create_task(self.energy_log_update(address))
for address in missing_addresses
]
for task in tasks:
await task

Check warning on line 463 in plugwise_usb/nodes/circle.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/circle.py#L462-L463

Added lines #L462 - L463 were not covered by tests

if self._cache_enabled:
await self._energy_log_records_save_to_cache()
Expand Down
3 changes: 2 additions & 1 deletion plugwise_usb/nodes/helpers/pulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,11 @@ def _logs_missing(self, from_timestamp: datetime) -> list[int] | None:

missing = []
_LOGGER.debug(
"_logs_missing | %s | first_address=%s, last_address=%s",
"_logs_missing | %s | first_address=%s, last_address=%s, from_timestamp=%s",
self._mac,
first_address,
last_address,
from_timestamp,
)

# When higher addresses contain outdated data
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.42.0"
version = "0.42.1"
license = "MIT"
keywords = ["home", "automation", "plugwise", "module", "usb"]
classifiers = [
Expand Down
Loading