Reduce complexity of bluetooth scanners for local adapters (#81940)

This is a small refactor to align the bluetooth scanner class
to the same design used in the esphome scanner to pass the
callback for new service infos in the constructor.

The original thinking was that multiple callbacks would
be needed to send the service infos to different paths,
however with the creation of a central manager, this
designed turned out to not be needed and can be
simplified
This commit is contained in:
J. Nick Koston 2022-11-11 07:59:06 -06:00 committed by GitHub
parent 6517e3e21e
commit 89959e7cda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 34 deletions

View File

@ -425,15 +425,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
passive = entry.options.get(CONF_PASSIVE)
mode = BluetoothScanningMode.PASSIVE if passive else BluetoothScanningMode.ACTIVE
scanner = HaScanner(hass, mode, adapter, address)
new_info_callback = async_get_advertisement_callback(hass)
scanner = HaScanner(hass, mode, adapter, address, new_info_callback)
try:
scanner.async_setup()
except RuntimeError as err:
raise ConfigEntryNotReady(
f"{adapter_human_name(adapter, address)}: {err}"
) from err
info_callback = async_get_advertisement_callback(hass)
entry.async_on_unload(scanner.async_register_callback(info_callback))
try:
await scanner.async_start()
except ScannerStartError as err:

View File

@ -125,6 +125,7 @@ class HaScanner(BaseHaScanner):
mode: BluetoothScanningMode,
adapter: str,
address: str,
new_info_callback: Callable[[BluetoothServiceInfoBleak], None],
) -> None:
"""Init bluetooth discovery."""
source = address if address != DEFAULT_ADDRESS else adapter or SOURCE_LOCAL
@ -135,7 +136,7 @@ class HaScanner(BaseHaScanner):
self._cancel_watchdog: CALLBACK_TYPE | None = None
self._last_detection = 0.0
self._start_time = 0.0
self._callbacks: list[Callable[[BluetoothServiceInfoBleak], None]] = []
self._new_info_callback = new_info_callback
self.name = adapter_human_name(adapter, address)
@property
@ -168,22 +169,6 @@ class HaScanner(BaseHaScanner):
"start_time": self._start_time,
}
@hass_callback
def async_register_callback(
self, callback: Callable[[BluetoothServiceInfoBleak], None]
) -> CALLBACK_TYPE:
"""Register a callback.
Currently this is used to feed the callbacks into the
central manager.
"""
def _remove() -> None:
self._callbacks.remove(callback)
self._callbacks.append(callback)
return _remove
@hass_callback
def _async_detection_callback(
self,
@ -206,7 +191,8 @@ class HaScanner(BaseHaScanner):
# as the adapter is in a failure
# state if all the data is empty.
self._last_detection = callback_time
service_info = BluetoothServiceInfoBleak(
self._new_info_callback(
BluetoothServiceInfoBleak(
name=advertisement_data.local_name or device.name or device.address,
address=device.address,
rssi=advertisement_data.rssi,
@ -219,8 +205,7 @@ class HaScanner(BaseHaScanner):
connectable=True,
time=callback_time,
)
for callback in self._callbacks:
callback(service_info)
)
async def async_start(self) -> None:
"""Start bluetooth scanner."""