diff --git a/src/bleak_esphome/backend/scanner.py b/src/bleak_esphome/backend/scanner.py index a47a78d..46db436 100644 --- a/src/bleak_esphome/backend/scanner.py +++ b/src/bleak_esphome/backend/scanner.py @@ -38,7 +38,11 @@ def async_on_raw_advertisements( ) -> None: """Call the registered callback.""" now = MONOTONIC_TIME() - for adv in raw.advertisements: + # Avoid enumeration of raw.advertisements in the loop + # because protobuf will always have an internal exception + # when the list is exhausted. + adv_len_idx = len(raw.advertisements) - 1 + for i, adv in enumerate(raw.advertisements): self._async_on_advertisement( int_to_bluetooth_address(adv.address), adv.rssi, @@ -46,3 +50,6 @@ def async_on_raw_advertisements( {"address_type": adv.address_type}, now, ) + if i == adv_len_idx: + # Break at the end since we are done + break