Home Assistant Unofficial Reference 2024.12.1
bluetooth.py
Go to the documentation of this file.
1 """Bluetooth support for esphome."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 from typing import TYPE_CHECKING
7 
8 from aioesphomeapi import APIClient, DeviceInfo
9 from bleak_esphome import connect_scanner
10 from bleak_esphome.backend.cache import ESPHomeBluetoothCache
11 
12 from homeassistant.components.bluetooth import async_register_scanner
13 from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback as hass_callback
14 
15 from .entry_data import RuntimeEntryData
16 
17 
18 @hass_callback
19 def _async_unload(unload_callbacks: list[CALLBACK_TYPE]) -> None:
20  """Cancel all the callbacks on unload."""
21  for callback in unload_callbacks:
22  callback()
23 
24 
25 @hass_callback
27  hass: HomeAssistant,
28  entry_data: RuntimeEntryData,
29  cli: APIClient,
30  device_info: DeviceInfo,
31  cache: ESPHomeBluetoothCache,
32 ) -> CALLBACK_TYPE:
33  """Connect scanner."""
34  client_data = connect_scanner(cli, device_info, cache, entry_data.available)
35  entry_data.bluetooth_device = client_data.bluetooth_device
36  client_data.disconnect_callbacks = entry_data.disconnect_callbacks
37  scanner = client_data.scanner
38  if TYPE_CHECKING:
39  assert scanner is not None
40  return partial(
41  _async_unload,
42  [
43  async_register_scanner(hass, scanner),
44  scanner.async_setup(),
45  ],
46  )
CALLBACK_TYPE async_register_scanner(HomeAssistant hass, BaseHaScanner scanner, int|None connection_slots=None)
Definition: api.py:181
None _async_unload(list[CALLBACK_TYPE] unload_callbacks)
Definition: bluetooth.py:19
CALLBACK_TYPE async_connect_scanner(HomeAssistant hass, RuntimeEntryData entry_data, APIClient cli, DeviceInfo device_info, ESPHomeBluetoothCache cache)
Definition: bluetooth.py:32