Skip to content

Commit

Permalink
Add a sensor of current value of the Baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed Dec 28, 2020
1 parent ac15ce5 commit 2c5cf97
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions esphome/components/ccs811/ccs811.cpp
Expand Up @@ -80,6 +80,8 @@ void CCS811Component::update() {
this->co2_->publish_state(co2);
if (this->tvoc_ != nullptr)
this->tvoc_->publish_state(tvoc);
if (this->cur_baseline_ != nullptr)
this->cur_baseline_->publish_state(baseline);

this->status_clear_warning();

Expand Down Expand Up @@ -113,6 +115,7 @@ void CCS811Component::dump_config() {
LOG_UPDATE_INTERVAL(this)
LOG_SENSOR(" ", "CO2 Sensor", this->co2_)
LOG_SENSOR(" ", "TVOC Sensor", this->tvoc_)
LOG_SENSOR(" ", "Current Baseline", this->cur_baseline_)
if (this->baseline_) {
ESP_LOGCONFIG(TAG, " Baseline: %04X", *this->baseline_);
} else {
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/ccs811/ccs811.h
Expand Up @@ -13,6 +13,7 @@ class CCS811Component : public PollingComponent, public i2c::I2CDevice {
void set_co2(sensor::Sensor *co2) { co2_ = co2; }
void set_tvoc(sensor::Sensor *tvoc) { tvoc_ = tvoc; }
void set_baseline(uint16_t baseline) { baseline_ = baseline; }
void set_cur_baseline(sensor::Sensor *cur_baseline) { cur_baseline_ = cur_baseline; }
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }

Expand Down Expand Up @@ -43,6 +44,7 @@ class CCS811Component : public PollingComponent, public i2c::I2CDevice {

sensor::Sensor *co2_{nullptr};
sensor::Sensor *tvoc_{nullptr};
sensor::Sensor *cur_baseline_{nullptr};
optional<uint16_t> baseline_{};
/// Input sensor for humidity reading.
sensor::Sensor *humidity_{nullptr};
Expand Down
6 changes: 5 additions & 1 deletion esphome/components/ccs811/sensor.py
Expand Up @@ -2,7 +2,7 @@
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import CONF_ID, ICON_RADIATOR, UNIT_PARTS_PER_MILLION, \
UNIT_PARTS_PER_BILLION, CONF_TEMPERATURE, CONF_HUMIDITY, ICON_MOLECULE_CO2
UNIT_PARTS_PER_BILLION, CONF_TEMPERATURE, CONF_HUMIDITY, ICON_MOLECULE_CO2, ICON_SCALE

DEPENDENCIES = ['i2c']

Expand All @@ -12,12 +12,14 @@
CONF_ECO2 = 'eco2'
CONF_TVOC = 'tvoc'
CONF_BASELINE = 'baseline'
CONF_CUR_BASELINE = 'cur_baseline'

CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(CCS811Component),
cv.Required(CONF_ECO2): sensor.sensor_schema(UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2,
0),
cv.Required(CONF_TVOC): sensor.sensor_schema(UNIT_PARTS_PER_BILLION, ICON_RADIATOR, 0),
cv.Required(CONF_CUR_BASELINE): sensor.sensor_schema('', ICON_SCALE, 0),

cv.Optional(CONF_BASELINE): cv.hex_uint16_t,
cv.Optional(CONF_TEMPERATURE): cv.use_id(sensor.Sensor),
Expand All @@ -34,6 +36,8 @@ def to_code(config):
cg.add(var.set_co2(sens))
sens = yield sensor.new_sensor(config[CONF_TVOC])
cg.add(var.set_tvoc(sens))
sens = yield sensor.new_sensor(config[CONF_CUR_BASELINE])
cg.add(var.set_cur_baseline(sens))

if CONF_BASELINE in config:
cg.add(var.set_baseline(config[CONF_BASELINE]))
Expand Down

0 comments on commit 2c5cf97

Please sign in to comment.