2022-07-08 23:55:31 +00:00
|
|
|
"""The bluetooth integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-28 18:10:37 +00:00
|
|
|
from asyncio import Future
|
2022-07-08 23:55:31 +00:00
|
|
|
from collections.abc import Callable
|
2022-08-17 00:52:53 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2022-07-08 23:55:31 +00:00
|
|
|
|
2022-07-28 18:10:37 +00:00
|
|
|
import async_timeout
|
2022-07-08 23:55:31 +00:00
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2022-08-17 20:51:56 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2022-08-17 00:52:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback as hass_callback
|
2022-08-17 20:51:56 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2022-07-08 23:55:31 +00:00
|
|
|
from homeassistant.helpers import discovery_flow
|
2022-07-24 21:39:53 +00:00
|
|
|
from homeassistant.loader import async_get_bluetooth
|
2022-08-17 00:52:53 +00:00
|
|
|
|
2022-08-17 20:51:56 +00:00
|
|
|
from . import models
|
|
|
|
from .const import CONF_ADAPTER, DATA_MANAGER, DOMAIN, SOURCE_LOCAL
|
2022-08-17 00:52:53 +00:00
|
|
|
from .manager import BluetoothManager
|
|
|
|
from .match import BluetoothCallbackMatcher, IntegrationMatcher
|
|
|
|
from .models import (
|
|
|
|
BluetoothCallback,
|
|
|
|
BluetoothChange,
|
|
|
|
BluetoothScanningMode,
|
|
|
|
BluetoothServiceInfo,
|
|
|
|
BluetoothServiceInfoBleak,
|
|
|
|
HaBleakScannerWrapper,
|
|
|
|
ProcessAdvertisementCallback,
|
2022-07-24 21:39:53 +00:00
|
|
|
)
|
2022-08-17 20:51:56 +00:00
|
|
|
from .scanner import HaScanner, create_bleak_scanner
|
2022-07-25 14:52:35 +00:00
|
|
|
from .util import async_get_bluetooth_adapters
|
2022-07-08 23:55:31 +00:00
|
|
|
|
2022-08-01 15:54:06 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from bleak.backends.device import BLEDevice
|
|
|
|
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
|
|
|
|
2022-08-17 00:52:53 +00:00
|
|
|
__all__ = [
|
|
|
|
"async_ble_device_from_address",
|
|
|
|
"async_discovered_service_info",
|
|
|
|
"async_get_scanner",
|
|
|
|
"async_process_advertisements",
|
|
|
|
"async_rediscover_address",
|
|
|
|
"async_register_callback",
|
|
|
|
"async_track_unavailable",
|
|
|
|
"BluetoothServiceInfo",
|
|
|
|
"BluetoothServiceInfoBleak",
|
|
|
|
"BluetoothScanningMode",
|
|
|
|
"BluetoothCallback",
|
|
|
|
"SOURCE_LOCAL",
|
|
|
|
]
|
2022-07-08 23:55:31 +00:00
|
|
|
|
|
|
|
|
2022-07-22 18:19:53 +00:00
|
|
|
@hass_callback
|
2022-07-22 23:12:08 +00:00
|
|
|
def async_get_scanner(hass: HomeAssistant) -> HaBleakScannerWrapper:
|
|
|
|
"""Return a HaBleakScannerWrapper.
|
|
|
|
|
|
|
|
This is a wrapper around our BleakScanner singleton that allows
|
|
|
|
multiple integrations to share the same BleakScanner.
|
|
|
|
"""
|
2022-08-17 20:51:56 +00:00
|
|
|
return HaBleakScannerWrapper()
|
2022-07-22 18:19:53 +00:00
|
|
|
|
|
|
|
|
2022-07-11 15:14:00 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_discovered_service_info(
|
|
|
|
hass: HomeAssistant,
|
2022-07-18 22:58:08 +00:00
|
|
|
) -> list[BluetoothServiceInfoBleak]:
|
2022-07-11 15:14:00 +00:00
|
|
|
"""Return the discovered devices list."""
|
2022-08-17 20:51:56 +00:00
|
|
|
if DATA_MANAGER not in hass.data:
|
2022-07-11 15:14:00 +00:00
|
|
|
return []
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-07-11 15:14:00 +00:00
|
|
|
return manager.async_discovered_service_info()
|
|
|
|
|
|
|
|
|
2022-07-18 22:58:08 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_ble_device_from_address(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
address: str,
|
|
|
|
) -> BLEDevice | None:
|
|
|
|
"""Return BLEDevice for an address if its present."""
|
2022-08-17 20:51:56 +00:00
|
|
|
if DATA_MANAGER not in hass.data:
|
2022-07-18 22:58:08 +00:00
|
|
|
return None
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-07-18 22:58:08 +00:00
|
|
|
return manager.async_ble_device_from_address(address)
|
|
|
|
|
|
|
|
|
2022-07-11 15:14:00 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_address_present(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
address: str,
|
|
|
|
) -> bool:
|
|
|
|
"""Check if an address is present in the bluetooth device list."""
|
2022-08-17 20:51:56 +00:00
|
|
|
if DATA_MANAGER not in hass.data:
|
2022-07-11 15:14:00 +00:00
|
|
|
return False
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-07-11 15:14:00 +00:00
|
|
|
return manager.async_address_present(address)
|
|
|
|
|
|
|
|
|
2022-07-08 23:55:31 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_register_callback(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
callback: BluetoothCallback,
|
2022-07-11 15:14:00 +00:00
|
|
|
match_dict: BluetoothCallbackMatcher | None,
|
2022-07-30 00:53:33 +00:00
|
|
|
mode: BluetoothScanningMode,
|
2022-07-08 23:55:31 +00:00
|
|
|
) -> Callable[[], None]:
|
|
|
|
"""Register to receive a callback on bluetooth change.
|
|
|
|
|
2022-07-30 00:53:33 +00:00
|
|
|
mode is currently not used as we only support active scanning.
|
|
|
|
Passive scanning will be available in the future. The flag
|
|
|
|
is required to be present to avoid a future breaking change
|
|
|
|
when we support passive scanning.
|
|
|
|
|
2022-07-08 23:55:31 +00:00
|
|
|
Returns a callback that can be used to cancel the registration.
|
|
|
|
"""
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-07-08 23:55:31 +00:00
|
|
|
return manager.async_register_callback(callback, match_dict)
|
|
|
|
|
|
|
|
|
2022-07-28 18:10:37 +00:00
|
|
|
async def async_process_advertisements(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
callback: ProcessAdvertisementCallback,
|
|
|
|
match_dict: BluetoothCallbackMatcher,
|
2022-07-30 00:53:33 +00:00
|
|
|
mode: BluetoothScanningMode,
|
2022-07-28 18:10:37 +00:00
|
|
|
timeout: int,
|
2022-07-30 00:53:33 +00:00
|
|
|
) -> BluetoothServiceInfoBleak:
|
2022-07-28 18:10:37 +00:00
|
|
|
"""Process advertisements until callback returns true or timeout expires."""
|
2022-07-30 00:53:33 +00:00
|
|
|
done: Future[BluetoothServiceInfoBleak] = Future()
|
2022-07-28 18:10:37 +00:00
|
|
|
|
|
|
|
@hass_callback
|
|
|
|
def _async_discovered_device(
|
2022-07-30 00:53:33 +00:00
|
|
|
service_info: BluetoothServiceInfoBleak, change: BluetoothChange
|
2022-07-28 18:10:37 +00:00
|
|
|
) -> None:
|
2022-08-04 15:58:15 +00:00
|
|
|
if not done.done() and callback(service_info):
|
2022-07-28 18:10:37 +00:00
|
|
|
done.set_result(service_info)
|
|
|
|
|
2022-07-30 00:53:33 +00:00
|
|
|
unload = async_register_callback(hass, _async_discovered_device, match_dict, mode)
|
2022-07-28 18:10:37 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
async with async_timeout.timeout(timeout):
|
|
|
|
return await done
|
|
|
|
finally:
|
|
|
|
unload()
|
|
|
|
|
|
|
|
|
2022-07-22 00:16:45 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_track_unavailable(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
callback: Callable[[str], None],
|
|
|
|
address: str,
|
|
|
|
) -> Callable[[], None]:
|
|
|
|
"""Register to receive a callback when an address is unavailable.
|
|
|
|
|
|
|
|
Returns a callback that can be used to cancel the registration.
|
|
|
|
"""
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-07-22 00:16:45 +00:00
|
|
|
return manager.async_track_unavailable(callback, address)
|
|
|
|
|
|
|
|
|
2022-08-05 12:49:34 +00:00
|
|
|
@hass_callback
|
|
|
|
def async_rediscover_address(hass: HomeAssistant, address: str) -> None:
|
|
|
|
"""Trigger discovery of devices which have already been seen."""
|
2022-08-17 20:51:56 +00:00
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
2022-08-05 12:49:34 +00:00
|
|
|
manager.async_rediscover_address(address)
|
|
|
|
|
|
|
|
|
2022-07-22 18:19:53 +00:00
|
|
|
async def _async_has_bluetooth_adapter() -> bool:
|
|
|
|
"""Return if the device has a bluetooth adapter."""
|
2022-07-25 14:52:35 +00:00
|
|
|
return bool(await async_get_bluetooth_adapters())
|
2022-07-22 18:19:53 +00:00
|
|
|
|
|
|
|
|
2022-07-08 23:55:31 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
|
|
"""Set up the bluetooth integration."""
|
2022-07-24 21:39:53 +00:00
|
|
|
integration_matcher = IntegrationMatcher(await async_get_bluetooth(hass))
|
|
|
|
manager = BluetoothManager(hass, integration_matcher)
|
2022-07-22 18:19:53 +00:00
|
|
|
manager.async_setup()
|
2022-08-17 20:51:56 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, manager.async_stop)
|
|
|
|
hass.data[DATA_MANAGER] = models.MANAGER = manager
|
2022-07-22 18:19:53 +00:00
|
|
|
# The config entry is responsible for starting the manager
|
|
|
|
# if its enabled
|
|
|
|
|
|
|
|
if hass.config_entries.async_entries(DOMAIN):
|
|
|
|
return True
|
|
|
|
if DOMAIN in config:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif await _async_has_bluetooth_adapter():
|
2022-08-12 13:25:23 +00:00
|
|
|
discovery_flow.async_create_flow(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
|
|
|
data={},
|
2022-07-22 18:19:53 +00:00
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: config_entries.ConfigEntry
|
|
|
|
) -> bool:
|
2022-08-17 20:51:56 +00:00
|
|
|
"""Set up a config entry for a bluetooth scanner."""
|
|
|
|
manager: BluetoothManager = hass.data[DATA_MANAGER]
|
|
|
|
adapter: str | None = entry.options.get(CONF_ADAPTER)
|
|
|
|
try:
|
|
|
|
bleak_scanner = create_bleak_scanner(BluetoothScanningMode.ACTIVE, adapter)
|
|
|
|
except RuntimeError as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
scanner = HaScanner(hass, bleak_scanner, adapter)
|
|
|
|
entry.async_on_unload(scanner.async_register_callback(manager.scanner_adv_received))
|
|
|
|
await scanner.async_start()
|
|
|
|
entry.async_on_unload(manager.async_register_scanner(scanner))
|
2022-07-25 14:52:35 +00:00
|
|
|
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
2022-08-17 20:51:56 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = scanner
|
2022-07-22 18:19:53 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-07-25 14:52:35 +00:00
|
|
|
async def _async_update_listener(
|
|
|
|
hass: HomeAssistant, entry: config_entries.ConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
|
2022-07-22 18:19:53 +00:00
|
|
|
async def async_unload_entry(
|
|
|
|
hass: HomeAssistant, entry: config_entries.ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Unload a config entry."""
|
2022-08-17 20:51:56 +00:00
|
|
|
scanner: HaScanner = hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
await scanner.async_stop()
|
2022-07-08 23:55:31 +00:00
|
|
|
return True
|