diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cae2b68f..3a0e020de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/plugwise_usb/nodes/circle.py b/plugwise_usb/nodes/circle.py index f8aa358f7..b760353ce 100644 --- a/plugwise_usb/nodes/circle.py +++ b/plugwise_usb/nodes/circle.py @@ -302,46 +302,39 @@ async def power_update(self) -> PowerStatistics | None: ) return self._power + def _log_no_energy_stats_update(self) -> None: + """Return log-message based on conditions.""" + 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, + ) + + @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() 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() return None # Always request last energy log records at initial startup @@ -351,6 +344,7 @@ async def energy_update(self) -> EnergyStatistics | None: ) 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", @@ -358,6 +352,7 @@ async def energy_update(self) -> EnergyStatistics | None: ) 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", @@ -365,10 +360,7 @@ async def energy_update(self) -> EnergyStatistics | None: ) return None - if ( - self._energy_counters.log_rollover - and self._current_log_address is not None - ): + if self._current_log_address is not None: # 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( @@ -392,6 +384,7 @@ async def energy_update(self) -> EnergyStatistics | None: 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() @@ -414,11 +407,7 @@ async def energy_update(self) -> EnergyStatistics | None: 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 @@ -441,7 +430,7 @@ async def get_missing_energy_logs(self) -> None: 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: _LOGGER.debug( "Start with initial energy request for the last 10 log addresses for node %s.", self._mac_in_str, @@ -459,23 +448,19 @@ async def get_missing_energy_logs(self) -> None: 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( + "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 if self._cache_enabled: await self._energy_log_records_save_to_cache() diff --git a/plugwise_usb/nodes/helpers/pulses.py b/plugwise_usb/nodes/helpers/pulses.py index a5da42316..811254969 100644 --- a/plugwise_usb/nodes/helpers/pulses.py +++ b/plugwise_usb/nodes/helpers/pulses.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 2e3af094f..ad51ec355 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [