diff --git a/esphome/components/bme680_bsec/bme680_bsec.cpp b/esphome/components/bme680_bsec/bme680_bsec.cpp index 3f0082e1af7..216247013a1 100644 --- a/esphome/components/bme680_bsec/bme680_bsec.cpp +++ b/esphome/components/bme680_bsec/bme680_bsec.cpp @@ -1,7 +1,7 @@ #include "bme680_bsec.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" -#include +#include "esphome/core/progmem.h" namespace esphome::bme680_bsec { #ifdef USE_BSEC @@ -9,7 +9,7 @@ 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"}; +PROGMEM_STRING_TABLE(IaqAccuracyStrings, "Stabilizing", "Uncertain", "Calibrating", "Calibrated"); std::vector BME680BSECComponent::instances; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -391,10 +391,16 @@ void BME680BSECComponent::publish_(const bsec_output_t *outputs, uint8_t num_out switch (outputs[i].sensor_id) { case BSEC_OUTPUT_IAQ: case BSEC_OUTPUT_STATIC_IAQ: { - uint8_t accuracy = std::min(outputs[i].accuracy, std::size(IAQ_ACCURACY_STATES) - 1); + uint8_t accuracy = std::min(outputs[i].accuracy, IaqAccuracyStrings::LAST_INDEX); this->queue_push_([this, signal]() { this->publish_sensor_(this->iaq_sensor_, signal); }); this->queue_push_([this, accuracy]() { - this->publish_sensor_(this->iaq_accuracy_text_sensor_, IAQ_ACCURACY_STATES[accuracy]); + char text[12]; + ESPHOME_strncpy_P(text, + reinterpret_cast( + IaqAccuracyStrings::get_progmem_str(accuracy, IaqAccuracyStrings::LAST_INDEX)), + sizeof(text)); + text[sizeof(text) - 1] = '\0'; + this->publish_sensor_(this->iaq_accuracy_text_sensor_, text); }); this->queue_push_([this, accuracy]() { this->publish_sensor_(this->iaq_accuracy_sensor_, accuracy, true); }); @@ -440,7 +446,7 @@ void BME680BSECComponent::publish_sensor_(sensor::Sensor *sensor, float value, b sensor->publish_state(value); } -void BME680BSECComponent::publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value) { +void BME680BSECComponent::publish_sensor_(text_sensor::TextSensor *sensor, const char *value) { if (!sensor || (sensor->has_state() && sensor->state == value)) { return; } diff --git a/esphome/components/bme680_bsec/bme680_bsec.h b/esphome/components/bme680_bsec/bme680_bsec.h index ff974d1c6f0..88389e8ba19 100644 --- a/esphome/components/bme680_bsec/bme680_bsec.h +++ b/esphome/components/bme680_bsec/bme680_bsec.h @@ -77,7 +77,7 @@ class BME680BSECComponent final : public Component, public i2c::I2CDevice { int64_t get_time_ns_(); void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only = false); - void publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value); + void publish_sensor_(text_sensor::TextSensor *sensor, const char *value); void snapshot_state_(); // Fetch the current BSEC library state and save it in the bsec_state_data_ member (volatile // memory)