From b2544d1e7b38d63278bdfb71460d5603b54d3583 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 25 Jan 2026 20:12:20 -1000 Subject: [PATCH 1/3] [mhz19] Refactor detection range logging to use LogString --- esphome/components/mhz19/mhz19.cpp | 56 +++++++++++++----------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index 259d597b44..f110dc8dcc 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -17,6 +17,19 @@ static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM[] = {0xFF, 0x01, 0x static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88}; static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10}; +static const LogString *detection_range_to_log_string(MHZ19DetectionRange range) { + switch (range) { + case MHZ19_DETECTION_RANGE_0_2000PPM: + return LOG_STR("0-2000 ppm"); + case MHZ19_DETECTION_RANGE_0_5000PPM: + return LOG_STR("0-5000 ppm"); + case MHZ19_DETECTION_RANGE_0_10000PPM: + return LOG_STR("0-10000 ppm"); + default: + return LOG_STR("default"); + } +} + uint8_t mhz19_checksum(const uint8_t *command) { uint8_t sum = 0; for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) { @@ -91,24 +104,24 @@ void MHZ19Component::abc_disable() { this->mhz19_write_command_(MHZ19_COMMAND_ABC_DISABLE, nullptr); } -void MHZ19Component::range_set(MHZ19DetectionRange detection_ppm) { - switch (detection_ppm) { - case MHZ19_DETECTION_RANGE_DEFAULT: - ESP_LOGV(TAG, "Using previously set detection range (no change)"); - break; +void MHZ19Component::range_set(MHZ19DetectionRange detection_range) { + const uint8_t *command; + switch (detection_range) { case MHZ19_DETECTION_RANGE_0_2000PPM: - ESP_LOGD(TAG, "Setting detection range to 0 to 2000ppm"); - this->mhz19_write_command_(MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM, nullptr); + command = MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM; break; case MHZ19_DETECTION_RANGE_0_5000PPM: - ESP_LOGD(TAG, "Setting detection range to 0 to 5000ppm"); - this->mhz19_write_command_(MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM, nullptr); + command = MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM; break; case MHZ19_DETECTION_RANGE_0_10000PPM: - ESP_LOGD(TAG, "Setting detection range to 0 to 10000ppm"); - this->mhz19_write_command_(MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM, nullptr); + command = MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM; break; + default: + ESP_LOGV(TAG, "Using previously set detection range (no change)"); + return; } + ESP_LOGD(TAG, "Setting detection range to %s", LOG_STR_ARG(detection_range_to_log_string(detection_range))); + this->mhz19_write_command_(command, nullptr); } bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) { @@ -140,26 +153,7 @@ void MHZ19Component::dump_config() { } ESP_LOGCONFIG(TAG, " Warmup time: %" PRIu32 " s", this->warmup_seconds_); - - const char *range_str; - switch (this->detection_range_) { - case MHZ19_DETECTION_RANGE_DEFAULT: - range_str = "default"; - break; - case MHZ19_DETECTION_RANGE_0_2000PPM: - range_str = "0 to 2000ppm"; - break; - case MHZ19_DETECTION_RANGE_0_5000PPM: - range_str = "0 to 5000ppm"; - break; - case MHZ19_DETECTION_RANGE_0_10000PPM: - range_str = "0 to 10000ppm"; - break; - default: - range_str = "default"; - break; - } - ESP_LOGCONFIG(TAG, " Detection range: %s", range_str); + ESP_LOGCONFIG(TAG, " Detection range: %s", LOG_STR_ARG(detection_range_to_log_string(this->detection_range_))); } } // namespace mhz19 From 5cdd012d262cd6adc4d6eb46b06c5671a049dc50 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 25 Jan 2026 22:09:22 -1000 Subject: [PATCH 2/3] Address review comments - Fix parameter name mismatch (clang-tidy): detection_ppm -> detection_range in header --- esphome/components/mhz19/mhz19.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/mhz19/mhz19.h b/esphome/components/mhz19/mhz19.h index 5898bab649..f7ceefd16d 100644 --- a/esphome/components/mhz19/mhz19.h +++ b/esphome/components/mhz19/mhz19.h @@ -32,7 +32,7 @@ class MHZ19Component : public PollingComponent, public uart::UARTDevice { void calibrate_zero(); void abc_enable(); void abc_disable(); - void range_set(MHZ19DetectionRange detection_ppm); + void range_set(MHZ19DetectionRange detection_range); void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } void set_co2_sensor(sensor::Sensor *co2_sensor) { co2_sensor_ = co2_sensor; } From b6a062445ab2955c0cb454bbdd6248d5fbba9505 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 25 Jan 2026 22:13:31 -1000 Subject: [PATCH 3/3] namespace --- esphome/components/mhz19/mhz19.cpp | 6 ++---- esphome/components/mhz19/mhz19.h | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index f110dc8dcc..b6b4031e16 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -3,8 +3,7 @@ #include -namespace esphome { -namespace mhz19 { +namespace esphome::mhz19 { static const char *const TAG = "mhz19"; static const uint8_t MHZ19_REQUEST_LENGTH = 8; @@ -156,5 +155,4 @@ void MHZ19Component::dump_config() { ESP_LOGCONFIG(TAG, " Detection range: %s", LOG_STR_ARG(detection_range_to_log_string(this->detection_range_))); } -} // namespace mhz19 -} // namespace esphome +} // namespace esphome::mhz19 diff --git a/esphome/components/mhz19/mhz19.h b/esphome/components/mhz19/mhz19.h index f7ceefd16d..a27f1c31eb 100644 --- a/esphome/components/mhz19/mhz19.h +++ b/esphome/components/mhz19/mhz19.h @@ -5,8 +5,7 @@ #include "esphome/components/sensor/sensor.h" #include "esphome/components/uart/uart.h" -namespace esphome { -namespace mhz19 { +namespace esphome::mhz19 { enum MHZ19ABCLogic { MHZ19_ABC_NONE = 0, @@ -74,5 +73,4 @@ template class MHZ19DetectionRangeSetAction : public Actionparent_->range_set(this->detection_range_.value(x...)); } }; -} // namespace mhz19 -} // namespace esphome +} // namespace esphome::mhz19