2019-10-19 21:21:07 +02:00
|
|
|
import esphome.codegen as cg
|
|
|
|
import esphome.config_validation as cv
|
2022-04-13 00:19:48 +02:00
|
|
|
from esphome.components import i2c, sensor, sensirion_common
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID,
|
2021-07-12 23:21:54 +02:00
|
|
|
CONF_BASELINE,
|
|
|
|
CONF_ECO2,
|
2022-04-13 00:19:48 +02:00
|
|
|
CONF_STORE_BASELINE,
|
|
|
|
CONF_TEMPERATURE_SOURCE,
|
2021-07-12 23:21:54 +02:00
|
|
|
CONF_TVOC,
|
2021-03-07 20:03:16 +01:00
|
|
|
ICON_RADIATOR,
|
2021-09-07 00:12:26 +02:00
|
|
|
DEVICE_CLASS_CARBON_DIOXIDE,
|
|
|
|
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
2021-06-03 03:49:56 +02:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
UNIT_PARTS_PER_MILLION,
|
|
|
|
UNIT_PARTS_PER_BILLION,
|
|
|
|
ICON_MOLECULE_CO2,
|
2022-03-08 03:21:13 +01:00
|
|
|
ENTITY_CATEGORY_DIAGNOSTIC,
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
DEPENDENCIES = ["i2c"]
|
2022-04-13 00:19:48 +02:00
|
|
|
AUTO_LOAD = ["sensirion_common"]
|
2021-03-07 20:03:16 +01:00
|
|
|
|
|
|
|
sgp30_ns = cg.esphome_ns.namespace("sgp30")
|
2022-04-13 00:19:48 +02:00
|
|
|
SGP30Component = sgp30_ns.class_(
|
|
|
|
"SGP30Component", cg.PollingComponent, sensirion_common.SensirionI2CDevice
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
|
|
|
|
CONF_ECO2_BASELINE = "eco2_baseline"
|
|
|
|
CONF_TVOC_BASELINE = "tvoc_baseline"
|
|
|
|
CONF_UPTIME = "uptime"
|
|
|
|
CONF_COMPENSATION = "compensation"
|
|
|
|
CONF_HUMIDITY_SOURCE = "humidity_source"
|
2022-04-13 00:19:48 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = (
|
|
|
|
cv.Schema(
|
|
|
|
{
|
|
|
|
cv.GenerateID(): cv.declare_id(SGP30Component),
|
|
|
|
cv.Required(CONF_ECO2): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_PARTS_PER_MILLION,
|
|
|
|
icon=ICON_MOLECULE_CO2,
|
|
|
|
accuracy_decimals=0,
|
2021-09-07 00:12:26 +02:00
|
|
|
device_class=DEVICE_CLASS_CARBON_DIOXIDE,
|
2021-08-01 12:21:32 +02:00
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
),
|
|
|
|
cv.Required(CONF_TVOC): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_PARTS_PER_BILLION,
|
|
|
|
icon=ICON_RADIATOR,
|
|
|
|
accuracy_decimals=0,
|
2021-09-07 00:12:26 +02:00
|
|
|
device_class=DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
2021-08-01 12:21:32 +02:00
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
),
|
2021-07-12 23:21:54 +02:00
|
|
|
cv.Optional(CONF_ECO2_BASELINE): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
icon=ICON_MOLECULE_CO2,
|
|
|
|
accuracy_decimals=0,
|
2022-03-08 03:21:13 +01:00
|
|
|
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
2021-07-12 23:21:54 +02:00
|
|
|
),
|
|
|
|
cv.Optional(CONF_TVOC_BASELINE): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
icon=ICON_RADIATOR,
|
|
|
|
accuracy_decimals=0,
|
2022-03-08 03:21:13 +01:00
|
|
|
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
2021-07-12 23:21:54 +02:00
|
|
|
),
|
|
|
|
cv.Optional(CONF_STORE_BASELINE, default=True): cv.boolean,
|
2021-03-07 20:03:16 +01:00
|
|
|
cv.Optional(CONF_BASELINE): cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_ECO2_BASELINE): cv.hex_uint16_t,
|
|
|
|
cv.Required(CONF_TVOC_BASELINE): cv.hex_uint16_t,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_COMPENSATION): cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_HUMIDITY_SOURCE): cv.use_id(sensor.Sensor),
|
|
|
|
cv.Required(CONF_TEMPERATURE_SOURCE): cv.use_id(sensor.Sensor),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2021-07-12 23:21:54 +02:00
|
|
|
.extend(cv.polling_component_schema("1s"))
|
2021-03-07 20:03:16 +01:00
|
|
|
.extend(i2c.i2c_device_schema(0x58))
|
|
|
|
)
|
2019-10-19 21:21:07 +02:00
|
|
|
|
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
async def to_code(config):
|
2019-10-19 21:21:07 +02:00
|
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
2021-05-24 10:58:29 +02:00
|
|
|
await cg.register_component(var, config)
|
|
|
|
await i2c.register_i2c_device(var, config)
|
2019-10-19 21:21:07 +02:00
|
|
|
|
|
|
|
if CONF_ECO2 in config:
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await sensor.new_sensor(config[CONF_ECO2])
|
2019-10-19 21:21:07 +02:00
|
|
|
cg.add(var.set_eco2_sensor(sens))
|
|
|
|
|
|
|
|
if CONF_TVOC in config:
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await sensor.new_sensor(config[CONF_TVOC])
|
2019-10-19 21:21:07 +02:00
|
|
|
cg.add(var.set_tvoc_sensor(sens))
|
|
|
|
|
2021-07-12 23:21:54 +02:00
|
|
|
if CONF_ECO2_BASELINE in config:
|
|
|
|
sens = await sensor.new_sensor(config[CONF_ECO2_BASELINE])
|
|
|
|
cg.add(var.set_eco2_baseline_sensor(sens))
|
|
|
|
|
|
|
|
if CONF_TVOC_BASELINE in config:
|
|
|
|
sens = await sensor.new_sensor(config[CONF_TVOC_BASELINE])
|
|
|
|
cg.add(var.set_tvoc_baseline_sensor(sens))
|
|
|
|
|
|
|
|
if CONF_STORE_BASELINE in config:
|
|
|
|
cg.add(var.set_store_baseline(config[CONF_STORE_BASELINE]))
|
|
|
|
|
2019-10-19 21:21:07 +02:00
|
|
|
if CONF_BASELINE in config:
|
2020-01-12 16:42:18 +01:00
|
|
|
baseline_config = config[CONF_BASELINE]
|
|
|
|
cg.add(var.set_eco2_baseline(baseline_config[CONF_ECO2_BASELINE]))
|
|
|
|
cg.add(var.set_tvoc_baseline(baseline_config[CONF_TVOC_BASELINE]))
|
2019-10-19 21:21:07 +02:00
|
|
|
|
|
|
|
if CONF_COMPENSATION in config:
|
|
|
|
compensation_config = config[CONF_COMPENSATION]
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await cg.get_variable(compensation_config[CONF_HUMIDITY_SOURCE])
|
2019-10-19 21:21:07 +02:00
|
|
|
cg.add(var.set_humidity_sensor(sens))
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await cg.get_variable(compensation_config[CONF_TEMPERATURE_SOURCE])
|
2019-10-19 21:21:07 +02:00
|
|
|
cg.add(var.set_temperature_sensor(sens))
|