@@ -518,12 +518,12 @@ async def _get_initial_energy_logs(self) -> None:
518518 max_addresses_to_collect , ceil (datetime .now (tz = UTC ).hour / factor ) + 1
519519 )
520520 log_address = self ._current_log_address
521+ any_updates = False
521522 while total_addresses > 0 :
522523 result , slots_empty = await self .energy_log_update (
523524 log_address , save_cache = False
524525 )
525- if result and not slots_empty :
526- # (address with outdated data in slots is stored as with None data!)
526+ if result and slots_empty :
527527 # Stop initial log collection when an address contains no (None) or outdated data
528528 # Outdated data can indicate a EnergyLog address rollover: from address 6014 to 0
529529 _LOGGER .debug (
@@ -532,10 +532,11 @@ async def _get_initial_energy_logs(self) -> None:
532532 )
533533 break
534534
535+ any_updates |= (not slots_empty )
535536 log_address , _ = calc_log_address (log_address , 1 , - 4 )
536537 total_addresses -= 1
537538
538- if self ._cache_enabled :
539+ if self ._cache_enabled and any_updates :
539540 await self .save_cache ()
540541
541542 async def get_missing_energy_logs (self ) -> None :
@@ -560,8 +561,9 @@ async def get_missing_energy_logs(self) -> None:
560561 create_task (self .energy_log_update (address , save_cache = False ))
561562 for address in missing_addresses
562563 ]
564+ any_updates = False
563565 for idx , task in enumerate (tasks ):
564- result = await task
566+ result , slots_empty = await task
565567 # When an energy log collection task returns False, stop and cancel the remaining tasks
566568 if not result :
567569 to_cancel = tasks [idx + 1 :]
@@ -571,7 +573,9 @@ async def get_missing_energy_logs(self) -> None:
571573 await gather (* to_cancel , return_exceptions = True )
572574 break
573575
574- if self ._cache_enabled :
576+ any_updates |= (not slots_empty )
577+
578+ if self ._cache_enabled and any_updates :
575579 await self .save_cache ()
576580
577581 async def energy_log_update (
0 commit comments