@@ -160,6 +160,9 @@ def __init__(
160160 self ._entity_registry = er .async_get (self .hass )
161161 self ._device_registry = dr .async_get (self .hass )
162162
163+ # Add storage for scanner entity IDs
164+ self .scanner_entity_ids : dict [str , str ] = {}
165+
163166 # Track the list of Private BLE devices, noting their entity id
164167 # and current "last address".
165168 self .pb_state_sources : dict [str , str | None ] = {}
@@ -1120,6 +1123,9 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11201123 _previous_scannerlist = [device .address for device in self .devices .values () if device .is_scanner ]
11211124 _purge_scanners = _previous_scannerlist .copy ()
11221125
1126+ # Clear existing mappings to ensure fresh start
1127+ self .scanner_entity_ids .clear ()
1128+
11231129 # _LOGGER.error("Preserving %d current scanner entries", len(_previous_scannerlist))
11241130
11251131 # Find active HaBaseScanners in the backend, and only pay attention to those
@@ -1147,6 +1153,28 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11471153 hascanner .source ,
11481154 )
11491155 continue
1156+
1157+ if scanner_devreg :
1158+ _LOGGER .debug ("Found device entry id: %s" , scanner_devreg .id )
1159+
1160+ # Get all entities for this device
1161+ entities = list (self ._entity_registry .entities .get_entries_for_device_id (scanner_devreg .id ))
1162+
1163+ if len (entities ) > 1 :
1164+ # Device must have multiple entities. Reduce search space
1165+ entities = [entity for entity in entities if entity .domain in ("switch" , "light" )]
1166+ if entities :
1167+ # Take the first valid entity
1168+ self .scanner_entity_ids [scanner_address ] = entities [0 ].entity_id
1169+ _LOGGER .debug (
1170+ "Mapped scanner %s (%s) to entity %s" ,
1171+ scanner_devreg .name ,
1172+ scanner_address ,
1173+ entities [0 ].entity_id ,
1174+ )
1175+ else :
1176+ _LOGGER .debug ("No entity found for scanner %s" , scanner_address )
1177+
11501178 # _LOGGER.info("Great! Found scanner: %s (%s)", scanner_ha.name, scanner_ha.source)
11511179 # Since this scanner still exists, we won't purge it
11521180 if scanner_address in _purge_scanners :
0 commit comments