1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-17 18:52:19 +01:00

Merge branch 'esp32_ble_tracker_cleanup_code' into integration

This commit is contained in:
J. Nick Koston
2025-08-04 15:25:37 -10:00
7 changed files with 250 additions and 99 deletions

View File

@@ -76,58 +76,17 @@ void ESP32BLETracker::loop() {
this->start_scan(); this->start_scan();
} }
} }
int connecting = 0; ClientStateCounts counts = this->count_client_states_();
int discovered = 0; if (counts != this->client_state_counts_) {
int searching = 0; this->client_state_counts_ = counts;
int disconnecting = 0; ESP_LOGD(TAG, "connecting: %d, discovered: %d, searching: %d, disconnecting: %d",
for (auto *client : this->clients_) { this->client_state_counts_.connecting, this->client_state_counts_.discovered,
switch (client->state()) { this->client_state_counts_.searching, this->client_state_counts_.disconnecting);
case ClientState::DISCONNECTING:
disconnecting++;
break;
case ClientState::DISCOVERED:
discovered++;
break;
case ClientState::SEARCHING:
searching++;
break;
case ClientState::CONNECTING:
case ClientState::READY_TO_CONNECT:
connecting++;
break;
default:
break;
}
} }
if (connecting != connecting_ || discovered != discovered_ || searching != searching_ ||
disconnecting != disconnecting_) {
connecting_ = connecting;
discovered_ = discovered;
searching_ = searching;
disconnecting_ = disconnecting;
ESP_LOGD(TAG, "connecting: %d, discovered: %d, searching: %d, disconnecting: %d", connecting_, discovered_,
searching_, disconnecting_);
}
bool promote_to_connecting = discovered && !searching && !connecting;
// All scan result processing is now done immediately in gap_scan_event_handler
// No ring buffer processing needed here
if (this->scanner_state_ == ScannerState::FAILED || if (this->scanner_state_ == ScannerState::FAILED ||
(this->scan_set_param_failed_ && this->scanner_state_ == ScannerState::RUNNING)) { (this->scan_set_param_failed_ && this->scanner_state_ == ScannerState::RUNNING)) {
this->stop_scan_(); this->handle_scanner_failure_();
if (this->scan_start_fail_count_ == std::numeric_limits<uint8_t>::max()) {
ESP_LOGE(TAG, "Scan could not restart after %d attempts, rebooting to restore stack (IDF)",
std::numeric_limits<uint8_t>::max());
App.reboot();
}
if (this->scan_start_failed_) {
ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_);
this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
}
if (this->scan_set_param_failed_) {
ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_);
this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS;
}
} }
/* /*
@@ -142,13 +101,12 @@ void ESP32BLETracker::loop() {
https://github.com/espressif/esp-idf/issues/6688 https://github.com/espressif/esp-idf/issues/6688
*/ */
if (this->scanner_state_ == ScannerState::IDLE && !connecting && !disconnecting && !promote_to_connecting) { bool promote_to_connecting = counts.discovered && !counts.searching && !counts.connecting;
if (this->scanner_state_ == ScannerState::IDLE && !counts.connecting && !counts.disconnecting &&
!promote_to_connecting) {
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
if (this->coex_prefer_ble_) { this->update_coex_preference_(false);
this->coex_prefer_ble_ = false;
ESP_LOGD(TAG, "Setting coexistence preference to balanced.");
esp_coex_preference_set(ESP_COEX_PREFER_BALANCE); // Reset to default
}
#endif #endif
if (this->scan_continuous_) { if (this->scan_continuous_) {
this->start_scan_(false); // first = false this->start_scan_(false); // first = false
@@ -157,34 +115,12 @@ void ESP32BLETracker::loop() {
// If there is a discovered client and no connecting // If there is a discovered client and no connecting
// clients and no clients using the scanner to search for // clients and no clients using the scanner to search for
// devices, then promote the discovered client to ready to connect. // devices, then promote the discovered client to ready to connect.
// Note: Scanning is already stopped by gap_scan_event_handler when // We check both RUNNING and IDLE states because:
// a discovered client is found, so we only need to handle promotion // - RUNNING: gap_scan_event_handler initiates stop_scan_() but promotion can happen immediately
// when the scanner is IDLE. // - IDLE: Scanner has already stopped (naturally or by gap_scan_event_handler)
if (promote_to_connecting && if (promote_to_connecting &&
(this->scanner_state_ == ScannerState::RUNNING || this->scanner_state_ == ScannerState::IDLE)) { (this->scanner_state_ == ScannerState::RUNNING || this->scanner_state_ == ScannerState::IDLE)) {
for (auto *client : this->clients_) { this->try_promote_discovered_clients_();
if (client->state() == ClientState::DISCOVERED) {
if (this->scanner_state_ == ScannerState::RUNNING) {
ESP_LOGD(TAG, "Stopping scan to make connection");
this->stop_scan_();
// Don't wait for scan stop complete - promote immediately.
// This is safe because ESP-IDF processes BLE commands sequentially through its internal mailbox queue.
// This guarantees that the stop scan command will be fully processed before any subsequent connect command,
// preventing race conditions or overlapping operations.
}
ESP_LOGD(TAG, "Promoting client to connect");
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
if (!this->coex_prefer_ble_) {
this->coex_prefer_ble_ = true;
esp_coex_preference_set(ESP_COEX_PREFER_BT); // Prioritize Bluetooth
}
#endif
client->set_state(ClientState::READY_TO_CONNECT);
break;
}
}
} }
} }
@@ -699,8 +635,9 @@ void ESP32BLETracker::dump_config() {
ESP_LOGCONFIG(TAG, " Scanner State: FAILED"); ESP_LOGCONFIG(TAG, " Scanner State: FAILED");
break; break;
} }
ESP_LOGCONFIG(TAG, " Connecting: %d, discovered: %d, searching: %d, disconnecting: %d", connecting_, discovered_, ESP_LOGCONFIG(TAG, " Connecting: %d, discovered: %d, searching: %d, disconnecting: %d",
searching_, disconnecting_); this->client_state_counts_.connecting, this->client_state_counts_.discovered,
this->client_state_counts_.searching, this->client_state_counts_.disconnecting);
if (this->scan_start_fail_count_) { if (this->scan_start_fail_count_) {
ESP_LOGCONFIG(TAG, " Scan Start Fail Count: %d", this->scan_start_fail_count_); ESP_LOGCONFIG(TAG, " Scan Start Fail Count: %d", this->scan_start_fail_count_);
} }
@@ -848,6 +785,61 @@ void ESP32BLETracker::cleanup_scan_state_(bool is_stop_complete) {
this->set_scanner_state_(ScannerState::IDLE); this->set_scanner_state_(ScannerState::IDLE);
} }
void ESP32BLETracker::handle_scanner_failure_() {
this->stop_scan_();
if (this->scan_start_fail_count_ == std::numeric_limits<uint8_t>::max()) {
ESP_LOGE(TAG, "Scan could not restart after %d attempts, rebooting to restore stack (IDF)",
std::numeric_limits<uint8_t>::max());
App.reboot();
}
if (this->scan_start_failed_) {
ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_);
this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
}
if (this->scan_set_param_failed_) {
ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_);
this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS;
}
}
void ESP32BLETracker::try_promote_discovered_clients_() {
for (auto *client : this->clients_) {
if (client->state() != ClientState::DISCOVERED) {
continue;
}
if (this->scanner_state_ == ScannerState::RUNNING) {
ESP_LOGD(TAG, "Stopping scan to make connection");
this->stop_scan_();
// Don't wait for scan stop complete - promote immediately.
// This is safe because ESP-IDF processes BLE commands sequentially through its internal mailbox queue.
// This guarantees that the stop scan command will be fully processed before any subsequent connect command,
// preventing race conditions or overlapping operations.
}
ESP_LOGD(TAG, "Promoting client to connect");
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
this->update_coex_preference_(true);
#endif
client->set_state(ClientState::READY_TO_CONNECT);
break;
}
}
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
void ESP32BLETracker::update_coex_preference_(bool force_ble) {
if (force_ble && !this->coex_prefer_ble_) {
ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
this->coex_prefer_ble_ = true;
esp_coex_preference_set(ESP_COEX_PREFER_BT); // Prioritize Bluetooth
} else if (!force_ble && this->coex_prefer_ble_) {
ESP_LOGD(TAG, "Setting coexistence preference to balanced.");
this->coex_prefer_ble_ = false;
esp_coex_preference_set(ESP_COEX_PREFER_BALANCE); // Reset to default
}
}
#endif
} // namespace esphome::esp32_ble_tracker } // namespace esphome::esp32_ble_tracker
#endif // USE_ESP32 #endif // USE_ESP32

