@@ -255,7 +255,7 @@ async def _calibration_update_state(
255255 gain_b : float | None ,
256256 off_noise : float | None ,
257257 off_tot : float | None ,
258- load_from_cache = False ,
258+ load_from_cache : bool = False ,
259259 ) -> bool :
260260 """Process new energy calibration settings. Returns True if successful."""
261261 if gain_a is None or gain_b is None or off_noise is None or off_tot is None :
@@ -434,84 +434,78 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
434434 )
435435 return None
436436
437- async def get_missing_energy_logs (self ) -> None :
438- """Task to retrieve missing energy logs."""
439- self ._energy_counters .update ()
437+ async def _get_initial_energy_logs (self ) -> None :
438+ """Collect initial energy logs from the last 10 log addresses."""
440439 if self ._current_log_address is None :
441- return None
442-
443- if (missing_addresses := self ._energy_counters .log_addresses_missing ) is None :
444- _LOGGER .debug (
445- "Start collecting initial energy logs from the last 10 log addresses for node %s." ,
446- self ._mac_in_str ,
447- )
448- total_addresses = 11
449- log_address = self ._current_log_address
450- prev_address_timestamp : datetime | None = None
451- while total_addresses > 0 :
452- if not await self .energy_log_update (log_address ):
453- # Handle case with None-data in all address slots
454- _LOGGER .debug (
455- "Energy None-data collected from log address %s, stopping collection" ,
456- log_address ,
457- )
458- break
440+ return
459441
460- # Check if the most recent timestamp of an earlier address is recent
461- # (within 2/4 * log_interval plus 5 mins margin)
462- log_interval = self .energy_consumption_interval
463- _LOGGER .debug ("log_interval: %s" , log_interval )
464- _LOGGER .debug (
465- "last_collected_energy_timestamp: %s" ,
466- self ._last_collected_energy_timestamp ,
467- )
442+ _LOGGER .debug (
443+ "Start collecting initial energy logs from the last 10 log addresses for node %s." ,
444+ self ._mac_in_str ,
445+ )
446+ total_addresses = 11
447+ log_address = self ._current_log_address
448+ prev_address_timestamp : datetime | None = None
449+ while total_addresses > 0 :
450+ if not await self .energy_log_update (log_address ):
451+ # Handle case with None-data in all address slots
468452 _LOGGER .debug (
469- "energy_production_interval: %s " ,
470- self . energy_production_interval ,
453+ "Energy None-data collected from log address %s, stopping collection " ,
454+ log_address ,
471455 )
472- factor = 4
473- if self .energy_production_interval is not None :
474- factor = 2
456+ break
457+
458+ # Check if the most recent timestamp of an earlier address is recent
459+ # (within 2/4 * log_interval plus 5 mins margin)
460+ log_interval = self .energy_consumption_interval
461+ factor = 2 if self .energy_production_interval is not None else 4
475462
463+ if log_interval is not None :
464+ max_gap_minutes = (factor * log_interval ) + 5
476465 if log_address == self ._current_log_address :
477466 if (
478467 self ._last_collected_energy_timestamp is not None
479- and log_interval is not None
480468 and (
481469 datetime .now (tz = UTC ) - self ._last_collected_energy_timestamp
482- ).total_seconds () // 60 > (factor * log_interval ) + 5 # minutes
483- ):
484- _LOGGER .debug (
485- "Energy data collected from the current log address is outdated, stopping collection"
486- )
487- break
488- else :
489- _LOGGER .debug ("prev_address_timestamp: %s" , prev_address_timestamp )
490- if (
491- self ._last_collected_energy_timestamp is not None
492- and log_interval is not None
493- and prev_address_timestamp is not None
494- and (
495- prev_address_timestamp
496- - self ._last_collected_energy_timestamp
497470 ).total_seconds ()
498471 // 60
499- > ( factor * log_interval ) + 5 # minutes
472+ > max_gap_minutes
500473 ):
501474 _LOGGER .debug (
502- "Collected energy data is outdated, stopping collection"
475+ "Energy data collected from the current log address is outdated, stopping collection"
503476 )
504477 break
478+ elif (
479+ prev_address_timestamp is not None
480+ and self ._last_collected_energy_timestamp is not None
481+ and (
482+ prev_address_timestamp - self ._last_collected_energy_timestamp
483+ ).total_seconds ()
484+ // 60
485+ > max_gap_minutes
486+ ):
487+ _LOGGER .debug (
488+ "Collected energy data is outdated, stopping collection"
489+ )
490+ break
505491
506- if self ._last_collected_energy_timestamp is not None :
507- prev_address_timestamp = self ._last_collected_energy_timestamp
492+ if self ._last_collected_energy_timestamp is not None :
493+ prev_address_timestamp = self ._last_collected_energy_timestamp
508494
509- log_address , _ = calc_log_address (log_address , 1 , - 4 )
510- total_addresses -= 1
495+ log_address , _ = calc_log_address (log_address , 1 , - 4 )
496+ total_addresses -= 1
511497
512- if self ._cache_enabled :
513- await self ._energy_log_records_save_to_cache ()
498+ if self ._cache_enabled :
499+ await self ._energy_log_records_save_to_cache ()
500+
501+ async def get_missing_energy_logs (self ) -> None :
502+ """Task to retrieve missing energy logs."""
503+ self ._energy_counters .update ()
504+ if self ._current_log_address is None :
505+ return
514506
507+ if (missing_addresses := self ._energy_counters .log_addresses_missing ) is None :
508+ await self ._get_initial_energy_logs ()
515509 return
516510
517511 _LOGGER .debug ("Task created to get missing logs of %s" , self ._mac_in_str )
@@ -813,7 +807,9 @@ async def _relay_update_state(
813807 _LOGGER .debug ("Saving relay state update to cache for %s" , self ._mac_in_str )
814808 await self .save_cache ()
815809
816- async def _relay_update_lock (self , state : bool , load_from_cache = False ) -> None :
810+ async def _relay_update_lock (
811+ self , state : bool , load_from_cache : bool = False
812+ ) -> None :
817813 """Process relay lock update."""
818814 state_update = False
819815 if state :
@@ -1302,18 +1298,18 @@ async def energy_reset_request(self) -> None:
13021298 _LOGGER .warning ("Energy reset for Node %s successful" , self ._mac_in_str )
13031299
13041300 # Follow up by an energy-intervals (re)set
1305- request = CircleMeasureIntervalRequest (
1301+ interval_request = CircleMeasureIntervalRequest (
13061302 self ._send ,
13071303 self ._mac_in_bytes ,
13081304 DEFAULT_CONS_INTERVAL ,
13091305 NO_PRODUCTION_INTERVAL ,
13101306 )
1311- if (response := await request .send ()) is None :
1307+ if (interval_response := await interval_request .send ()) is None :
13121308 raise NodeError ("No response for CircleMeasureIntervalRequest" )
13131309
1314- if response .response_type != NodeResponseType .POWER_LOG_INTERVAL_ACCEPTED :
1310+ if interval_response .response_type != NodeResponseType .POWER_LOG_INTERVAL_ACCEPTED :
13151311 raise MessageError (
1316- f"Unknown NodeResponseType '{ response .response_type .name } ' received"
1312+ f"Unknown NodeResponseType '{ interval_response .response_type .name } ' received"
13171313 )
13181314 _LOGGER .warning ("Resetting energy intervals to default (= consumption only)" )
13191315
0 commit comments