From 58746b737fb58c81df860360e469629452ef1e28 Mon Sep 17 00:00:00 2001 From: Big Mike Date: Sat, 24 Jan 2026 15:07:12 -0600 Subject: [PATCH] [sen5x] Eliminate product name string (#13489) --- esphome/components/sen5x/sen5x.cpp | 68 +++++++++++++++--------------- esphome/components/sen5x/sen5x.h | 6 +-- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/esphome/components/sen5x/sen5x.cpp b/esphome/components/sen5x/sen5x.cpp index b23837f2b1..fc4932d867 100644 --- a/esphome/components/sen5x/sen5x.cpp +++ b/esphome/components/sen5x/sen5x.cpp @@ -30,6 +30,19 @@ static const int8_t SEN5X_INDEX_SCALE_FACTOR = 10; // static const int8_t SEN5X_MIN_INDEX_VALUE = 1 * SEN5X_INDEX_SCALE_FACTOR; // must be adjusted by the scale factor static const int16_t SEN5X_MAX_INDEX_VALUE = 500 * SEN5X_INDEX_SCALE_FACTOR; // must be adjusted by the scale factor +static const LogString *type_to_string(Sen5xType type) { + switch (type) { + case Sen5xType::SEN50: + return LOG_STR("SEN50"); + case Sen5xType::SEN54: + return LOG_STR("SEN54"); + case Sen5xType::SEN55: + return LOG_STR("SEN55"); + default: + return LOG_STR("UNKNOWN"); + } +} + static const LogString *rht_accel_mode_to_string(RhtAccelerationMode mode) { switch (mode) { case LOW_ACCELERATION: @@ -104,50 +117,35 @@ void SEN5XComponent::setup() { this->mark_failed(); return; } - // 2 ASCII bytes are encoded in an int - const uint16_t *current_int = raw_product_name; - char current_char; - uint8_t max = 16; - do { - // first char - current_char = *current_int >> 8; - if (current_char) { - this->product_name_.push_back(current_char); - // second char - current_char = *current_int & 0xFF; - if (current_char) { - this->product_name_.push_back(current_char); - } - } - current_int++; - } while (current_char && --max); - - Sen5xType sen5x_type = UNKNOWN; - if (this->product_name_ == "SEN50") { - sen5x_type = SEN50; + const char *product_name = sensirion_convert_to_string_in_place(raw_product_name, 16); + if (strncmp(product_name, "SEN50", 5) == 0) { + this->type_ = Sen5xType::SEN50; + } else if (strncmp(product_name, "SEN54", 5) == 0) { + this->type_ = Sen5xType::SEN54; + } else if (strncmp(product_name, "SEN55", 5) == 0) { + this->type_ = Sen5xType::SEN55; } else { - if (this->product_name_ == "SEN54") { - sen5x_type = SEN54; - } else { - if (this->product_name_ == "SEN55") { - sen5x_type = SEN55; - } - } + this->type_ = Sen5xType::UNKNOWN; + ESP_LOGE(TAG, "Unknown product name: %.32s", product_name); + this->error_code_ = PRODUCT_NAME_FAILED; + this->mark_failed(); + return; } - ESP_LOGD(TAG, "Product name: %s", this->product_name_.c_str()); - if (this->humidity_sensor_ && sen5x_type == SEN50) { + + ESP_LOGD(TAG, "Type: %s", LOG_STR_ARG(type_to_string(this->type_))); + if (this->humidity_sensor_ && this->type_ == Sen5xType::SEN50) { ESP_LOGE(TAG, "Relative humidity requires a SEN54 or SEN55"); this->humidity_sensor_ = nullptr; // mark as not used } - if (this->temperature_sensor_ && sen5x_type == SEN50) { + if (this->temperature_sensor_ && this->type_ == Sen5xType::SEN50) { ESP_LOGE(TAG, "Temperature requires a SEN54 or SEN55"); this->temperature_sensor_ = nullptr; // mark as not used } - if (this->voc_sensor_ && sen5x_type == SEN50) { + if (this->voc_sensor_ && this->type_ == Sen5xType::SEN50) { ESP_LOGE(TAG, "VOC requires a SEN54 or SEN55"); this->voc_sensor_ = nullptr; // mark as not used } - if (this->nox_sensor_ && sen5x_type != SEN55) { + if (this->nox_sensor_ && this->type_ != Sen5xType::SEN55) { ESP_LOGE(TAG, "NOx requires a SEN55"); this->nox_sensor_ = nullptr; // mark as not used } @@ -267,10 +265,10 @@ void SEN5XComponent::dump_config() { } } ESP_LOGCONFIG(TAG, - " Product name: %s\n" + " Type: %s\n" " Firmware version: %d\n" " Serial number: %s", - this->product_name_.c_str(), this->firmware_version_, this->serial_number_); + LOG_STR_ARG(type_to_string(this->type_)), this->firmware_version_, this->serial_number_); if (this->auto_cleaning_interval_.has_value()) { ESP_LOGCONFIG(TAG, " Auto cleaning interval: %" PRId32 "s", this->auto_cleaning_interval_.value()); } diff --git a/esphome/components/sen5x/sen5x.h b/esphome/components/sen5x/sen5x.h index 822160833e..e3bf931b41 100644 --- a/esphome/components/sen5x/sen5x.h +++ b/esphome/components/sen5x/sen5x.h @@ -24,6 +24,8 @@ enum RhtAccelerationMode : uint16_t { HIGH_ACCELERATION = 2, }; +enum class Sen5xType : uint8_t { SEN50, SEN54, SEN55, UNKNOWN }; + struct GasTuning { uint16_t index_offset; uint16_t learning_time_offset_hours; @@ -49,8 +51,6 @@ class SEN5XComponent : public PollingComponent, public sensirion_common::Sensiri void dump_config() override; void update() override; - enum Sen5xType { SEN50, SEN54, SEN55, UNKNOWN }; - void set_pm_1_0_sensor(sensor::Sensor *pm_1_0) { this->pm_1_0_sensor_ = pm_1_0; } void set_pm_2_5_sensor(sensor::Sensor *pm_2_5) { this->pm_2_5_sensor_ = pm_2_5; } void set_pm_4_0_sensor(sensor::Sensor *pm_4_0) { this->pm_4_0_sensor_ = pm_4_0; } @@ -106,6 +106,7 @@ class SEN5XComponent : public PollingComponent, public sensirion_common::Sensiri uint16_t voc_baseline_state_[4]{0}; uint32_t voc_baseline_time_; uint16_t firmware_version_; + Sen5xType type_{Sen5xType::UNKNOWN}; ERRORCODE error_code_; bool initialized_{false}; bool store_baseline_; @@ -127,7 +128,6 @@ class SEN5XComponent : public PollingComponent, public sensirion_common::Sensiri optional nox_tuning_params_; optional temperature_compensation_; ESPPreferenceObject pref_; - std::string product_name_; }; } // namespace sen5x