Avoid regex overhead in processing esphome bluetooth advertisements (#83246)
parent
1efc71624a
commit
3044e78d43
|
@ -1,7 +1,6 @@
|
||||||
"""Bluetooth scanner for esphome."""
|
"""Bluetooth scanner for esphome."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aioesphomeapi import BluetoothLEAdvertisement
|
from aioesphomeapi import BluetoothLEAdvertisement
|
||||||
|
@ -9,8 +8,6 @@ from aioesphomeapi import BluetoothLEAdvertisement
|
||||||
from homeassistant.components.bluetooth import BaseHaRemoteScanner
|
from homeassistant.components.bluetooth import BaseHaRemoteScanner
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
TWO_CHAR = re.compile("..")
|
|
||||||
|
|
||||||
|
|
||||||
class ESPHomeScanner(BaseHaRemoteScanner):
|
class ESPHomeScanner(BaseHaRemoteScanner):
|
||||||
"""Scanner for esphome."""
|
"""Scanner for esphome."""
|
||||||
|
@ -18,9 +15,10 @@ class ESPHomeScanner(BaseHaRemoteScanner):
|
||||||
@callback
|
@callback
|
||||||
def async_on_advertisement(self, adv: BluetoothLEAdvertisement) -> None:
|
def async_on_advertisement(self, adv: BluetoothLEAdvertisement) -> None:
|
||||||
"""Call the registered callback."""
|
"""Call the registered callback."""
|
||||||
address = ":".join(TWO_CHAR.findall("%012X" % adv.address)) # must be upper
|
# The mac address is a uint64, but we need a string
|
||||||
|
mac_hex = f"{adv.address:012X}"
|
||||||
self._async_on_advertisement(
|
self._async_on_advertisement(
|
||||||
address,
|
f"{mac_hex[0:2]}:{mac_hex[2:4]}:{mac_hex[4:6]}:{mac_hex[6:8]}:{mac_hex[8:10]}:{mac_hex[10:12]}",
|
||||||
adv.rssi,
|
adv.rssi,
|
||||||
adv.name,
|
adv.name,
|
||||||
adv.service_uuids,
|
adv.service_uuids,
|
||||||
|
|
Loading…
Reference in New Issue