View File

@@ -136,6 +136,18 @@ class ESPBTDeviceListener {
ESP32BLETracker *parent_{nullptr}; ESP32BLETracker *parent_{nullptr};
}; };
struct ClientStateCounts {
uint8_t connecting = 0;
uint8_t discovered = 0;
uint8_t searching = 0;
uint8_t disconnecting = 0;
bool operator!=(const ClientStateCounts &other) const {
return connecting != other.connecting || discovered != other.discovered || searching != other.searching ||
disconnecting != other.disconnecting;
}
};
enum class ClientState : uint8_t { enum class ClientState : uint8_t {
// Connection is allocated // Connection is allocated
INIT, INIT,
@@ -279,6 +291,38 @@ class ESP32BLETracker : public Component,
/// Check if any clients are in connecting or ready to connect state /// Check if any clients are in connecting or ready to connect state
bool has_connecting_clients_() const; bool has_connecting_clients_() const;
#endif #endif
/// Handle scanner failure states
void handle_scanner_failure_();
/// Try to promote discovered clients to ready to connect
void try_promote_discovered_clients_();
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
/// Update BLE coexistence preference
void update_coex_preference_(bool force_ble);
#endif
/// Count clients in each state
ClientStateCounts count_client_states_() const {
ClientStateCounts counts;
for (auto *client : this->clients_) {
switch (client->state()) {
case ClientState::DISCONNECTING:
counts.disconnecting++;
break;
case ClientState::DISCOVERED:
counts.discovered++;
break;
case ClientState::SEARCHING:
counts.searching++;
break;
case ClientState::CONNECTING:
case ClientState::READY_TO_CONNECT:
counts.connecting++;
break;
default:
break;
}
}
return counts;
}
uint8_t app_id_{0}; uint8_t app_id_{0};
@@ -304,10 +348,7 @@ class ESP32BLETracker : public Component,
esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS}; esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS}; esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
int connecting_{0}; ClientStateCounts client_state_counts_;
int discovered_{0};
int searching_{0};
int disconnecting_{0};
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
bool coex_prefer_ble_{false}; bool coex_prefer_ble_{false};
#endif #endif

