From 15d0b4355ebf88699d423b95e0226100854f02b3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 May 2025 15:48:57 -0400 Subject: [PATCH] Reduce number of calls to fetch time in the main loop (#8804) --- esphome/components/api/api_connection.cpp | 5 +-- esphome/components/bedjet/bedjet_hub.cpp | 1 + .../bluetooth_proxy/bluetooth_proxy.cpp | 3 +- esphome/components/cse7766/cse7766.cpp | 3 +- .../current_based/current_based_cover.cpp | 3 +- esphome/components/daly_bms/daly_bms.cpp | 3 +- esphome/components/debug/debug_component.cpp | 2 +- esphome/components/endstop/endstop_cover.cpp | 3 +- .../components/esp32_ble/ble_advertising.cpp | 3 +- .../components/esp32_camera/esp32_camera.cpp | 3 +- .../esp32_improv/esp32_improv_component.cpp | 2 +- .../components/esp32_touch/esp32_touch.cpp | 2 +- .../ethernet/ethernet_component.cpp | 2 +- .../components/feedback/feedback_cover.cpp | 3 +- esphome/components/gcja5/gcja5.cpp | 3 +- .../growatt_solar/growatt_solar.cpp | 3 +- esphome/components/kuntze/kuntze.cpp | 3 +- .../matrix_keypad/matrix_keypad.cpp | 3 +- .../components/max7219digit/max7219digit.cpp | 3 +- esphome/components/modbus/modbus.cpp | 3 +- esphome/components/mqtt/mqtt_client.cpp | 2 +- esphome/components/pmsx003/pmsx003.cpp | 3 +- esphome/components/pzem004t/pzem004t.cpp | 3 +- esphome/components/rf_bridge/rf_bridge.cpp | 3 +- esphome/components/sds011/sds011.cpp | 3 +- .../components/slow_pwm/slow_pwm_output.cpp | 3 +- esphome/components/sprinkler/sprinkler.cpp | 11 ++++--- .../time_based/time_based_cover.cpp | 3 +- .../components/uart/switch/uart_switch.cpp | 3 +- .../climate/uponor_smatrix_climate.cpp | 3 +- .../uponor_smatrix/uponor_smatrix.cpp | 3 +- esphome/core/application.cpp | 32 +++++++++++++------ esphome/core/application.h | 6 +++- esphome/core/component.cpp | 15 ++++++--- esphome/core/component.h | 6 +++- esphome/core/scheduler.cpp | 5 ++- 36 files changed, 107 insertions(+), 53 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index ee0451f499..847d7840dc 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -8,6 +8,7 @@ #include "esphome/core/hal.h" #include "esphome/core/log.h" #include "esphome/core/version.h" +#include "esphome/core/application.h" #ifdef USE_DEEP_SLEEP #include "esphome/components/deep_sleep/deep_sleep_component.h" @@ -146,7 +147,7 @@ void APIConnection::loop() { } return; } else { - this->last_traffic_ = millis(); + this->last_traffic_ = App.get_loop_component_start_time(); // read a packet this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]); if (this->remove_) @@ -163,7 +164,7 @@ void APIConnection::loop() { static uint32_t keepalive = 60000; static uint8_t max_ping_retries = 60; static uint16_t ping_retry_interval = 1000; - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (this->sent_ping_) { // Disconnect if not responded within 2.5*keepalive if (now - this->last_traffic_ > (keepalive * 5) / 2) { diff --git a/esphome/components/bedjet/bedjet_hub.cpp b/esphome/components/bedjet/bedjet_hub.cpp index 6404298697..fea7080de6 100644 --- a/esphome/components/bedjet/bedjet_hub.cpp +++ b/esphome/components/bedjet/bedjet_hub.cpp @@ -3,6 +3,7 @@ #include "bedjet_hub.h" #include "bedjet_child.h" #include "bedjet_const.h" +#include "esphome/core/application.h" #include namespace esphome { diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index 915d2882d3..d8b2111cb0 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -2,6 +2,7 @@ #include "esphome/core/log.h" #include "esphome/core/macros.h" +#include "esphome/core/application.h" #ifdef USE_ESP32 @@ -177,7 +178,7 @@ void BluetoothProxy::loop() { // Flush any pending BLE advertisements that have been accumulated but not yet sent if (this->raw_advertisements_) { static uint32_t last_flush_time = 0; - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); // Flush accumulated advertisements every 100ms if (now - last_flush_time >= 100) { diff --git a/esphome/components/cse7766/cse7766.cpp b/esphome/components/cse7766/cse7766.cpp index 88a91e374a..b0876778a3 100644 --- a/esphome/components/cse7766/cse7766.cpp +++ b/esphome/components/cse7766/cse7766.cpp @@ -1,5 +1,6 @@ #include "cse7766.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace cse7766 { @@ -7,7 +8,7 @@ namespace cse7766 { static const char *const TAG = "cse7766"; void CSE7766Component::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_transmission_ >= 500) { // last transmission too long ago. Reset RX index. this->raw_data_index_ = 0; diff --git a/esphome/components/current_based/current_based_cover.cpp b/esphome/components/current_based/current_based_cover.cpp index 8404e07894..8bb27dbeca 100644 --- a/esphome/components/current_based/current_based_cover.cpp +++ b/esphome/components/current_based/current_based_cover.cpp @@ -1,6 +1,7 @@ #include "current_based_cover.h" #include "esphome/core/hal.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" #include namespace esphome { @@ -60,7 +61,7 @@ void CurrentBasedCover::loop() { if (this->current_operation == COVER_OPERATION_IDLE) return; - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (this->current_operation == COVER_OPERATION_OPENING) { if (this->malfunction_detection_ && this->is_closing_()) { // Malfunction diff --git a/esphome/components/daly_bms/daly_bms.cpp b/esphome/components/daly_bms/daly_bms.cpp index 929f31e008..1dd0520465 100644 --- a/esphome/components/daly_bms/daly_bms.cpp +++ b/esphome/components/daly_bms/daly_bms.cpp @@ -1,6 +1,7 @@ #include "daly_bms.h" #include #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace daly_bms { @@ -32,7 +33,7 @@ void DalyBmsComponent::update() { } void DalyBmsComponent::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (this->receiving_ && (now - this->last_transmission_ >= 200)) { // last transmission too long ago. Reset RX index. ESP_LOGW(TAG, "Last transmission too long ago. Reset RX index."); diff --git a/esphome/components/debug/debug_component.cpp b/esphome/components/debug/debug_component.cpp index 5bcc676247..c4de42c7e9 100644 --- a/esphome/components/debug/debug_component.cpp +++ b/esphome/components/debug/debug_component.cpp @@ -70,7 +70,7 @@ void DebugComponent::loop() { #ifdef USE_SENSOR // calculate loop time - from last call to this one if (this->loop_time_sensor_ != nullptr) { - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); uint32_t loop_time = now - this->last_loop_timetag_; this->max_loop_time_ = std::max(this->max_loop_time_, loop_time); this->last_loop_timetag_ = now; diff --git a/esphome/components/endstop/endstop_cover.cpp b/esphome/components/endstop/endstop_cover.cpp index 1190acc46b..381f098eb5 100644 --- a/esphome/components/endstop/endstop_cover.cpp +++ b/esphome/components/endstop/endstop_cover.cpp @@ -1,6 +1,7 @@ #include "endstop_cover.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include "esphome/core/application.h" namespace esphome { namespace endstop { @@ -65,7 +66,7 @@ void EndstopCover::loop() { if (this->current_operation == COVER_OPERATION_IDLE) return; - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (this->current_operation == COVER_OPERATION_OPENING && this->is_open_()) { float dur = (now - this->start_dir_time_) / 1e3f; diff --git a/esphome/components/esp32_ble/ble_advertising.cpp b/esphome/components/esp32_ble/ble_advertising.cpp index 1d340c76d9..8d43b5af33 100644 --- a/esphome/components/esp32_ble/ble_advertising.cpp +++ b/esphome/components/esp32_ble/ble_advertising.cpp @@ -6,6 +6,7 @@ #include #include "ble_uuid.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace esp32_ble { @@ -143,7 +144,7 @@ void BLEAdvertising::loop() { if (this->raw_advertisements_callbacks_.empty()) { return; } - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_advertisement_time_ > this->advertising_cycle_time_) { this->stop(); this->current_adv_index_ += 1; diff --git a/esphome/components/esp32_camera/esp32_camera.cpp b/esphome/components/esp32_camera/esp32_camera.cpp index e9e9d3cffb..60accba747 100644 --- a/esphome/components/esp32_camera/esp32_camera.cpp +++ b/esphome/components/esp32_camera/esp32_camera.cpp @@ -3,6 +3,7 @@ #include "esp32_camera.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include "esphome/core/application.h" #include @@ -162,7 +163,7 @@ void ESP32Camera::loop() { } // request idle image every idle_update_interval - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) { this->last_idle_request_ = now; this->request_image(IDLE); diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index b720425506..d74714838f 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -92,7 +92,7 @@ void ESP32ImprovComponent::loop() { if (!this->incoming_data_.empty()) this->process_incoming_data_(); - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); switch (this->state_) { case improv::STATE_STOPPED: diff --git a/esphome/components/esp32_touch/esp32_touch.cpp b/esphome/components/esp32_touch/esp32_touch.cpp index 69e4e37cec..1dcb39e5de 100644 --- a/esphome/components/esp32_touch/esp32_touch.cpp +++ b/esphome/components/esp32_touch/esp32_touch.cpp @@ -288,7 +288,7 @@ uint32_t ESP32TouchComponent::component_touch_pad_read(touch_pad_t tp) { } void ESP32TouchComponent::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); bool should_print = this->setup_mode_ && now - this->setup_mode_last_log_print_ > 250; for (auto *child : this->children_) { child->value_ = this->component_touch_pad_read(child->get_touch_pad()); diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp index 68a6e617fc..79ef0715d8 100644 --- a/esphome/components/ethernet/ethernet_component.cpp +++ b/esphome/components/ethernet/ethernet_component.cpp @@ -240,7 +240,7 @@ void EthernetComponent::setup() { } void EthernetComponent::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); switch (this->state_) { case EthernetComponentState::STOPPED: diff --git a/esphome/components/feedback/feedback_cover.cpp b/esphome/components/feedback/feedback_cover.cpp index fa3166ba65..e419ee6229 100644 --- a/esphome/components/feedback/feedback_cover.cpp +++ b/esphome/components/feedback/feedback_cover.cpp @@ -1,6 +1,7 @@ #include "feedback_cover.h" #include "esphome/core/hal.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace feedback { @@ -220,7 +221,7 @@ void FeedbackCover::set_open_obstacle_sensor(binary_sensor::BinarySensor *open_o void FeedbackCover::loop() { if (this->current_operation == COVER_OPERATION_IDLE) return; - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); // Recompute position every loop cycle this->recompute_position_(); diff --git a/esphome/components/gcja5/gcja5.cpp b/esphome/components/gcja5/gcja5.cpp index b1db58654b..64b237515b 100644 --- a/esphome/components/gcja5/gcja5.cpp +++ b/esphome/components/gcja5/gcja5.cpp @@ -6,6 +6,7 @@ */ #include "gcja5.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" #include namespace esphome { @@ -16,7 +17,7 @@ static const char *const TAG = "gcja5"; void GCJA5Component::setup() { ESP_LOGCONFIG(TAG, "Setting up gcja5..."); } void GCJA5Component::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_transmission_ >= 500) { // last transmission too long ago. Reset RX index. this->rx_message_.clear(); diff --git a/esphome/components/growatt_solar/growatt_solar.cpp b/esphome/components/growatt_solar/growatt_solar.cpp index c4ed5ab841..60fd1379e8 100644 --- a/esphome/components/growatt_solar/growatt_solar.cpp +++ b/esphome/components/growatt_solar/growatt_solar.cpp @@ -1,5 +1,6 @@ #include "growatt_solar.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace growatt_solar { @@ -18,7 +19,7 @@ void GrowattSolar::loop() { void GrowattSolar::update() { // If our last send has had no reply yet, and it wasn't that long ago, do nothing. - uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_send_ < this->get_update_interval() / 2) { return; } diff --git a/esphome/components/kuntze/kuntze.cpp b/esphome/components/kuntze/kuntze.cpp index e50dafca86..8ab7af8cd9 100644 --- a/esphome/components/kuntze/kuntze.cpp +++ b/esphome/components/kuntze/kuntze.cpp @@ -1,5 +1,6 @@ #include "kuntze.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace kuntze { @@ -60,7 +61,7 @@ void Kuntze::on_modbus_data(const std::vector &data) { } void Kuntze::loop() { - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); // timeout after 15 seconds if (this->waiting_ && (now - this->last_send_ > 15000)) { ESP_LOGW(TAG, "timed out waiting for response"); diff --git a/esphome/components/matrix_keypad/matrix_keypad.cpp b/esphome/components/matrix_keypad/matrix_keypad.cpp index 8537997935..6cb4fc4f3c 100644 --- a/esphome/components/matrix_keypad/matrix_keypad.cpp +++ b/esphome/components/matrix_keypad/matrix_keypad.cpp @@ -1,5 +1,6 @@ #include "matrix_keypad.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace matrix_keypad { @@ -28,7 +29,7 @@ void MatrixKeypad::setup() { void MatrixKeypad::loop() { static uint32_t active_start = 0; static int active_key = -1; - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); int key = -1; bool error = false; int pos = 0, row, col; diff --git a/esphome/components/max7219digit/max7219digit.cpp b/esphome/components/max7219digit/max7219digit.cpp index 13b75ca734..154accd66f 100644 --- a/esphome/components/max7219digit/max7219digit.cpp +++ b/esphome/components/max7219digit/max7219digit.cpp @@ -2,6 +2,7 @@ #include "esphome/core/log.h" #include "esphome/core/helpers.h" #include "esphome/core/hal.h" +#include "esphome/core/application.h" #include "max7219font.h" #include @@ -63,7 +64,7 @@ void MAX7219Component::dump_config() { } void MAX7219Component::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); const uint32_t millis_since_last_scroll = now - this->last_scroll_; const size_t first_line_size = this->max_displaybuffer_[0].size(); // check if the buffer has shrunk past the current position since last update diff --git a/esphome/components/modbus/modbus.cpp b/esphome/components/modbus/modbus.cpp index 47deea83e6..80c2ffe3d6 100644 --- a/esphome/components/modbus/modbus.cpp +++ b/esphome/components/modbus/modbus.cpp @@ -1,6 +1,7 @@ #include "modbus.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" +#include "esphome/core/application.h" namespace esphome { namespace modbus { @@ -13,7 +14,7 @@ void Modbus::setup() { } } void Modbus::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); while (this->available()) { uint8_t byte; diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index 1fcef3293c..e3722099a7 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -345,7 +345,7 @@ void MQTTClientComponent::loop() { this->disconnect_reason_.reset(); } - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); switch (this->state_) { case MQTT_CLIENT_DISABLED: diff --git a/esphome/components/pmsx003/pmsx003.cpp b/esphome/components/pmsx003/pmsx003.cpp index 11626768d8..0abed8a5a4 100644 --- a/esphome/components/pmsx003/pmsx003.cpp +++ b/esphome/components/pmsx003/pmsx003.cpp @@ -1,5 +1,6 @@ #include "pmsx003.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace pmsx003 { @@ -42,7 +43,7 @@ void PMSX003Component::dump_config() { } void PMSX003Component::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); // If we update less often than it takes the device to stabilise, spin the fan down // rather than running it constantly. It does take some time to stabilise, so we diff --git a/esphome/components/pzem004t/pzem004t.cpp b/esphome/components/pzem004t/pzem004t.cpp index 35b66b03f2..356847825e 100644 --- a/esphome/components/pzem004t/pzem004t.cpp +++ b/esphome/components/pzem004t/pzem004t.cpp @@ -1,5 +1,6 @@ #include "pzem004t.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" #include namespace esphome { @@ -16,7 +17,7 @@ void PZEM004T::setup() { } void PZEM004T::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_read_ > 500 && this->available() < 7) { while (this->available()) this->read(); diff --git a/esphome/components/rf_bridge/rf_bridge.cpp b/esphome/components/rf_bridge/rf_bridge.cpp index 3b3e00a416..52ce037dbe 100644 --- a/esphome/components/rf_bridge/rf_bridge.cpp +++ b/esphome/components/rf_bridge/rf_bridge.cpp @@ -1,5 +1,6 @@ #include "rf_bridge.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" #include #include @@ -128,7 +129,7 @@ void RFBridgeComponent::write_byte_str_(const std::string &codes) { } void RFBridgeComponent::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_bridge_byte_ > 50) { this->rx_buffer_.clear(); this->last_bridge_byte_ = now; diff --git a/esphome/components/sds011/sds011.cpp b/esphome/components/sds011/sds011.cpp index 0c04ff557f..a34059d85d 100644 --- a/esphome/components/sds011/sds011.cpp +++ b/esphome/components/sds011/sds011.cpp @@ -1,5 +1,6 @@ #include "sds011.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace sds011 { @@ -75,7 +76,7 @@ void SDS011Component::dump_config() { } void SDS011Component::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if ((now - this->last_transmission_ >= 500) && this->data_index_) { // last transmission too long ago. Reset RX index. ESP_LOGV(TAG, "Last transmission too long ago. Reset RX index."); diff --git a/esphome/components/slow_pwm/slow_pwm_output.cpp b/esphome/components/slow_pwm/slow_pwm_output.cpp index d6b2cdfe12..643294303c 100644 --- a/esphome/components/slow_pwm/slow_pwm_output.cpp +++ b/esphome/components/slow_pwm/slow_pwm_output.cpp @@ -1,5 +1,6 @@ #include "slow_pwm_output.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace slow_pwm { @@ -39,7 +40,7 @@ void SlowPWMOutput::set_output_state_(bool new_state) { } void SlowPWMOutput::loop() { - uint32_t now = millis(); + uint32_t now = App.get_loop_component_start_time(); float scaled_state = this->state_ * this->period_; if (now - this->period_start_time_ >= this->period_) { diff --git a/esphome/components/sprinkler/sprinkler.cpp b/esphome/components/sprinkler/sprinkler.cpp index 3cfb5ccdee..50ea3eff51 100644 --- a/esphome/components/sprinkler/sprinkler.cpp +++ b/esphome/components/sprinkler/sprinkler.cpp @@ -20,7 +20,7 @@ SprinklerSwitch::SprinklerSwitch(switch_::Switch *off_switch, switch_::Switch *o bool SprinklerSwitch::is_latching_valve() { return (this->off_switch_ != nullptr) && (this->on_switch_ != nullptr); } void SprinklerSwitch::loop() { - if ((this->pinned_millis_) && (millis() > this->pinned_millis_ + this->pulse_duration_)) { + if ((this->pinned_millis_) && (App.get_loop_component_start_time() > this->pinned_millis_ + this->pulse_duration_)) { this->pinned_millis_ = 0; // reset tracker if (this->off_switch_->state) { this->off_switch_->turn_off(); @@ -148,22 +148,23 @@ SprinklerValveOperator::SprinklerValveOperator(SprinklerValve *valve, Sprinkler : controller_(controller), valve_(valve) {} void SprinklerValveOperator::loop() { - if (millis() >= this->start_millis_) { // dummy check + uint32_t now = App.get_loop_component_start_time(); + if (now >= this->start_millis_) { // dummy check switch (this->state_) { case STARTING: - if (millis() > (this->start_millis_ + this->start_delay_)) { + if (now > (this->start_millis_ + this->start_delay_)) { this->run_(); // start_delay_ has been exceeded, so ensure both valves are on and update the state } break; case ACTIVE: - if (millis() > (this->start_millis_ + this->start_delay_ + this->run_duration_)) { + if (now > (this->start_millis_ + this->start_delay_ + this->run_duration_)) { this->stop(); // start_delay_ + run_duration_ has been exceeded, start shutting down } break; case STOPPING: - if (millis() > (this->stop_millis_ + this->stop_delay_)) { + if (now > (this->stop_millis_ + this->stop_delay_)) { this->kill_(); // stop_delay_has been exceeded, ensure all valves are off } break; diff --git a/esphome/components/time_based/time_based_cover.cpp b/esphome/components/time_based/time_based_cover.cpp index e1936d5ee1..ec219d6db7 100644 --- a/esphome/components/time_based/time_based_cover.cpp +++ b/esphome/components/time_based/time_based_cover.cpp @@ -1,6 +1,7 @@ #include "time_based_cover.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include "esphome/core/application.h" namespace esphome { namespace time_based { @@ -26,7 +27,7 @@ void TimeBasedCover::loop() { if (this->current_operation == COVER_OPERATION_IDLE) return; - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); // Recompute position every loop cycle this->recompute_position_(); diff --git a/esphome/components/uart/switch/uart_switch.cpp b/esphome/components/uart/switch/uart_switch.cpp index 1edb54641b..96f50ff50f 100644 --- a/esphome/components/uart/switch/uart_switch.cpp +++ b/esphome/components/uart/switch/uart_switch.cpp @@ -1,5 +1,6 @@ #include "uart_switch.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace uart { @@ -8,7 +9,7 @@ static const char *const TAG = "uart.switch"; void UARTSwitch::loop() { if (this->send_every_) { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); if (now - this->last_transmission_ > this->send_every_) { this->write_command_(this->state); this->last_transmission_ = now; diff --git a/esphome/components/uponor_smatrix/climate/uponor_smatrix_climate.cpp b/esphome/components/uponor_smatrix/climate/uponor_smatrix_climate.cpp index 5afc628db3..cc9b8a0f90 100644 --- a/esphome/components/uponor_smatrix/climate/uponor_smatrix_climate.cpp +++ b/esphome/components/uponor_smatrix/climate/uponor_smatrix_climate.cpp @@ -1,6 +1,7 @@ #include "uponor_smatrix_climate.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace uponor_smatrix { @@ -13,7 +14,7 @@ void UponorSmatrixClimate::dump_config() { } void UponorSmatrixClimate::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); // Publish state after all update packets are processed if (this->last_data_ != 0 && (now - this->last_data_ > 100) && this->target_temperature_raw_ != 0) { diff --git a/esphome/components/uponor_smatrix/uponor_smatrix.cpp b/esphome/components/uponor_smatrix/uponor_smatrix.cpp index e058de2852..2dbbef72ab 100644 --- a/esphome/components/uponor_smatrix/uponor_smatrix.cpp +++ b/esphome/components/uponor_smatrix/uponor_smatrix.cpp @@ -1,5 +1,6 @@ #include "uponor_smatrix.h" #include "esphome/core/log.h" +#include "esphome/core/application.h" namespace esphome { namespace uponor_smatrix { @@ -35,7 +36,7 @@ void UponorSmatrixComponent::dump_config() { } void UponorSmatrixComponent::loop() { - const uint32_t now = millis(); + const uint32_t now = App.get_loop_component_start_time(); // Discard stale data if (!this->rx_buffer_.empty() && (now - this->last_rx_ > 50)) { diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 3f5a283fd8..a81a2c580c 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -67,22 +67,32 @@ void Application::loop() { uint32_t new_app_state = 0; this->scheduler.call(); - this->feed_wdt(); + + // Get the initial loop time at the start + uint32_t last_op_end_time = millis(); + + // Feed WDT with time + this->feed_wdt(last_op_end_time); + for (Component *component : this->looping_components_) { + // Update the cached time before each component runs + this->loop_component_start_time_ = last_op_end_time; + { this->set_current_component(component); - WarnIfComponentBlockingGuard guard{component}; + WarnIfComponentBlockingGuard guard{component, last_op_end_time}; component->call(); + // Use the finish method to get the current time as the end time + last_op_end_time = guard.finish(); } new_app_state |= component->get_component_state(); this->app_state_ |= new_app_state; - this->feed_wdt(); + this->feed_wdt(last_op_end_time); } this->app_state_ = new_app_state; - const uint32_t now = millis(); - - auto elapsed = now - this->last_loop_; + // Use the last component's end time instead of calling millis() again + auto elapsed = last_op_end_time - this->last_loop_; if (elapsed >= this->loop_interval_ || HighFrequencyLoopRequester::is_high_frequency()) { yield(); } else { @@ -94,7 +104,7 @@ void Application::loop() { delay_time = std::min(next_schedule, delay_time); delay(delay_time); } - this->last_loop_ = now; + this->last_loop_ = last_op_end_time; if (this->dump_config_at_ < this->components_.size()) { if (this->dump_config_at_ == 0) { @@ -109,10 +119,12 @@ void Application::loop() { } } -void IRAM_ATTR HOT Application::feed_wdt() { +void IRAM_ATTR HOT Application::feed_wdt(uint32_t time) { static uint32_t last_feed = 0; - uint32_t now = micros(); - if (now - last_feed > 3000) { + // Use provided time if available, otherwise get current time + uint32_t now = time ? time : millis(); + // Compare in milliseconds (3ms threshold) + if (now - last_feed > 3) { arch_feed_wdt(); last_feed = now; #ifdef USE_STATUS_LED diff --git a/esphome/core/application.h b/esphome/core/application.h index e64e2b7655..aa44d9ba1d 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -217,6 +217,9 @@ class Application { std::string get_compilation_time() const { return this->compilation_time_; } + /// Get the cached time in milliseconds from when the current component started its loop execution + inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; } + /** Set the target interval with which to run the loop() calls. * If the loop() method takes longer than the target interval, ESPHome won't * sleep in loop(), but if the time spent in loop() is small than the target, ESPHome @@ -236,7 +239,7 @@ class Application { void schedule_dump_config() { this->dump_config_at_ = 0; } - void feed_wdt(); + void feed_wdt(uint32_t time = 0); void reboot(); @@ -551,6 +554,7 @@ class Application { size_t dump_config_at_{SIZE_MAX}; uint32_t app_state_{0}; Component *current_component_{nullptr}; + uint32_t loop_component_start_time_{0}; }; /// Global storage of Application pointer - only one Application can exist. diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index a7e451b93d..1141e4067d 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -240,10 +240,12 @@ void PollingComponent::stop_poller() { uint32_t PollingComponent::get_update_interval() const { return this->update_interval_; } void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; } -WarnIfComponentBlockingGuard::WarnIfComponentBlockingGuard(Component *component) - : started_(millis()), component_(component) {} -WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() { - uint32_t blocking_time = millis() - this->started_; +WarnIfComponentBlockingGuard::WarnIfComponentBlockingGuard(Component *component, uint32_t start_time) + : started_(start_time), component_(component) {} +uint32_t WarnIfComponentBlockingGuard::finish() { + uint32_t curr_time = millis(); + + uint32_t blocking_time = curr_time - this->started_; bool should_warn; if (this->component_ != nullptr) { should_warn = this->component_->should_warn_of_blocking(blocking_time); @@ -254,8 +256,11 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() { const char *src = component_ == nullptr ? "" : component_->get_component_source(); ESP_LOGW(TAG, "Component %s took a long time for an operation (%" PRIu32 " ms).", src, blocking_time); ESP_LOGW(TAG, "Components should block for at most 30 ms."); - ; } + + return curr_time; } +WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {} + } // namespace esphome diff --git a/esphome/core/component.h b/esphome/core/component.h index 412074282d..7b3e12eb59 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -339,7 +339,11 @@ class PollingComponent : public Component { class WarnIfComponentBlockingGuard { public: - WarnIfComponentBlockingGuard(Component *component); + WarnIfComponentBlockingGuard(Component *component, uint32_t start_time); + + // Finish the timing operation and return the current time + uint32_t finish(); + ~WarnIfComponentBlockingGuard(); protected: diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index b4f617d405..2dea450ead 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -229,8 +229,11 @@ void HOT Scheduler::call() { // - timeouts/intervals get added, potentially invalidating vector pointers // - timeouts/intervals get cancelled { - WarnIfComponentBlockingGuard guard{item->component}; + uint32_t now_ms = millis(); + WarnIfComponentBlockingGuard guard{item->component, now_ms}; item->callback(); + // Call finish to ensure blocking time is properly calculated and reported + guard.finish(); } }