1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-20 08:46:01 +00:00

[bthome] Optimize loop efficiency with disable_loop/enable_loop

Use disable_loop() and enable_loop() to avoid running loop() when there's
no immediate advertising work to do. The loop is disabled by default and
only enabled when a sensor with advertise_immediately triggers, then
disabled again after processing.
This commit is contained in:
Claude
2025-11-17 23:08:29 +00:00
parent 116f66953d
commit 55407844d8

View File

@@ -96,6 +96,9 @@ void BTHome::setup() {
} }
}); });
} }
// Start with loop disabled - only enable when immediate advertising is needed
this->disable_loop();
} }
void BTHome::loop() { void BTHome::loop() {
@@ -107,6 +110,8 @@ void BTHome::loop() {
this->build_advertisement_packets_(); this->build_advertisement_packets_();
this->current_packet_index_ = 0; this->current_packet_index_ = 0;
this->on_advertise_(); this->on_advertise_();
// Disable loop again until next immediate advertising request
this->disable_loop();
} }
} }
@@ -128,6 +133,8 @@ void BTHome::trigger_immediate_advertising_(uint8_t measurement_index, bool is_b
this->immediate_advertising_pending_ = true; this->immediate_advertising_pending_ = true;
this->immediate_adv_measurement_index_ = measurement_index; this->immediate_adv_measurement_index_ = measurement_index;
this->immediate_adv_is_binary_ = is_binary; this->immediate_adv_is_binary_ = is_binary;
// Enable loop to process immediate advertising
this->enable_loop();
} }
void BTHome::build_advertisement_packets_() { void BTHome::build_advertisement_packets_() {