13#define CHECK_TRUE(f, message) \
15 this->mark_failed(LOG_STR(message)); \
19#define CHECKED_IO(f) CHECK_TRUE(f, ESP_LOG_MSG_COMM_FAIL)
21void CCS811Component::setup() {
25 CHECK_TRUE(hw_id == 0x81,
"Sensor reported an invalid ID. Is this a CCS811?")
34 CHECK_TRUE(this->
write_bytes(0xF4, {}),
"Sensor reported APP start failed")
39 uint8_t meas_mode = 0;
41 if (interval >= 60 * 1000) {
43 }
else if (interval >= 10 * 1000) {
45 }
else if (interval >= 1 * 1000) {
62 uint8_t hardware_version = 0;
63 uint16_t bootloader_version = 0;
64 uint16_t application_version = 0;
66 if (hardware_version_data.has_value()) {
67 hardware_version = (*hardware_version_data)[0];
70 if (bootloader_version_data.has_value()) {
71 bootloader_version =
encode_uint16((*bootloader_version_data)[0], (*bootloader_version_data)[1]);
74 if (application_version_data.has_value()) {
75 application_version =
encode_uint16((*application_version_data)[0], (*application_version_data)[1]);
78 ESP_LOGD(TAG,
"hardware_version=0x%x bootloader_version=0x%x application_version=0x%x\n", hardware_version,
79 bootloader_version, application_version);
80 if (this->version_text_sensor_ !=
nullptr) {
82 buf_append_printf(version,
sizeof(version), 0,
"%d.%d.%d (0x%02x)", (application_version >> 12 & 15),
83 (application_version >> 8 & 15), (application_version >> 4 & 15), application_version);
84 ESP_LOGD(TAG,
"publishing version state: %s", version);
85 this->version_text_sensor_->publish_state(version);
88void CCS811Component::update() {
90 ESP_LOGD(TAG,
"Status indicates no data ready!");
97 if (!alg_data.has_value()) {
98 ESP_LOGW(TAG,
"Reading CCS811 data failed!");
102 auto res = *alg_data;
108 uint16_t baseline = 0;
109 if (baseline_data.has_value()) {
110 baseline =
encode_uint16((*baseline_data)[0], (*baseline_data)[1]);
113 ESP_LOGD(TAG,
"Got co2=%u ppm, tvoc=%u ppb, baseline=0x%04X", co2, tvoc, baseline);
115 PUBLISH_STATE(this->co2_sensor_, co2);
116 PUBLISH_STATE(this->tvoc_sensor_, tvoc);
123 if (this->humidity_sensor_ ==
nullptr && this->temperature_sensor_ ==
nullptr)
126 float humidity = NAN;
127 if (this->humidity_sensor_ !=
nullptr)
128 humidity = this->humidity_sensor_->state;
129 if (std::isnan(humidity) || humidity < 0 || humidity > 100)
132 if (this->temperature_sensor_ !=
nullptr)
144 uint16_t hum_conv =
static_cast<uint16_t
>(lroundf(humidity * 512.0f));
145 uint16_t temp_conv =
static_cast<uint16_t
>(lroundf(
temperature * 512.0f));
146 this->
write_bytes(0x05, {(uint8_t) ((hum_conv >> 8) & 0xff), (uint8_t) ((hum_conv & 0xff)),
147 (uint8_t) ((temp_conv >> 8) & 0xff), (uint8_t) ((temp_conv & 0xff))});
149void CCS811Component::dump_config() {
150 ESP_LOGCONFIG(TAG,
"CCS811");
152 LOG_UPDATE_INTERVAL(this);
153 LOG_SENSOR(" ", "CO2 Sensor", this->co2_sensor_);
154 LOG_SENSOR(" ", "TVOC Sensor", this->tvoc_sensor_);
155 LOG_TEXT_SENSOR(" ", "Firmware Version Sensor", this->version_text_sensor_);
157 ESP_LOGCONFIG(TAG,
" Baseline: %04X", *this->baseline_);
159 ESP_LOGCONFIG(TAG,
" Baseline: NOT SET");