@@ -302,46 +302,39 @@ async def power_update(self) -> PowerStatistics | None:
302302 )
303303 return self ._power
304304
305+ def _log_no_energy_stats_update (self ) -> None :
306+ """Return log-message based on conditions."""
307+ if (
308+ self ._initialization_delay_expired is not None
309+ and datetime .now (tz = UTC ) < self ._initialization_delay_expired
310+ ):
311+ _LOGGER .info (
312+ "Unable to return energy statistics for %s during initialization, because it is not responding" ,
313+ self .name ,
314+ )
315+ else :
316+ _LOGGER .warning (
317+ "Unable to return energy statistics for %s, because it is not responding" ,
318+ self .name ,
319+ )
320+
321+
305322 @raise_not_loaded
306323 @raise_calibration_missing
307324 async def energy_update (self ) -> EnergyStatistics | None :
308325 """Return updated energy usage statistics."""
309326 if self ._current_log_address is None :
310327 _LOGGER .debug (
311- "Unable to update energy logs for node %s because last_log_address is unknown." ,
328+ "Unable to update energy logs for node %s because the current log address is unknown." ,
312329 self ._mac_in_str ,
313330 )
314331 if await self .node_info_update () is None :
315- if (
316- self ._initialization_delay_expired is not None
317- and datetime .now (tz = UTC ) < self ._initialization_delay_expired
318- ):
319- _LOGGER .info (
320- "Unable to return energy statistics for %s during initialization, because it is not responding" ,
321- self .name ,
322- )
323- else :
324- _LOGGER .warning (
325- "Unable to return energy statistics for %s, because it is not responding" ,
326- self .name ,
327- )
332+ self ._log_no_energy_stats_update ()
328333 return None
329- # request node info update every 30 minutes.
334+ # Request node info update every 30 minutes.
330335 elif not self .skip_update (self ._node_info , 1800 ):
331336 if await self .node_info_update () is None :
332- if (
333- self ._initialization_delay_expired is not None
334- and datetime .now (tz = UTC ) < self ._initialization_delay_expired
335- ):
336- _LOGGER .info (
337- "Unable to return energy statistics for %s during initialization, because it is not responding" ,
338- self .name ,
339- )
340- else :
341- _LOGGER .warning (
342- "Unable to return energy statistics for %s, because it is not responding" ,
343- self .name ,
344- )
337+ self ._log_no_energy_stats_update ()
345338 return None
346339
347340 # Always request last energy log records at initial startup
@@ -351,24 +344,23 @@ async def energy_update(self) -> EnergyStatistics | None:
351344 )
352345
353346 if self ._energy_counters .log_rollover :
347+ # Try updating node_info
354348 if await self .node_info_update () is None :
355349 _LOGGER .debug (
356350 "async_energy_update | %s | Log rollover | node_info_update failed" ,
357351 self ._mac_in_str ,
358352 )
359353 return None
360354
355+ # Try collecting energy-stats for _current_log_address
361356 if not await self .energy_log_update (self ._current_log_address ):
362357 _LOGGER .debug (
363358 "async_energy_update | %s | Log rollover | energy_log_update failed" ,
364359 self ._mac_in_str ,
365360 )
366361 return None
367362
368- if (
369- self ._energy_counters .log_rollover
370- and self ._current_log_address is not None
371- ):
363+ if self ._current_log_address is not None :
372364 # Retry with previous log address as Circle node pointer to self._current_log_address
373365 # could be rolled over while the last log is at previous address/slot
374366 _prev_log_address , _ = calc_log_address (
@@ -414,11 +406,7 @@ async def energy_update(self) -> EnergyStatistics | None:
414406 self ._retrieve_energy_logs_task = create_task (
415407 self .get_missing_energy_logs ()
416408 )
417- else :
418- _LOGGER .debug (
419- "Skip creating task to update energy logs for node %s" ,
420- self ._mac_in_str ,
421- )
409+
422410 if (
423411 self ._initialization_delay_expired is not None
424412 and datetime .now (tz = UTC ) < self ._initialization_delay_expired
0 commit comments