diff --git a/esphome/components/sgp4x/sgp4x.cpp b/esphome/components/sgp4x/sgp4x.cpp index 9c91c50ae7..bd84ae97f3 100644 --- a/esphome/components/sgp4x/sgp4x.cpp +++ b/esphome/components/sgp4x/sgp4x.cpp @@ -14,29 +14,29 @@ void SGP4xComponent::setup() { // Serial Number identification uint16_t raw_serial_number[3]; 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->mark_failed(); return; } this->serial_number_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) | (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 - uint16_t raw_featureset; - if (!this->get_register(SGP4X_CMD_GET_FEATURESET, raw_featureset, 1)) { - ESP_LOGD(TAG, "raw_featureset write_command_ failed"); + uint16_t featureset; + if (!this->get_register(SGP4X_CMD_GET_FEATURESET, featureset, 1)) { + ESP_LOGD(TAG, "Get feature set failed"); this->mark_failed(); return; } - this->featureset_ = raw_featureset; - if ((this->featureset_ & 0x1FF) == SGP40_FEATURESET) { - sgp_type_ = SGP40; - self_test_time_ = SPG40_SELFTEST_TIME; - measure_time_ = SGP40_MEASURE_TIME; + featureset &= 0x1FF; + if (featureset == SGP40_FEATURESET) { + this->sgp_type_ = SGP40; + this->self_test_time_ = SPG40_SELFTEST_TIME; + this->measure_time_ = SGP40_MEASURE_TIME; 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 this->nox_sensor_->set_disabled_by_default(true); // make sure it's not visible in HA @@ -45,20 +45,17 @@ void SGP4xComponent::setup() { // remove pointer to sensor 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 { - if ((this->featureset_ & 0x1FF) == SGP41_FEATURESET) { - sgp_type_ = SGP41; - self_test_time_ = SPG41_SELFTEST_TIME; - 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, "Unknown feature set 0x%0X", 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_) { // Hash with compilation time and serial number @@ -70,7 +67,7 @@ void SGP4xComponent::setup() { if (this->pref_.load(&this->voc_baselines_storage_)) { this->voc_state0_ = this->voc_baselines_storage_.state0; 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); } @@ -78,7 +75,7 @@ void SGP4xComponent::setup() { this->seconds_since_last_store_ = 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); 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 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(); }); } void SGP4xComponent::self_test_() { - ESP_LOGD(TAG, "Self-test started"); + ESP_LOGD(TAG, "Starting self-test"); if (!this->write_command(SGP4X_CMD_SELF_TEST)) { this->error_code_ = COMMUNICATION_FAILED; - ESP_LOGD(TAG, "Self-test communication failed"); + ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL); this->mark_failed(); } - this->set_timeout(self_test_time_, [this]() { - uint16_t reply; - if (!this->read_data(reply)) { + this->set_timeout(this->self_test_time_, [this]() { + uint16_t reply = 0; + if (!this->read_data(reply) || (reply != 0xD400)) { 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(); return; } - if (reply == 0xD400) { - this->self_test_complete_ = true; - 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(); + this->self_test_complete_ = true; + ESP_LOGD(TAG, "Self-test complete"); }); } @@ -150,7 +137,7 @@ void SGP4xComponent::update_gas_indices_() { this->voc_index_ = this->voc_algorithm_.process(this->voc_sraw_); if (this->nox_sensor_ != nullptr) 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 // much 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_; 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); } else { - ESP_LOGW(TAG, "Could not store VOC baselines"); + ESP_LOGW(TAG, "Storing VOC baselines failed"); } } } if (this->samples_read_ < this->samples_to_stabilize_) { this->samples_read_++; - ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_, - this->samples_to_stabilize_, this->voc_index_); + ESP_LOGD(TAG, "Stabilizing (%d/%d); VOC index: %" PRIu32, this->samples_read_, this->samples_to_stabilize_, + this->voc_index_); } } @@ -182,7 +169,7 @@ void SGP4xComponent::measure_raw_() { static uint32_t nox_conditioning_start = millis(); if (!this->self_test_complete_) { - ESP_LOGD(TAG, "Self-test not yet complete"); + ESP_LOGW(TAG, "Self-test incomplete"); return; } if (this->humidity_sensor_ != nullptr) { @@ -270,7 +257,7 @@ void SGP4xComponent::update() { void SGP4xComponent::dump_config() { ESP_LOGCONFIG(TAG, "SGP4x:"); 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()) { switch (this->error_code_) { @@ -278,14 +265,13 @@ void SGP4xComponent::dump_config() { ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL); break; case SERIAL_NUMBER_IDENTIFICATION_FAILED: - ESP_LOGW(TAG, "Get Serial number failed"); + ESP_LOGW(TAG, "Get serial number failed"); break; case SELF_TEST_FAILED: - ESP_LOGW(TAG, "Self test failed"); + ESP_LOGW(TAG, "Self-test failed"); break; - default: - ESP_LOGW(TAG, "Unknown setup error"); + ESP_LOGW(TAG, "Unknown error"); break; } } else { @@ -297,12 +283,12 @@ void SGP4xComponent::dump_config() { } LOG_UPDATE_INTERVAL(this); + ESP_LOGCONFIG(TAG, " Compensation:"); if (this->humidity_sensor_ != nullptr || this->temperature_sensor_ != nullptr) { - ESP_LOGCONFIG(TAG, " Compensation:"); LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_); LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_); } else { - ESP_LOGCONFIG(TAG, " Compensation: No source configured"); + ESP_LOGCONFIG(TAG, " No source configured"); } LOG_SENSOR(" ", "VOC", this->voc_sensor_); LOG_SENSOR(" ", "NOx", this->nox_sensor_); diff --git a/esphome/components/sgp4x/sgp4x.h b/esphome/components/sgp4x/sgp4x.h index 959ff12c27..45ee66af68 100644 --- a/esphome/components/sgp4x/sgp4x.h +++ b/esphome/components/sgp4x/sgp4x.h @@ -115,7 +115,6 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se SgpType sgp_type_{SGP40}; uint64_t serial_number_; - uint16_t featureset_; bool self_test_complete_; uint16_t self_test_time_;