Compare commits

...
4 changed files with 119 additions and 5 deletions

No files matched your search

+18 -5
View File
@@ -2,6 +2,8 @@
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include <cmath>
namespace esphome::scd4x {
ESPHOME_LOG_TAG(TAG, "scd4x");
@@ -24,6 +26,9 @@ static const uint16_t SCD4X_CMD_STOP_MEASUREMENTS = 0x3f86;
static const uint16_t SCD4X_CMD_FACTORY_RESET = 0x3632;
static const uint16_t SCD4X_CMD_GET_FEATURESET = 0x202f;
static const float SCD4X_TEMPERATURE_OFFSET_MULTIPLIER = (1 << 16) / 175.0f;
// Accepted ambient pressure range, the same as the schema's ambient_pressure_compensation
static constexpr float AMBIENT_PRESSURE_MIN_HPA = 700.0f;
static constexpr float AMBIENT_PRESSURE_MAX_HPA = 1200.0f;
void SCD4XComponent::setup() {
// the sensor needs 1000 ms to enter the idle state
@@ -132,10 +137,7 @@ void SCD4XComponent::update() {
}
if (this->ambient_pressure_source_ != nullptr) {
float pressure = this->ambient_pressure_source_->state;
if (!std::isnan(pressure)) {
this->set_ambient_pressure_compensation(pressure);
}
this->set_ambient_pressure_compensation(this->ambient_pressure_source_->state);
}
uint32_t wait_time = 0;
@@ -236,7 +238,18 @@ bool SCD4XComponent::factory_reset() {
}
void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) {
uint16_t new_ambient_pressure = static_cast<uint16_t>(pressure_in_hpa);
// Range-check before narrowing so NaN or out-of-unit values (e.g. Pa) can't wrap into range; 0 means unset
const bool is_nan = std::isnan(pressure_in_hpa);
if (pressure_in_hpa != 0.0f &&
(is_nan || pressure_in_hpa < AMBIENT_PRESSURE_MIN_HPA || pressure_in_hpa > AMBIENT_PRESSURE_MAX_HPA)) {
if (!is_nan && !this->pressure_range_warned_) {
ESP_LOGW(TAG, "Ambient pressure %.0f outside 700-1200 hPa", pressure_in_hpa);
this->pressure_range_warned_ = true;
}
return;
}
this->pressure_range_warned_ = false;
const auto new_ambient_pressure = static_cast<uint16_t>(lroundf(pressure_in_hpa));
if (!this->initialized_) {
this->ambient_pressure_ = new_ambient_pressure;
return;
+1
View File
@@ -44,6 +44,7 @@ class SCD4XComponent final : public PollingComponent, public sensirion_common::S
uint16_t ambient_pressure_{0}; // Per datasheet, valid values are 700 to 1200 hPa; 0 is a valid sentinel value
bool initialized_{false};
bool enable_asc_{false};
bool pressure_range_warned_{false};
MeasurementMode measurement_mode_{PERIODIC};
};
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <vector>
#include "esphome/components/i2c/i2c_bus.h"
#include "esphome/core/helpers.h"
namespace esphome::scd4x::testing {
// Records every write and answers every read with zero words carrying a valid CRC
class SensirionBus final : public i2c::I2CBus {
public:
std::vector<std::vector<uint8_t>> writes;
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *out, size_t out_size, uint8_t *in,
size_t in_size) override {
if (out_size > 0)
this->writes.emplace_back(out, out + out_size);
static constexpr uint8_t ZERO_WORD[2]{};
for (size_t i = 0; i < in_size; i++)
in[i] = i % 3 == 2 ? crc8(ZERO_WORD, 2, 0xFF, 0x31, true) : 0;
return i2c::ERROR_OK;
}
// Data words of every write of `command`
std::vector<uint16_t> words_for(uint16_t command) const {
std::vector<uint16_t> words;
for (const auto &w : this->writes) {
if (w.size() >= 5 && encode_uint16(w[0], w[1]) == command)
words.push_back(encode_uint16(w[2], w[3]));
}
return words;
}
};
} // namespace esphome::scd4x::testing
@@ -0,0 +1,64 @@
#include <gtest/gtest.h>
#include <cmath>
#include "../common.h"
#include "esphome/components/scd4x/scd4x.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
namespace esphome::scd4x::testing {
static constexpr uint16_t AMBIENT_PRESSURE_COMMAND = 0xe000;
// One test: the scheduler outlives a fixture, so a second component would see the first one's timeouts
TEST(SCD4XPressureSource, OnlyInRangeReadingsAreRoundedAndWritten) {
SensirionBus bus;
sensor::Sensor pressure;
SCD4XComponent scd;
scd.set_i2c_bus(&bus);
scd.set_i2c_address(0x62);
scd.set_ambient_pressure_source(&pressure);
scd.set_temperature_offset(4.0f);
// setup() waits 1000 ms, then 500 ms, before the sensor is initialized
scd.setup();
for (uint32_t wait : {1010, 510}) {
delay(wait);
App.scheduler.call(millis());
}
auto update_with = [&](float value) {
bus.writes.clear();
pressure.publish_state(value);
scd.update();
return bus.words_for(AMBIENT_PRESSURE_COMMAND);
};
EXPECT_EQ(update_with(1013.6f), std::vector<uint16_t>{1014});
EXPECT_TRUE(update_with(101325.0f).empty());
EXPECT_TRUE(update_with(500.0f).empty());
EXPECT_TRUE(update_with(NAN).empty());
EXPECT_EQ(update_with(700.0f), std::vector<uint16_t>{700});
EXPECT_EQ(update_with(1200.0f), std::vector<uint16_t>{1200});
// The range check runs before rounding, so values just outside are rejected rather than rounded in
EXPECT_TRUE(update_with(699.6f).empty());
EXPECT_TRUE(update_with(1200.4f).empty());
// A lambda calling the setter directly gets the same check
auto set_directly = [&](float value) {
bus.writes.clear();
scd.set_ambient_pressure_compensation(value);
return bus.words_for(AMBIENT_PRESSURE_COMMAND);
};
EXPECT_TRUE(set_directly(101325.0f).empty());
EXPECT_TRUE(set_directly(NAN).empty());
EXPECT_TRUE(set_directly(-5.0f).empty());
EXPECT_EQ(set_directly(1013.0f), std::vector<uint16_t>{1013});
// Run the reads each update() queued while `scd` is still alive
bus.writes.clear();
App.scheduler.call(millis());
EXPECT_FALSE(bus.writes.empty());
}
} // namespace esphome::scd4x::testing