[sgp4x] Shorten log messages, various clean-up (#9048)

This commit is contained in:
Keith Burzinski 2025-06-11 06:11:11 -05:00 committed by GitHub
parent c3c3a27af2
commit dcf41db878
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 57 deletions

View File

@ -14,29 +14,29 @@ void SGP4xComponent::setup() {
// Serial Number identification // Serial Number identification
uint16_t raw_serial_number[3]; uint16_t raw_serial_number[3];
if (!this->get_register(SGP4X_CMD_GET_SERIAL_ID, raw_serial_number, 3, 1)) { if (!this->get_register(SGP4X_CMD_GET_SERIAL_ID, raw_serial_number, 3, 1)) {
ESP_LOGE(TAG, "Failed to read serial number"); ESP_LOGE(TAG, "Get serial number failed");
this->error_code_ = SERIAL_NUMBER_IDENTIFICATION_FAILED; this->error_code_ = SERIAL_NUMBER_IDENTIFICATION_FAILED;
this->mark_failed(); this->mark_failed();
return; return;
} }
this->serial_number_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) | this->serial_number_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) |
(uint64_t(raw_serial_number[2])); (uint64_t(raw_serial_number[2]));
ESP_LOGD(TAG, "Serial Number: %" PRIu64, this->serial_number_); ESP_LOGD(TAG, "Serial number: %" PRIu64, this->serial_number_);
// Featureset identification for future use // Featureset identification for future use
uint16_t raw_featureset; uint16_t featureset;
if (!this->get_register(SGP4X_CMD_GET_FEATURESET, raw_featureset, 1)) { if (!this->get_register(SGP4X_CMD_GET_FEATURESET, featureset, 1)) {
ESP_LOGD(TAG, "raw_featureset write_command_ failed"); ESP_LOGD(TAG, "Get feature set failed");
this->mark_failed(); this->mark_failed();
return; return;
} }
this->featureset_ = raw_featureset; featureset &= 0x1FF;
if ((this->featureset_ & 0x1FF) == SGP40_FEATURESET) { if (featureset == SGP40_FEATURESET) {
sgp_type_ = SGP40; this->sgp_type_ = SGP40;
self_test_time_ = SPG40_SELFTEST_TIME; this->self_test_time_ = SPG40_SELFTEST_TIME;
measure_time_ = SGP40_MEASURE_TIME; this->measure_time_ = SGP40_MEASURE_TIME;
if (this->nox_sensor_) { if (this->nox_sensor_) {
ESP_LOGE(TAG, "Measuring NOx requires a SGP41 sensor but a SGP40 sensor is detected"); ESP_LOGE(TAG, "SGP41 required for NOx");
// disable the sensor // disable the sensor
this->nox_sensor_->set_disabled_by_default(true); this->nox_sensor_->set_disabled_by_default(true);
// make sure it's not visible in HA // make sure it's not visible in HA
@ -45,20 +45,17 @@ void SGP4xComponent::setup() {
// remove pointer to sensor // remove pointer to sensor
this->nox_sensor_ = nullptr; this->nox_sensor_ = nullptr;
} }
} else if (featureset == SGP41_FEATURESET) {
this->sgp_type_ = SGP41;
this->self_test_time_ = SPG41_SELFTEST_TIME;
this->measure_time_ = SGP41_MEASURE_TIME;
} else { } else {
if ((this->featureset_ & 0x1FF) == SGP41_FEATURESET) { ESP_LOGD(TAG, "Unknown feature set 0x%0X", featureset);
sgp_type_ = SGP41; this->mark_failed();
self_test_time_ = SPG41_SELFTEST_TIME; return;
measure_time_ = SGP41_MEASURE_TIME;
} else {
ESP_LOGD(TAG, "Product feature set failed 0x%0X , expecting 0x%0X", uint16_t(this->featureset_ & 0x1FF),
SGP40_FEATURESET);
this->mark_failed();
return;
}
} }
ESP_LOGD(TAG, "Product version: 0x%0X", uint16_t(this->featureset_ & 0x1FF)); ESP_LOGD(TAG, "Version 0x%0X", featureset);
if (this->store_baseline_) { if (this->store_baseline_) {
// Hash with compilation time and serial number // Hash with compilation time and serial number
@ -70,7 +67,7 @@ void SGP4xComponent::setup() {
if (this->pref_.load(&this->voc_baselines_storage_)) { if (this->pref_.load(&this->voc_baselines_storage_)) {
this->voc_state0_ = this->voc_baselines_storage_.state0; this->voc_state0_ = this->voc_baselines_storage_.state0;
this->voc_state1_ = this->voc_baselines_storage_.state1; this->voc_state1_ = this->voc_baselines_storage_.state1;
ESP_LOGI(TAG, "Loaded VOC baseline state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32, ESP_LOGV(TAG, "Loaded VOC baseline state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32,
this->voc_baselines_storage_.state0, voc_baselines_storage_.state1); this->voc_baselines_storage_.state0, voc_baselines_storage_.state1);
} }
@ -78,7 +75,7 @@ void SGP4xComponent::setup() {
this->seconds_since_last_store_ = 0; this->seconds_since_last_store_ = 0;
if (this->voc_baselines_storage_.state0 > 0 && this->voc_baselines_storage_.state1 > 0) { if (this->voc_baselines_storage_.state0 > 0 && this->voc_baselines_storage_.state1 > 0) {
ESP_LOGI(TAG, "Setting VOC baseline from save state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32, ESP_LOGV(TAG, "Setting VOC baseline from save state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32,
this->voc_baselines_storage_.state0, voc_baselines_storage_.state1); this->voc_baselines_storage_.state0, voc_baselines_storage_.state1);
voc_algorithm_.set_states(this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1); voc_algorithm_.set_states(this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1);
} }
@ -110,39 +107,29 @@ void SGP4xComponent::setup() {
limit the amount of communication done over wifi for power consumption or to keep the limit the amount of communication done over wifi for power consumption or to keep the
number of records reported from being overwhelming. number of records reported from being overwhelming.
*/ */
ESP_LOGD(TAG, "Component requires sampling of 1Hz, setting up background sampler"); ESP_LOGV(TAG, "Component requires sampling of 1Hz, setting up background sampler");
this->set_interval(1000, [this]() { this->take_sample(); }); this->set_interval(1000, [this]() { this->take_sample(); });
} }
void SGP4xComponent::self_test_() { void SGP4xComponent::self_test_() {
ESP_LOGD(TAG, "Self-test started"); ESP_LOGD(TAG, "Starting self-test");
if (!this->write_command(SGP4X_CMD_SELF_TEST)) { if (!this->write_command(SGP4X_CMD_SELF_TEST)) {
this->error_code_ = COMMUNICATION_FAILED; this->error_code_ = COMMUNICATION_FAILED;
ESP_LOGD(TAG, "Self-test communication failed"); ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL);
this->mark_failed(); this->mark_failed();
} }
this->set_timeout(self_test_time_, [this]() { this->set_timeout(this->self_test_time_, [this]() {
uint16_t reply; uint16_t reply = 0;
if (!this->read_data(reply)) { if (!this->read_data(reply) || (reply != 0xD400)) {
this->error_code_ = SELF_TEST_FAILED; this->error_code_ = SELF_TEST_FAILED;
ESP_LOGD(TAG, "Self-test read_data_ failed"); ESP_LOGW(TAG, "Self-test failed (0x%X)", reply);
this->mark_failed(); this->mark_failed();
return; return;
} }
if (reply == 0xD400) { this->self_test_complete_ = true;
this->self_test_complete_ = true; ESP_LOGD(TAG, "Self-test complete");
ESP_LOGD(TAG, "Self-test completed");
return;
} else {
this->error_code_ = SELF_TEST_FAILED;
ESP_LOGD(TAG, "Self-test failed 0x%X", reply);
return;
}
ESP_LOGD(TAG, "Self-test failed 0x%X", reply);
this->mark_failed();
}); });
} }
@ -150,7 +137,7 @@ void SGP4xComponent::update_gas_indices_() {
this->voc_index_ = this->voc_algorithm_.process(this->voc_sraw_); this->voc_index_ = this->voc_algorithm_.process(this->voc_sraw_);
if (this->nox_sensor_ != nullptr) if (this->nox_sensor_ != nullptr)
this->nox_index_ = this->nox_algorithm_.process(this->nox_sraw_); this->nox_index_ = this->nox_algorithm_.process(this->nox_sraw_);
ESP_LOGV(TAG, "VOC = %" PRId32 ", NOx = %" PRId32, this->voc_index_, this->nox_index_); ESP_LOGV(TAG, "VOC: %" PRId32 ", NOx: %" PRId32, this->voc_index_, this->nox_index_);
// Store baselines after defined interval or if the difference between current and stored baseline becomes too // Store baselines after defined interval or if the difference between current and stored baseline becomes too
// much // much
if (this->store_baseline_ && this->seconds_since_last_store_ > SHORTEST_BASELINE_STORE_INTERVAL) { if (this->store_baseline_ && this->seconds_since_last_store_ > SHORTEST_BASELINE_STORE_INTERVAL) {
@ -162,18 +149,18 @@ void SGP4xComponent::update_gas_indices_() {
this->voc_baselines_storage_.state1 = this->voc_state1_; this->voc_baselines_storage_.state1 = this->voc_state1_;
if (this->pref_.save(&this->voc_baselines_storage_)) { if (this->pref_.save(&this->voc_baselines_storage_)) {
ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04" PRIX32 " ,state1: 0x%04" PRIX32, ESP_LOGV(TAG, "Stored VOC baseline state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32,
this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1); this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1);
} else { } else {
ESP_LOGW(TAG, "Could not store VOC baselines"); ESP_LOGW(TAG, "Storing VOC baselines failed");
} }
} }
} }
if (this->samples_read_ < this->samples_to_stabilize_) { if (this->samples_read_ < this->samples_to_stabilize_) {
this->samples_read_++; this->samples_read_++;
ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_, ESP_LOGD(TAG, "Stabilizing (%d/%d); VOC index: %" PRIu32, this->samples_read_, this->samples_to_stabilize_,
this->samples_to_stabilize_, this->voc_index_); this->voc_index_);
} }
} }
@ -182,7 +169,7 @@ void SGP4xComponent::measure_raw_() {
static uint32_t nox_conditioning_start = millis(); static uint32_t nox_conditioning_start = millis();
if (!this->self_test_complete_) { if (!this->self_test_complete_) {
ESP_LOGD(TAG, "Self-test not yet complete"); ESP_LOGW(TAG, "Self-test incomplete");
return; return;
} }
if (this->humidity_sensor_ != nullptr) { if (this->humidity_sensor_ != nullptr) {
@ -270,7 +257,7 @@ void SGP4xComponent::update() {
void SGP4xComponent::dump_config() { void SGP4xComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SGP4x:"); ESP_LOGCONFIG(TAG, "SGP4x:");
LOG_I2C_DEVICE(this); LOG_I2C_DEVICE(this);
ESP_LOGCONFIG(TAG, " store_baseline: %d", this->store_baseline_); ESP_LOGCONFIG(TAG, " Store baseline: %s", YESNO(this->store_baseline_));
if (this->is_failed()) { if (this->is_failed()) {
switch (this->error_code_) { switch (this->error_code_) {
@ -278,14 +265,13 @@ void SGP4xComponent::dump_config() {
ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL); ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
break; break;
case SERIAL_NUMBER_IDENTIFICATION_FAILED: case SERIAL_NUMBER_IDENTIFICATION_FAILED:
ESP_LOGW(TAG, "Get Serial number failed"); ESP_LOGW(TAG, "Get serial number failed");
break; break;
case SELF_TEST_FAILED: case SELF_TEST_FAILED:
ESP_LOGW(TAG, "Self test failed"); ESP_LOGW(TAG, "Self-test failed");
break; break;
default: default:
ESP_LOGW(TAG, "Unknown setup error"); ESP_LOGW(TAG, "Unknown error");
break; break;
} }
} else { } else {
@ -297,12 +283,12 @@ void SGP4xComponent::dump_config() {
} }
LOG_UPDATE_INTERVAL(this); LOG_UPDATE_INTERVAL(this);
ESP_LOGCONFIG(TAG, " Compensation:");
if (this->humidity_sensor_ != nullptr || this->temperature_sensor_ != nullptr) { if (this->humidity_sensor_ != nullptr || this->temperature_sensor_ != nullptr) {
ESP_LOGCONFIG(TAG, " Compensation:");
LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_); LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_);
LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_); LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_);
} else { } else {
ESP_LOGCONFIG(TAG, " Compensation: No source configured"); ESP_LOGCONFIG(TAG, " No source configured");
} }
LOG_SENSOR(" ", "VOC", this->voc_sensor_); LOG_SENSOR(" ", "VOC", this->voc_sensor_);
LOG_SENSOR(" ", "NOx", this->nox_sensor_); LOG_SENSOR(" ", "NOx", this->nox_sensor_);

View File

@ -115,7 +115,6 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se
SgpType sgp_type_{SGP40}; SgpType sgp_type_{SGP40};
uint64_t serial_number_; uint64_t serial_number_;
uint16_t featureset_;
bool self_test_complete_; bool self_test_complete_;
uint16_t self_test_time_; uint16_t self_test_time_;