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
|
|
|
|
from bluetooth_data_tools import int_to_bluetooth_address, parse_advertisement_data
|
2022-08-23 14:41:50 +00:00
|
|
|
|
2022-11-13 20:18:36 +00:00
|
|
|
from homeassistant.components.bluetooth import BaseHaRemoteScanner
|
|
|
|
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."""
|
|
|
|
|
|
|
|
@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},
|
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."""
|
|
|
|
for adv in advertisements:
|
|
|
|
parsed = parse_advertisement_data((adv.data,))
|
|
|
|
self._async_on_advertisement(
|
|
|
|
int_to_bluetooth_address(adv.address),
|
|
|
|
adv.rssi,
|
|
|
|
parsed.local_name,
|
|
|
|
parsed.service_uuids,
|
|
|
|
parsed.service_data,
|
|
|
|
parsed.manufacturer_data,
|
|
|
|
None,
|
|
|
|
{"address_type": adv.address_type},
|
|
|
|
)
|