View File

@@ -77,6 +77,7 @@ BRIGHTNESS = 0x51
WRDISBV = 0x51 WRDISBV = 0x51
RDDISBV = 0x52 RDDISBV = 0x52
WRCTRLD = 0x53 WRCTRLD = 0x53
WCE = 0x58
SWIRE1 = 0x5A SWIRE1 = 0x5A
SWIRE2 = 0x5B SWIRE2 = 0x5B
IFMODE = 0xB0 IFMODE = 0xB0
@@ -91,6 +92,7 @@ PWCTR2 = 0xC1
PWCTR3 = 0xC2 PWCTR3 = 0xC2
PWCTR4 = 0xC3 PWCTR4 = 0xC3
PWCTR5 = 0xC4 PWCTR5 = 0xC4
SPIMODESEL = 0xC4
VMCTR1 = 0xC5 VMCTR1 = 0xC5
IFCTR = 0xC6 IFCTR = 0xC6
VMCTR2 = 0xC7 VMCTR2 = 0xC7

View File

@@ -5,10 +5,13 @@ from esphome.components.mipi import (
PAGESEL, PAGESEL,
PIXFMT, PIXFMT,
SLPOUT, SLPOUT,
SPIMODESEL,
SWIRE1, SWIRE1,
SWIRE2, SWIRE2,
TEON, TEON,
WCE,
WRAM, WRAM,
WRCTRLD,
DriverChip, DriverChip,
delay, delay,
) )
@@ -87,4 +90,19 @@ T4_S3_AMOLED = RM690B0.extend(
bus_mode=TYPE_QUAD, bus_mode=TYPE_QUAD,
) )
CO5300 = DriverChip(
"CO5300",
brightness=0xD0,
color_order=MODE_RGB,
bus_mode=TYPE_QUAD,
initsequence=(
(SLPOUT,), # Requires early SLPOUT
(PAGESEL, 0x00),
(SPIMODESEL, 0x80),
(WRCTRLD, 0x20),
(WCE, 0x00),
),
)
models = {} models = {}

