[bluetooth_proxy] Scanner-state sync in set_mode; platform-gate tests (#18100)

This commit is contained in:
Edvard Filistovič
2026-08-05 21:41:07 +00:00
committed by GitHub
parent 28aedc2b14
commit 1bd94bb805
3 changed files with 55 additions and 2 deletions
@@ -561,6 +561,10 @@ void BluetoothProxy::bluetooth_scanner_set_mode(bool active) {
} }
} }
if (this->api_connection_ != nullptr) { if (this->api_connection_ != nullptr) {
// Keep loop()'s change detector in step with the state sent here, so a
// failed restart (scan_running_ dropped by the tracker) is not reported
// twice — once now and again on the next tick.
this->last_scan_running_ = this->hub_->scan_running();
this->send_bluetooth_scanner_state_(); this->send_bluetooth_scanner_state_();
} }
} }
@@ -2,9 +2,10 @@
them without importing the esp32 BLE stack; pin the two declarations together. them without importing the esp32 BLE stack; pin the two declarations together.
The outer schema carries no defaults (the per-platform schema applies them), so The outer schema carries no defaults (the per-platform schema applies them), so
drift cannot surface in validation output — a key renamed or re-bounded in drift cannot surface in validation output — a key renamed or removed in
_esp32_config_schema() but not here would silently vanish from the dashboard's _esp32_config_schema() but not here would silently vanish from the dashboard's
field extractor. This test is what catches that. field extractor. This test is what catches that; validator bounds are pinned
separately only for connection_slots (test_idf_max_connections_mirror).
""" """
import voluptuous as vol import voluptuous as vol
@@ -0,0 +1,48 @@
"""The three platform-gate branches: BLE-less platforms are rejected with the
real reason, hub platforms reject GATT-only options by name, and the
advertisement-only arm applies its own defaults."""
import pytest
from esphome import config_validation as cv
from esphome.components import bluetooth_proxy
from esphome.const import CONF_ACTIVE, KEY_TARGET_PLATFORM
from esphome.core import CORE, KEY_CORE
def _set_platform(platform: str) -> None:
CORE.data.setdefault(KEY_CORE, {})[KEY_TARGET_PLATFORM] = platform
def test_ble_less_platform_gets_the_real_reason() -> None:
_set_platform("esp8266")
with pytest.raises(cv.Invalid, match="not supported on esp8266"):
bluetooth_proxy.CONFIG_SCHEMA({})
def test_ble_less_platform_connection_keys_fall_through() -> None:
# The key-level rejection must not fire here — it would imply an
# advertisement-only proxy exists on this platform.
_set_platform("esp8266")
with pytest.raises(cv.Invalid, match="not supported on esp8266"):
bluetooth_proxy.CONFIG_SCHEMA({"connection_slots": 2})
def test_hub_platform_rejects_active() -> None:
_set_platform("ln882x")
with pytest.raises(cv.Invalid, match="Active connections are not supported"):
bluetooth_proxy.CONFIG_SCHEMA({"active": True})
def test_hub_platform_rejects_connection_keys_by_name() -> None:
_set_platform("ln882x")
with pytest.raises(cv.Invalid, match="'connection_slots' requires active"):
bluetooth_proxy.CONFIG_SCHEMA({"connection_slots": 2})
with pytest.raises(cv.Invalid, match="'cache_services' requires active"):
bluetooth_proxy.CONFIG_SCHEMA({"cache_services": True})
def test_hub_platform_accepts_the_advertisement_only_shape() -> None:
_set_platform("ln882x")
validated = bluetooth_proxy.CONFIG_SCHEMA({})
assert validated[CONF_ACTIVE] is False