[ble_device_base] Keep MAC filter lists in shared flash tables (#20088)

This commit is contained in:
J. Nick Koston
2026-10-05 19:28:36 -05:00
committed by GitHub
parent 3a3707926d
commit 8d8bd73112
9 changed files with 128 additions and 30 deletions
@@ -22,7 +22,11 @@ def test_trigger_codegen(
main_cpp = generate_main(component_config_path("test_automations.yaml"))
# on_ble_advertise: multi-mac filter (two addresses in one initializer list)
assert "set_addresses({0xAC3743775F4CULL, 0x112233445566ULL})" in main_cpp
assert (
"static constexpr uint64_t ble_mac_filter[] PROGMEM = "
"{0xAC3743775F4CULL, 0x112233445566ULL, 0};" in main_cpp
)
assert "set_addresses(ble_mac_filter)" in main_cpp
# 128-bit service uuid goes out reversed (BLE wire order); single-mac filter
assert (
"set_service_uuid128((uint8_t*)(const uint8_t[16]){0xCD,0xAB,0xCD,0xAB,"
@@ -0,0 +1,27 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
logger:
esp32_ble_tracker:
on_ble_advertise:
- mac_address:
- AC:37:43:77:5F:4C
- 11:22:33:44:55:66
then:
- logger.log: one
- mac_address:
- AC:37:43:77:5F:4C
- 11:22:33:44:55:66
then:
- logger.log: two
- then:
- logger.log: any
- mac_address: []
then:
- logger.log: empty
@@ -0,0 +1,38 @@
"""Tests for the esp32_ble_tracker on_ble_advertise MAC filter codegen."""
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome import config_validation as cv
from esphome.components.ble_device_base.automation import MAC_FILTER_LIST
def test_mac_filters_share_one_progmem_table(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Identical MAC lists share one flash table ended by 0; no or an empty filter sets none."""
main_cpp = generate_main(component_config_path("mac_filter.yaml"))
assert (
"static constexpr uint64_t ble_mac_filter[] PROGMEM = "
"{0xAC3743775F4CULL, 0x112233445566ULL, 0};" in main_cpp
)
assert main_cpp.count("set_addresses(ble_mac_filter);") == 2
assert main_cpp.count("set_addresses(") == 2
def test_zero_mac_is_rejected() -> None:
"""00:00:00:00:00:00 ends the flash table, so it cannot be a filter entry."""
assert MAC_FILTER_LIST(["11:22:33:44:55:66"])[0].parts == (
0x11,
0x22,
0x33,
0x44,
0x55,
0x66,
)
with pytest.raises(cv.Invalid):
MAC_FILTER_LIST(["00:00:00:00:00:00"])
@@ -17,7 +17,11 @@ def test_trigger_codegen(
main_cpp = generate_main(component_config_path("test_automations.yaml"))
# on_ble_advertise: multi-mac filter (two addresses in one initializer list)
assert "set_addresses({0xAC3743775F4CULL, 0x112233445566ULL})" in main_cpp
assert (
"static constexpr uint64_t ble_mac_filter[] PROGMEM = "
"{0xAC3743775F4CULL, 0x112233445566ULL, 0};" in main_cpp
)
assert "set_addresses(ble_mac_filter)" in main_cpp
# 128-bit service uuid goes out reversed (BLE wire order); single-mac filter
assert (
"set_service_uuid128((uint8_t*)(const uint8_t[16]){0xCD,0xAB,0xCD,0xAB,"
@@ -19,8 +19,12 @@ def test_trigger_codegen(
) -> None:
main_cpp = generate_main(component_config_path("test_automations.yaml"))
# on_ble_advertise: multi-mac filter (two addresses in one initializer list)
assert "set_addresses({0xAC3743775F4CULL, 0x112233445566ULL})" in main_cpp
# on_ble_advertise: multi-mac filter, a shared flash table ended by 0
assert (
"static constexpr uint64_t ble_mac_filter[] PROGMEM = "
"{0xAC3743775F4CULL, 0x112233445566ULL, 0};" in main_cpp
)
assert "set_addresses(ble_mac_filter)" in main_cpp
# 128-bit service uuid goes out reversed (BLE wire order); single-mac filter
assert (
"set_service_uuid128((uint8_t*)(const uint8_t[16]){0xCD,0xAB,0xCD,0xAB,"