View File

@@ -1,6 +1,7 @@
from esphome.components.mipi import DriverChip from esphome.components.mipi import DriverChip
import esphome.config_validation as cv import esphome.config_validation as cv
from .amoled import CO5300
from .ili import ILI9488_A from .ili import ILI9488_A
DriverChip( DriverChip(
@@ -140,3 +141,14 @@ ILI9488_A.extend(
data_rate="20MHz", data_rate="20MHz",
invert_colors=True, invert_colors=True,
) )
CO5300.extend(
"WAVESHARE-ESP32-S3-TOUCH-AMOLED-1.75",
width=466,
height=466,
pixel_mode="16bit",
offset_height=0,
offset_width=6,
cs_pin=12,
reset_pin=39,
)

View File

@@ -373,3 +373,20 @@ button:
name: "Test Button" name: "Test Button"
on_press: on_press:
- logger.log: "Button pressed" - logger.log: "Button pressed"
# Date, Time, and DateTime entities
datetime:
- platform: template
type: date
name: "Test Date"
initial_value: "2023-05-13"
optimistic: true
- platform: template
type: time
name: "Test Time"
initial_value: "12:30:00"
optimistic: true
- platform: template
type: datetime
name: "Test DateTime"
optimistic: true

View File

@@ -4,7 +4,17 @@ from __future__ import annotations
import asyncio import asyncio
from aioesphomeapi import ClimateInfo, EntityState, SensorState from aioesphomeapi import (
ClimateInfo,
DateInfo,
DateState,
DateTimeInfo,
DateTimeState,
EntityState,
SensorState,
TimeInfo,
TimeState,
)
import pytest import pytest
from .types import APIClientConnectedFactory, RunCompiledFunction from .types import APIClientConnectedFactory, RunCompiledFunction
@@ -22,34 +32,56 @@ async def test_host_mode_many_entities(
async with run_compiled(yaml_config), api_client_connected() as client: async with run_compiled(yaml_config), api_client_connected() as client:
# Subscribe to state changes # Subscribe to state changes
states: dict[int, EntityState] = {} states: dict[int, EntityState] = {}
sensor_count_future: asyncio.Future[int] = loop.create_future() minimum_states_future: asyncio.Future[None] = loop.create_future()
def on_state(state: EntityState) -> None: def on_state(state: EntityState) -> None:
states[state.key] = state states[state.key] = state
# Count sensor states specifically # Check if we have received minimum expected states
sensor_states = [ sensor_states = [
s s
for s in states.values() for s in states.values()
if isinstance(s, SensorState) and isinstance(s.state, float) if isinstance(s, SensorState) and isinstance(s.state, float)
] ]
# When we have received states from at least 50 sensors, resolve the future date_states = [s for s in states.values() if isinstance(s, DateState)]
if len(sensor_states) >= 50 and not sensor_count_future.done(): time_states = [s for s in states.values() if isinstance(s, TimeState)]
sensor_count_future.set_result(len(sensor_states)) datetime_states = [
s for s in states.values() if isinstance(s, DateTimeState)
]
# We expect at least 50 sensors and 1 of each datetime entity type
if (
len(sensor_states) >= 50
and len(date_states) >= 1
and len(time_states) >= 1
and len(datetime_states) >= 1
and not minimum_states_future.done()
):
minimum_states_future.set_result(None)
client.subscribe_states(on_state) client.subscribe_states(on_state)
# Wait for states from at least 50 sensors with timeout # Wait for minimum states with timeout
try: try:
sensor_count = await asyncio.wait_for(sensor_count_future, timeout=10.0) await asyncio.wait_for(minimum_states_future, timeout=10.0)
except TimeoutError: except TimeoutError:
sensor_states = [ sensor_states = [
s s
for s in states.values() for s in states.values()
if isinstance(s, SensorState) and isinstance(s.state, float) if isinstance(s, SensorState) and isinstance(s.state, float)
] ]
date_states = [s for s in states.values() if isinstance(s, DateState)]
time_states = [s for s in states.values() if isinstance(s, TimeState)]
datetime_states = [
s for s in states.values() if isinstance(s, DateTimeState)
]
pytest.fail( pytest.fail(
f"Did not receive states from at least 50 sensors within 10 seconds. " f"Did not receive expected states within 10 seconds. "
f"Received {len(sensor_states)} sensor states out of {len(states)} total states" f"Received: {len(sensor_states)} sensor states (expected >=50), "
f"{len(date_states)} date states (expected >=1), "
f"{len(time_states)} time states (expected >=1), "
f"{len(datetime_states)} datetime states (expected >=1). "
f"Total states: {len(states)}"
) )
# Verify we received a good number of entity states # Verify we received a good number of entity states
@@ -64,13 +96,25 @@ async def test_host_mode_many_entities(
if isinstance(s, SensorState) and isinstance(s.state, float) if isinstance(s, SensorState) and isinstance(s.state, float)
] ]
assert sensor_count >= 50, (
f"Expected at least 50 sensor states, got {sensor_count}"
)
assert len(sensor_states) >= 50, ( assert len(sensor_states) >= 50, (
f"Expected at least 50 sensor states, got {len(sensor_states)}" f"Expected at least 50 sensor states, got {len(sensor_states)}"
) )
# Verify we received datetime entity states
date_states = [s for s in states.values() if isinstance(s, DateState)]
time_states = [s for s in states.values() if isinstance(s, TimeState)]
datetime_states = [s for s in states.values() if isinstance(s, DateTimeState)]
assert len(date_states) >= 1, (
f"Expected at least 1 date state, got {len(date_states)}"
)
assert len(time_states) >= 1, (
f"Expected at least 1 time state, got {len(time_states)}"
)
assert len(datetime_states) >= 1, (
f"Expected at least 1 datetime state, got {len(datetime_states)}"
)
# Get entity info to verify climate entity details # Get entity info to verify climate entity details
entities = await client.list_entities_services() entities = await client.list_entities_services()
climate_infos = [e for e in entities[0] if isinstance(e, ClimateInfo)] climate_infos = [e for e in entities[0] if isinstance(e, ClimateInfo)]
@@ -89,3 +133,28 @@ async def test_host_mode_many_entities(
assert "HOME" in preset_names, f"Expected 'HOME' preset, got {preset_names}" assert "HOME" in preset_names, f"Expected 'HOME' preset, got {preset_names}"
assert "AWAY" in preset_names, f"Expected 'AWAY' preset, got {preset_names}" assert "AWAY" in preset_names, f"Expected 'AWAY' preset, got {preset_names}"
assert "SLEEP" in preset_names, f"Expected 'SLEEP' preset, got {preset_names}" assert "SLEEP" in preset_names, f"Expected 'SLEEP' preset, got {preset_names}"
# Verify datetime entities exist
date_infos = [e for e in entities[0] if isinstance(e, DateInfo)]
time_infos = [e for e in entities[0] if isinstance(e, TimeInfo)]
datetime_infos = [e for e in entities[0] if isinstance(e, DateTimeInfo)]
assert len(date_infos) >= 1, "Expected at least 1 date entity"
assert len(time_infos) >= 1, "Expected at least 1 time entity"
assert len(datetime_infos) >= 1, "Expected at least 1 datetime entity"
# Verify the entity names
date_info = date_infos[0]
assert date_info.name == "Test Date", (
f"Expected date entity name 'Test Date', got {date_info.name}"
)
time_info = time_infos[0]
assert time_info.name == "Test Time", (
f"Expected time entity name 'Test Time', got {time_info.name}"
)
datetime_info = datetime_infos[0]
assert datetime_info.name == "Test DateTime", (
f"Expected datetime entity name 'Test DateTime', got {datetime_info.name}"
)