Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """The bluetooth integration apis.
2 
3 These APIs are the only documented way to interact with the bluetooth integration.
4 """
5 
6 from __future__ import annotations
7 
8 import asyncio
9 from asyncio import Future
10 from collections.abc import Callable, Iterable
11 from typing import TYPE_CHECKING, cast
12 
13 from habluetooth import (
14  BaseHaScanner,
15  BluetoothScannerDevice,
16  BluetoothScanningMode,
17  HaBleakScannerWrapper,
18  get_manager,
19 )
20 from home_assistant_bluetooth import BluetoothServiceInfoBleak
21 
22 from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback as hass_callback
23 from homeassistant.helpers.singleton import singleton
24 
25 from .const import DATA_MANAGER
26 from .manager import HomeAssistantBluetoothManager
27 from .match import BluetoothCallbackMatcher
28 from .models import BluetoothCallback, BluetoothChange, ProcessAdvertisementCallback
29 
30 if TYPE_CHECKING:
31  from bleak.backends.device import BLEDevice
32 
33 
34 @singleton(DATA_MANAGER)
35 def _get_manager(hass: HomeAssistant) -> HomeAssistantBluetoothManager:
36  """Get the bluetooth manager."""
37  return cast(HomeAssistantBluetoothManager, get_manager())
38 
39 
40 @hass_callback
41 def async_get_scanner(hass: HomeAssistant) -> HaBleakScannerWrapper:
42  """Return a HaBleakScannerWrapper.
43 
44  This is a wrapper around our BleakScanner singleton that allows
45  multiple integrations to share the same BleakScanner.
46  """
47  return HaBleakScannerWrapper()
48 
49 
50 @hass_callback
51 def async_scanner_by_source(hass: HomeAssistant, source: str) -> BaseHaScanner | None:
52  """Return a scanner for a given source.
53 
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.
56 
57  It is not intended to be used by integrations that need to interact
58  with a device.
59  """
60  return _get_manager(hass).async_scanner_by_source(source)
61 
62 
63 @hass_callback
64 def async_scanner_count(hass: HomeAssistant, connectable: bool = True) -> int:
65  """Return the number of scanners currently in use."""
66  return _get_manager(hass).async_scanner_count(connectable)
67 
68 
69 @hass_callback
71  hass: HomeAssistant, connectable: bool = True
72 ) -> Iterable[BluetoothServiceInfoBleak]:
73  """Return the discovered devices list."""
74  return _get_manager(hass).async_discovered_service_info(connectable)
75 
76 
77 @hass_callback
79  hass: HomeAssistant, address: str, connectable: bool = True
80 ) -> BluetoothServiceInfoBleak | None:
81  """Return the last service info for an address."""
82  return _get_manager(hass).async_last_service_info(address, connectable)
83 
84 
85 @hass_callback
87  hass: HomeAssistant, address: str, connectable: bool = True
88 ) -> BLEDevice | None:
89  """Return BLEDevice for an address if its present."""
90  return _get_manager(hass).async_ble_device_from_address(address, connectable)
91 
92 
93 @hass_callback
95  hass: HomeAssistant, address: str, connectable: bool = True
96 ) -> list[BluetoothScannerDevice]:
97  """Return all discovered BluetoothScannerDevice for an address."""
98  return _get_manager(hass).async_scanner_devices_by_address(address, connectable)
99 
100 
101 @hass_callback
103  hass: HomeAssistant, address: str, connectable: bool = True
104 ) -> bool:
105  """Check if an address is present in the bluetooth device list."""
106  return _get_manager(hass).async_address_present(address, connectable)
107 
108 
109 @hass_callback
111  hass: HomeAssistant,
112  callback: BluetoothCallback,
113  match_dict: BluetoothCallbackMatcher | None,
114  mode: BluetoothScanningMode,
115 ) -> Callable[[], None]:
116  """Register to receive a callback on bluetooth change.
117 
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.
122 
123  Returns a callback that can be used to cancel the registration.
124  """
125  return _get_manager(hass).async_register_callback(callback, match_dict)
126 
127 
129  hass: HomeAssistant,
130  callback: ProcessAdvertisementCallback,
131  match_dict: BluetoothCallbackMatcher,
132  mode: BluetoothScanningMode,
133  timeout: int,
134 ) -> BluetoothServiceInfoBleak:
135  """Process advertisements until callback returns true or timeout expires."""
136  done: Future[BluetoothServiceInfoBleak] = hass.loop.create_future()
137 
138  @hass_callback
139  def _async_discovered_device(
140  service_info: BluetoothServiceInfoBleak, change: BluetoothChange
141  ) -> None:
142  if not done.done() and callback(service_info):
143  done.set_result(service_info)
144 
145  unload = _get_manager(hass).async_register_callback(
146  _async_discovered_device, match_dict
147  )
148 
149  try:
150  async with asyncio.timeout(timeout):
151  return await done
152  finally:
153  unload()
154 
155 
156 @hass_callback
158  hass: HomeAssistant,
159  callback: Callable[[BluetoothServiceInfoBleak], None],
160  address: str,
161  connectable: bool = True,
162 ) -> Callable[[], None]:
163  """Register to receive a callback when an address is unavailable.
164 
165  Returns a callback that can be used to cancel the registration.
166  """
167  return _get_manager(hass).async_track_unavailable(callback, address, connectable)
168 
169 
170 @hass_callback
171 def async_rediscover_address(hass: HomeAssistant, address: str) -> None:
172  """Trigger discovery of devices which have already been seen."""
174 
175 
176 @hass_callback
178  hass: HomeAssistant,
179  scanner: BaseHaScanner,
180  connection_slots: int | None = None,
181 ) -> CALLBACK_TYPE:
182  """Register a BleakScanner."""
183  return _get_manager(hass).async_register_scanner(scanner, connection_slots)
184 
185 
186 @hass_callback
188  hass: HomeAssistant,
189 ) -> Callable[[BluetoothServiceInfoBleak], None]:
190  """Get the advertisement callback."""
191  return _get_manager(hass).scanner_adv_received
192 
193 
194 @hass_callback
196  hass: HomeAssistant, address: str
197 ) -> float | None:
198  """Get the learned advertising interval for a MAC address."""
200 
201 
202 @hass_callback
204  hass: HomeAssistant, address: str
205 ) -> float | None:
206  """Get the fallback availability timeout for a MAC address."""
208 
209 
210 @hass_callback
212  hass: HomeAssistant, address: str, interval: float
213 ) -> None:
214  """Override the fallback availability timeout for a MAC address."""
list[BluetoothScannerDevice] async_scanner_devices_by_address(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:96
Callable[[BluetoothServiceInfoBleak], None] async_get_advertisement_callback(HomeAssistant hass)
Definition: api.py:189
BLEDevice|None async_ble_device_from_address(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:88
CALLBACK_TYPE async_register_scanner(HomeAssistant hass, BaseHaScanner scanner, int|None connection_slots=None)
Definition: api.py:181
bool async_address_present(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:104
Callable[[], None] async_register_callback(HomeAssistant hass, BluetoothCallback callback, BluetoothCallbackMatcher|None match_dict, BluetoothScanningMode mode)
Definition: api.py:115
float|None async_get_learned_advertising_interval(HomeAssistant hass, str address)
Definition: api.py:197
None async_set_fallback_availability_interval(HomeAssistant hass, str address, float interval)
Definition: api.py:213
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
Definition: api.py:72
float|None async_get_fallback_availability_interval(HomeAssistant hass, str address)
Definition: api.py:205
None async_rediscover_address(HomeAssistant hass, str address)
Definition: api.py:171
Callable[[], None] async_track_unavailable(HomeAssistant hass, Callable[[BluetoothServiceInfoBleak], None] callback, str address, bool connectable=True)
Definition: api.py:162
int async_scanner_count(HomeAssistant hass, bool connectable=True)
Definition: api.py:64
BluetoothServiceInfoBleak async_process_advertisements(HomeAssistant hass, ProcessAdvertisementCallback callback, BluetoothCallbackMatcher match_dict, BluetoothScanningMode mode, int timeout)
Definition: api.py:134
BaseHaScanner|None async_scanner_by_source(HomeAssistant hass, str source)
Definition: api.py:51
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:80
HaBleakScannerWrapper async_get_scanner(HomeAssistant hass)
Definition: api.py:41
HomeAssistantBluetoothManager _get_manager(HomeAssistant hass)
Definition: api.py:35