| 1 |
"""Bluetooth scanner for shelly.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import logging |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from bluetooth_data_tools import monotonic_time_coarse |
| 9 |
from habluetooth import BaseHaRemoteScanner |
| 10 |
|
| 11 |
from ..const import BLE_SCAN_RESULT_EVENT |
| 12 |
from ..parser import parse_ble_scan_result_event |
| 13 |
|
| 14 |
LOGGER = logging.getLogger(__name__) |
| 15 |
|
| 16 |
|
| 17 |
class ShellyBLEScanner(BaseHaRemoteScanner): |
| 18 |
"""Scanner for shelly.""" |
| 19 |
|
| 20 |
def async_on_event(self, event: dict[str, Any]) -> None: |
| 21 |
"""Process an event from the shelly and ignore if its not a ble.scan_result.""" |
| 22 |
if event.get("event") != BLE_SCAN_RESULT_EVENT: |
| 23 |
return |
| 24 |
|
| 25 |
try: |
| 26 |
parsed_advs = parse_ble_scan_result_event(event["data"]) |
| 27 |
except Exception as err: |
| 28 |
# Broad exception catch because we have no |
| 29 |
# control over the data that is coming in. |
| 30 |
LOGGER.error("Failed to parse BLE event: %s", err, exc_info=True) |
| 31 |
return |
| 32 |
|
| 33 |
now = monotonic_time_coarse() |
| 34 |
for address, rssi, raw in parsed_advs: |
| 35 |
self._async_on_raw_advertisement( |
| 36 |
address, |
| 37 |
rssi, |
| 38 |
raw, |
| 39 |
{}, |
| 40 |
now, |
| 41 |
) |