1 """The bluetooth integration apis.
3 These APIs are the only documented way to interact with the bluetooth integration.
6 from __future__
import annotations
9 from asyncio
import Future
10 from collections.abc
import Callable, Iterable
11 from typing
import TYPE_CHECKING, cast
13 from habluetooth
import (
15 BluetoothScannerDevice,
16 BluetoothScanningMode,
17 HaBleakScannerWrapper,
20 from home_assistant_bluetooth
import BluetoothServiceInfoBleak
25 from .const
import DATA_MANAGER
26 from .manager
import HomeAssistantBluetoothManager
27 from .match
import BluetoothCallbackMatcher
28 from .models
import BluetoothCallback, BluetoothChange, ProcessAdvertisementCallback
31 from bleak.backends.device
import BLEDevice
34 @singleton(DATA_MANAGER)
35 def _get_manager(hass: HomeAssistant) -> HomeAssistantBluetoothManager:
36 """Get the bluetooth manager."""
37 return cast(HomeAssistantBluetoothManager, get_manager())
42 """Return a HaBleakScannerWrapper.
44 This is a wrapper around our BleakScanner singleton that allows
45 multiple integrations to share the same BleakScanner.
47 return HaBleakScannerWrapper()
52 """Return a scanner for a given source.
54 This method is only intended to be used by integrations that implement
55 a bluetooth client and need to interact with a scanner directly.
57 It is not intended to be used by integrations that need to interact
65 """Return the number of scanners currently in use."""
71 hass: HomeAssistant, connectable: bool =
True
72 ) -> Iterable[BluetoothServiceInfoBleak]:
73 """Return the discovered devices list."""
79 hass: HomeAssistant, address: str, connectable: bool =
True
80 ) -> BluetoothServiceInfoBleak |
None:
81 """Return the last service info for an address."""
87 hass: HomeAssistant, address: str, connectable: bool =
True
88 ) -> BLEDevice |
None:
89 """Return BLEDevice for an address if its present."""
95 hass: HomeAssistant, address: str, connectable: bool =
True
96 ) -> list[BluetoothScannerDevice]:
97 """Return all discovered BluetoothScannerDevice for an address."""
103 hass: HomeAssistant, address: str, connectable: bool =
True
105 """Check if an address is present in the bluetooth device list."""
112 callback: BluetoothCallback,
113 match_dict: BluetoothCallbackMatcher |
None,
114 mode: BluetoothScanningMode,
115 ) -> Callable[[],
None]:
116 """Register to receive a callback on bluetooth change.
118 mode is currently not used as we only support active scanning.
119 Passive scanning will be available in the future. The flag
120 is required to be present to avoid a future breaking change
121 when we support passive scanning.
123 Returns a callback that can be used to cancel the registration.
130 callback: ProcessAdvertisementCallback,
131 match_dict: BluetoothCallbackMatcher,
132 mode: BluetoothScanningMode,
134 ) -> BluetoothServiceInfoBleak:
135 """Process advertisements until callback returns true or timeout expires."""
136 done: Future[BluetoothServiceInfoBleak] = hass.loop.create_future()
139 def _async_discovered_device(
140 service_info: BluetoothServiceInfoBleak, change: BluetoothChange
142 if not done.done()
and callback(service_info):
143 done.set_result(service_info)
146 _async_discovered_device, match_dict
150 async
with asyncio.timeout(timeout):
159 callback: Callable[[BluetoothServiceInfoBleak],
None],
161 connectable: bool =
True,
162 ) -> Callable[[],
None]:
163 """Register to receive a callback when an address is unavailable.
165 Returns a callback that can be used to cancel the registration.
172 """Trigger discovery of devices which have already been seen."""
179 scanner: BaseHaScanner,
180 connection_slots: int |
None =
None,
182 """Register a BleakScanner."""
189 ) -> Callable[[BluetoothServiceInfoBleak],
None]:
190 """Get the advertisement callback."""
196 hass: HomeAssistant, address: str
198 """Get the learned advertising interval for a MAC address."""
204 hass: HomeAssistant, address: str
206 """Get the fallback availability timeout for a MAC address."""
212 hass: HomeAssistant, address: str, interval: float
214 """Override the fallback availability timeout for a MAC address."""
list[BluetoothScannerDevice] async_scanner_devices_by_address(HomeAssistant hass, str address, bool connectable=True)
Callable[[BluetoothServiceInfoBleak], None] async_get_advertisement_callback(HomeAssistant hass)
BLEDevice|None async_ble_device_from_address(HomeAssistant hass, str address, bool connectable=True)
CALLBACK_TYPE async_register_scanner(HomeAssistant hass, BaseHaScanner scanner, int|None connection_slots=None)
bool async_address_present(HomeAssistant hass, str address, bool connectable=True)
Callable[[], None] async_register_callback(HomeAssistant hass, BluetoothCallback callback, BluetoothCallbackMatcher|None match_dict, BluetoothScanningMode mode)
float|None async_get_learned_advertising_interval(HomeAssistant hass, str address)
None async_set_fallback_availability_interval(HomeAssistant hass, str address, float interval)
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
float|None async_get_fallback_availability_interval(HomeAssistant hass, str address)
None async_rediscover_address(HomeAssistant hass, str address)
Callable[[], None] async_track_unavailable(HomeAssistant hass, Callable[[BluetoothServiceInfoBleak], None] callback, str address, bool connectable=True)
int async_scanner_count(HomeAssistant hass, bool connectable=True)
BluetoothServiceInfoBleak async_process_advertisements(HomeAssistant hass, ProcessAdvertisementCallback callback, BluetoothCallbackMatcher match_dict, BluetoothScanningMode mode, int timeout)
BaseHaScanner|None async_scanner_by_source(HomeAssistant hass, str source)
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)
HaBleakScannerWrapper async_get_scanner(HomeAssistant hass)
HomeAssistantBluetoothManager _get_manager(HomeAssistant hass)