@@ -819,28 +819,43 @@ def _logs_missing(self, from_timestamp: datetime) -> list[int] | None:
819819 first = self ._first_log_reference ()
820820 last = self ._last_log_reference ()
821821
822- if None in first or None in last :
823- _LOGGER .debug (
824- "_logs_missing | %s | first=%s, last=%s" , self ._mac , first , last
825- )
822+ if not self ._valid_log_references (first , last ):
826823 return None
827824
825+ if self ._is_single_unique_log (first , last ):
826+ return None
827+
828+ if (missing := self ._find_missing_in_range (first , last , from_timestamp )):
829+ return missing
830+
828831 first_address , first_slot = first
829- last_address , last_slot = last
832+ if not self ._is_first_log_recent_enough (first_address , first_slot , from_timestamp ):
833+ return []
830834
831- if (
832- first == last and
833- self ._logs [first_address ][first_slot ].timestamp == self ._logs [last_address ][last_slot ].timestamp
834- ):
835- return None # Only one unique log, not enough to work with
835+ return self ._calculate_missing_before_first (first_address , first_slot , from_timestamp )
836836
837- missing = []
838- _LOGGER .debug (
839- "_logs_missing | %s | first_address=%s, last_address=%s" ,
840- self ._mac , first_address , last_address
837+ # --- Helper Methods for _logs_missing() ---
838+
839+ def _valid_log_references (self , first , last ) -> bool :
840+ if None in first or None in last :
841+ _LOGGER .debug ("_logs_missing | %s | first=%s, last=%s" , self ._mac , first , last )
842+ return False
843+ return True
844+
845+ def _is_single_unique_log (self , first , last ) -> bool :
846+ if first != last :
847+ return False
848+ address , slot = first
849+ return (
850+ self ._logs [address ][slot ].timestamp
851+ == self ._logs [address ][slot ].timestamp
841852 )
842853
843- # Check for missing logs in the collected range
854+ def _find_missing_in_range (self , first , last , from_timestamp ) -> list [int ]:
855+ first_address , first_slot = first
856+ last_address , last_slot = last
857+ missing = []
858+
844859 address , slot = last_address , last_slot
845860 while (address , slot ) != (first_address , first_slot ):
846861 address , slot = calc_log_address (address , slot , - 1 )
@@ -853,49 +868,44 @@ def _logs_missing(self, from_timestamp: datetime) -> list[int] | None:
853868
854869 if missing :
855870 _LOGGER .debug ("_logs_missing | %s | missing in range=%s" , self ._mac , missing )
856- return missing
857-
858- # Check if first slot exists and is recent enough
859- if (
860- first_address not in self ._logs or
861- first_slot not in self ._logs [first_address ] or
862- self ._logs [first_address ][first_slot ].timestamp < from_timestamp
863- ):
864- return []
871+ return missing
865872
866- # Estimate log interval
867- log_interval = self ._log_interval_consumption or self ._log_interval_production
868- if (
869- self ._log_interval_production is not None and
870- log_interval is not None and
871- self ._log_interval_production < log_interval
872- ):
873- log_interval = self ._log_interval_production
873+ def _is_first_log_recent_enough (self , address , slot , from_timestamp ) -> bool :
874+ return (
875+ address in self ._logs and
876+ slot in self ._logs [address ] and
877+ self ._logs [address ][slot ].timestamp >= from_timestamp
878+ )
874879
875- if log_interval is None :
880+ def _calculate_missing_before_first (self , first_address , first_slot , from_timestamp ) -> list [int ] | None :
881+ if (log_interval := self ._get_log_interval ()) is None :
876882 return None
877883
878- # Walk backward before the first known log to find additional missing logs
879884 timestamp = self ._logs [first_address ][first_slot ].timestamp
880885 calc_timestamp = timestamp - timedelta (minutes = log_interval )
881886 address , slot = calc_log_address (first_address , first_slot , - 1 )
882887
883- _LOGGER .debug (
884- "_logs_missing | %s | calculated timestamp=%s" , self ._mac , calc_timestamp
885- )
886-
888+ missing = []
887889 while calc_timestamp > from_timestamp :
888890 if (address , slot ) == (self ._first_empty_log_address , self ._first_empty_log_slot ):
889891 break
890- if address not in missing :
891- missing .append (address )
892+ missing = update_addresses (address , missing )
892893 calc_timestamp -= timedelta (minutes = log_interval )
893894 address , slot = calc_log_address (address , slot , - 1 )
894895
895896 missing .sort (reverse = True )
896897 _LOGGER .debug ("_logs_missing | %s | calculated missing=%s" , self ._mac , missing )
897898 return missing
898899
900+ def _get_log_interval (self ) -> int | None :
901+ log_interval = self ._log_interval_consumption or self ._log_interval_production
902+ if (
903+ self ._log_interval_production is not None and
904+ log_interval is not None and
905+ self ._log_interval_production < log_interval
906+ ):
907+ log_interval = self ._log_interval_production
908+ return log_interval
899909
900910 def _last_known_duration (self ) -> timedelta :
901911 """Duration for last known logs."""
0 commit comments