2022-08-23 14:41:50 +00:00
|
|
|
"""Bluetooth scanner for esphome."""
|
2022-09-26 13:12:08 +00:00
|
|
|
from __future__ import annotations
|
2022-08-23 14:41:50 +00:00
|
|
|
|
2023-06-07 23:36:22 +00:00
|
|
|
from aioesphomeapi import BluetoothLEAdvertisement, BluetoothLERawAdvertisement
|
2023-08-11 01:59:37 +00:00
|
|
|
from bluetooth_data_tools import (
|
|
|
|
int_to_bluetooth_address,
|
|
|
|
parse_advertisement_data_tuple,
|
|
|
|
)
|
2022-08-23 14:41:50 +00:00
|
|
|
|
2023-06-15 01:47:00 +00:00
|
|
|
from homeassistant.components.bluetooth import MONOTONIC_TIME, BaseHaRemoteScanner
|
2022-11-13 20:18:36 +00:00
|
|
|
from homeassistant.core import callback
|
2022-08-23 14:41:50 +00:00
|
|
|
|
2022-11-01 17:07:42 +00:00
|
|
|
|
2022-11-13 20:18:36 +00:00
|
|
|
class ESPHomeScanner(BaseHaRemoteScanner):
|
2022-08-23 14:41:50 +00:00
|
|
|
"""Scanner for esphome."""
|
|
|
|
|
2023-08-11 01:59:37 +00:00
|
|
|
__slots__ = ()
|
|
|
|
|
2022-08-23 14:41:50 +00:00
|
|
|
@callback
|
|
|
|
def async_on_advertisement(self, adv: BluetoothLEAdvertisement) -> None:
|
|
|
|
"""Call the registered callback."""
|
2022-12-05 02:09:45 +00:00
|
|
|
# The mac address is a uint64, but we need a string
|
2022-11-13 20:18:36 +00:00
|
|
|
self._async_on_advertisement(
|
2023-04-16 21:55:08 +00:00
|
|
|
int_to_bluetooth_address(adv.address),
|
2022-11-13 20:18:36 +00:00
|
|
|
adv.rssi,
|
|
|
|
adv.name,
|
|
|
|
adv.service_uuids,
|
|
|
|
adv.service_data,
|
|
|
|
adv.manufacturer_data,
|
|
|
|
None,
|
2022-11-29 04:54:53 +00:00
|
|
|
{"address_type": adv.address_type},
|
2023-06-15 01:47:00 +00:00
|
|
|
MONOTONIC_TIME(),
|
2022-08-23 14:41:50 +00:00
|
|
|
)
|
2023-06-07 23:36:22 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_on_raw_advertisements(
|
|
|
|
self, advertisements: list[BluetoothLERawAdvertisement]
|
|
|
|
) -> None:
|
|
|
|
"""Call the registered callback."""
|
2023-06-15 01:47:00 +00:00
|
|
|
now = MONOTONIC_TIME()
|
2023-06-07 23:36:22 +00:00
|
|
|
for adv in advertisements:
|
|
|
|
self._async_on_advertisement(
|
|
|
|
int_to_bluetooth_address(adv.address),
|
|
|
|
adv.rssi,
|
2023-08-11 01:59:37 +00:00
|
|
|
*parse_advertisement_data_tuple((adv.data,)),
|
2023-06-07 23:36:22 +00:00
|
|
|
{"address_type": adv.address_type},
|
2023-06-15 01:47:00 +00:00
|
|
|
now,
|
2023-06-07 23:36:22 +00:00
|
|
|
)
|