@@ -158,6 +158,9 @@ def __init__(
158158 self ._entity_registry = er .async_get (self .hass )
159159 self ._device_registry = dr .async_get (self .hass )
160160
161+ # Add storage for scanner entity IDs
162+ self .scanner_entity_ids : dict [str , str ] = {}
163+
161164 # Track the list of Private BLE devices, noting their entity id
162165 # and current "last address".
163166 self .pb_state_sources : dict [str , str | None ] = {}
@@ -1105,6 +1108,9 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11051108 _previous_scannerlist = [device .address for device in self .devices .values () if device .is_scanner ]
11061109 _purge_scanners = _previous_scannerlist .copy ()
11071110
1111+ # Clear existing mappings to ensure fresh start
1112+ self .scanner_entity_ids .clear ()
1113+
11081114 # _LOGGER.error("Preserving %d current scanner entries", len(_previous_scannerlist))
11091115
11101116 # Find active HaBaseScanners in the backend, and only pay attention to those
@@ -1132,6 +1138,28 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11321138 hascanner .source ,
11331139 )
11341140 continue
1141+
1142+ if scanner_devreg :
1143+ _LOGGER .debug ("Found device entry id: %s" , scanner_devreg .id )
1144+
1145+ # Get all entities for this device
1146+ entities = list (self ._entity_registry .entities .get_entries_for_device_id (scanner_devreg .id ))
1147+
1148+ if len (entities ) > 1 :
1149+ # Device must have multiple entities. Reduce search space
1150+ entities = [entity for entity in entities if entity .domain in ("switch" , "light" )]
1151+ if entities :
1152+ # Take the first valid entity
1153+ self .scanner_entity_ids [scanner_address ] = entities [0 ].entity_id
1154+ _LOGGER .debug (
1155+ "Mapped scanner %s (%s) to entity %s" ,
1156+ scanner_devreg .name ,
1157+ scanner_address ,
1158+ entities [0 ].entity_id ,
1159+ )
1160+ else :
1161+ _LOGGER .debug ("No entity found for scanner %s" , scanner_address )
1162+
11351163 # _LOGGER.info("Great! Found scanner: %s (%s)", scanner_ha.name, scanner_ha.source)
11361164 # Since this scanner still exists, we won't purge it
11371165 if scanner_address in _purge_scanners :
0 commit comments