[core] Use numeric scheduler ids instead of string names (1/5) (#20374)

This commit is contained in:
J. Nick Koston
2026-10-09 07:10:04 -10:00
committed by GitHub
parent a2434f9aa3
commit bc6fecfadb
10 changed files with 39 additions and 16 deletions
+4 -2
View File
@@ -5,18 +5,20 @@ namespace esphome::aqi {
ESPHOME_LOG_TAG(TAG, "aqi");
static constexpr uint32_t CALCULATE_DEFER_ID = 0;
void AQISensor::setup() {
if (this->pm_2_5_sensor_ != nullptr) {
this->pm_2_5_sensor_->add_on_state_callback([this](float value) {
this->pm_2_5_value_ = value;
// Defer calculation to avoid double-publishing if both sensors update in the same loop
this->defer("update", [this]() { this->calculate_aqi_(); });
this->defer(CALCULATE_DEFER_ID, [this]() { this->calculate_aqi_(); });
});
}
if (this->pm_10_0_sensor_ != nullptr) {
this->pm_10_0_sensor_->add_on_state_callback([this](float value) {
this->pm_10_0_value_ = value;
this->defer("update", [this]() { this->calculate_aqi_(); });
this->defer(CALCULATE_DEFER_ID, [this]() { this->calculate_aqi_(); });
});
}
}
@@ -13,6 +13,8 @@ namespace esphome::bme280_base {
ESPHOME_LOG_TAG(TAG, "bme280.sensor");
static constexpr uint32_t DATA_TIMEOUT_ID = 0;
static const uint8_t BME280_REGISTER_DIG_T1 = 0x88;
static const uint8_t BME280_REGISTER_DIG_T2 = 0x8A;
static const uint8_t BME280_REGISTER_DIG_T3 = 0x8C;
@@ -221,7 +223,7 @@ void BME280Component::update() {
meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
this->set_timeout(DATA_TIMEOUT_ID, uint32_t(ceilf(meas_time)), [this]() {
uint8_t data[8];
if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
ESP_LOGW(TAG, "Error reading registers");
+3 -1
View File
@@ -6,6 +6,8 @@ namespace esphome::bme680 {
ESPHOME_LOG_TAG(TAG, "bme680.sensor");
static constexpr uint32_t DATA_TIMEOUT_ID = 0;
static const uint8_t BME680_REGISTER_COEFF1 = 0x89;
static const uint8_t BME680_REGISTER_COEFF2 = 0xE1;
@@ -242,7 +244,7 @@ void BME680Component::update() {
return;
}
this->set_timeout("data", this->calc_meas_duration_(), [this]() { this->read_data_(); });
this->set_timeout(DATA_TIMEOUT_ID, this->calc_meas_duration_(), [this]() { this->read_data_(); });
}
uint8_t BME680Component::calc_heater_resistance_(uint16_t temperature) {
@@ -7,6 +7,8 @@ namespace esphome::bme680_bsec {
#ifdef USE_BSEC
ESPHOME_LOG_TAG(TAG, "bme680_bsec.sensor");
static constexpr uint32_t READ_TIMEOUT_ID = 0;
static const std::string IAQ_ACCURACY_STATES[4] = {"Stabilizing", "Uncertain", "Calibrating", "Calibrated"};
std::vector<BME680BSECComponent *>
@@ -259,7 +261,7 @@ void BME680BSECComponent::run_() {
this->snapshot_state_();
ESP_LOGV(TAG, "Queueing read in %ums", meas_dur);
this->set_timeout("read", meas_dur, [this]() { this->read_(); });
this->set_timeout(READ_TIMEOUT_ID, meas_dur, [this]() { this->read_(); });
} else {
ESP_LOGV(TAG, "Measurement not required");
this->read_();
@@ -15,6 +15,8 @@ namespace esphome::bme68x_bsec2 {
ESPHOME_LOG_TAG(TAG, "bme68x_bsec2.sensor");
static constexpr uint32_t READ_TIMEOUT_ID = 0;
static constexpr const char *const IAQ_ACCURACY_STATES[4] = {"Stabilizing", "Uncertain", "Calibrating", "Calibrated"};
static bool is_no_new_data_warning(int8_t status) {
@@ -285,11 +287,11 @@ void BME68xBSEC2Component::run_() {
uint32_t meas_dur = bme68x_get_meas_dur(this->op_mode_, &bme68x_conf, &this->bme68x_);
ESP_LOGV(TAG, "Queueing read in %" PRIu32 "us", meas_dur);
this->trigger_time_ns_ = curr_time_ns;
this->set_timeout("read", meas_dur / 1000, [this]() { this->read_(this->trigger_time_ns_); });
this->set_timeout(READ_TIMEOUT_ID, meas_dur / 1000, [this]() { this->read_(this->trigger_time_ns_); });
} else {
ESP_LOGV(TAG, "Measurement not required, queueing immediate read");
this->trigger_time_ns_ = curr_time_ns;
this->set_timeout("read", 0, [this]() { this->read_(this->trigger_time_ns_); });
this->set_timeout(READ_TIMEOUT_ID, 0, [this]() { this->read_(this->trigger_time_ns_); });
}
}
+5 -2
View File
@@ -5,6 +5,9 @@ namespace esphome::bmp085 {
ESPHOME_LOG_TAG(TAG, "bmp085.sensor");
static constexpr uint32_t TEMPERATURE_TIMEOUT_ID = 0;
static constexpr uint32_t PRESSURE_TIMEOUT_ID = 1;
static const uint8_t BMP085_ADDRESS = 0x77;
static const uint8_t BMP085_REGISTER_AC1_H = 0xAA;
static const uint8_t BMP085_REGISTER_CONTROL = 0xF4;
@@ -16,7 +19,7 @@ void BMP085Component::update() {
if (!this->set_mode_(BMP085_CONTROL_MODE_TEMPERATURE))
return;
this->set_timeout("temperature", 5, [this]() { this->read_temperature_(); });
this->set_timeout(TEMPERATURE_TIMEOUT_ID, 5, [this]() { this->read_temperature_(); });
}
void BMP085Component::setup() {
uint8_t data[22];
@@ -84,7 +87,7 @@ void BMP085Component::read_temperature_() {
return;
}
this->set_timeout("pressure", 26, [this]() { this->read_pressure_(); });
this->set_timeout(PRESSURE_TIMEOUT_ID, 26, [this]() { this->read_pressure_(); });
}
void BMP085Component::read_pressure_() {
uint8_t buffer[3];
@@ -8,6 +8,8 @@ namespace esphome::bmp280_base {
ESPHOME_LOG_TAG(TAG, "bmp280.sensor");
static constexpr uint32_t DATA_TIMEOUT_ID = 0;
static const uint8_t BMP280_REGISTER_STATUS = 0xF3;
static const uint8_t BMP280_REGISTER_CONTROL = 0xF4;
static const uint8_t BMP280_REGISTER_CONFIG = 0xF5;
@@ -166,7 +168,7 @@ void BMP280Component::update() {
meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
this->set_timeout(DATA_TIMEOUT_ID, uint32_t(ceilf(meas_time)), [this]() {
int32_t t_fine = 0;
float temperature = this->read_temperature_(&t_fine);
if (std::isnan(temperature)) {
@@ -15,6 +15,8 @@ namespace esphome::bmp3xx_base {
ESPHOME_LOG_TAG(TAG, "bmp3xx.sensor");
static constexpr uint32_t DATA_TIMEOUT_ID = 0;
static const LogString *chip_type_to_str(uint8_t chip_type) {
switch (chip_type) {
case BMP388_ID:
@@ -170,7 +172,7 @@ void BMP3XXComponent::update() {
const uint32_t meas_timeout = uint32_t(ceilf(meas_time));
ESP_LOGVV(TAG, "measurement time %" PRIu32, meas_timeout);
this->set_timeout("data", meas_timeout, [this]() {
this->set_timeout(DATA_TIMEOUT_ID, meas_timeout, [this]() {
float temperature = 0.0f;
float pressure = 0.0f;
if (this->pressure_sensor_ != nullptr) {
@@ -19,6 +19,8 @@ namespace esphome::bmp581_base {
ESPHOME_LOG_TAG(TAG, "bmp581");
static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
// Oversampling strings indexed by Oversampling enum (0-7): NONE, X2, X4, X8, X16, X32, X64, X128
PROGMEM_STRING_TABLE(OversamplingStrings, "None", "2x", "4x", "8x", "16x", "32x", "64x", "128x", "");
@@ -272,7 +274,7 @@ void BMP581Component::update() {
ESP_LOGVV(TAG, "Measurement should take %d ms", this->conversion_time_);
this->set_timeout("measurement", this->conversion_time_, [this]() {
this->set_timeout(MEASUREMENT_TIMEOUT_ID, this->conversion_time_, [this]() {
float temperature = 0.0;
float pressure = 0.0;
@@ -9,6 +9,8 @@ namespace esphome::combination {
ESPHOME_LOG_TAG(TAG, "combination");
static constexpr uint32_t NEW_VALUE_DEFER_ID = 0;
void CombinationComponent::log_config_(const LogString *combo_type) {
LOG_SENSOR("", "Combination Sensor:", this);
ESP_LOGCONFIG(TAG, " Combination Type: %s", LOG_STR_ARG(combo_type));
@@ -43,8 +45,9 @@ void CombinationNoParameterComponent::setup() {
for (const auto &sensor : this->sensors_) {
// All sensor updates are deferred until the next loop. This avoids publishing the combined sensor's result
// repeatedly in the same loop if multiple source senors update.
sensor->add_on_state_callback(
[this](float value) -> void { this->defer("update", [this, value]() { this->handle_new_value(value); }); });
sensor->add_on_state_callback([this](float value) -> void {
this->defer(NEW_VALUE_DEFER_ID, [this, value]() { this->handle_new_value(value); });
});
}
}
@@ -119,8 +122,9 @@ void LinearCombinationComponent::setup() {
for (auto &source : this->sensor_sources_) {
// All sensor updates are deferred until the next loop. This avoids publishing the combined sensor's result
// repeatedly in the same loop if multiple source senors update.
source.sensor->add_on_state_callback(
[this](float value) -> void { this->defer("update", [this, value]() { this->handle_new_value(value); }); });
source.sensor->add_on_state_callback([this](float value) -> void {
this->defer(NEW_VALUE_DEFER_ID, [this, value]() { this->handle_new_value(value); });
});
}
}