2019-07-03 16:33:18 +02:00
|
|
|
import esphome.codegen as cg
|
|
|
|
import esphome.config_validation as cv
|
|
|
|
from esphome import pins
|
|
|
|
from esphome.components import sensor
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID,
|
|
|
|
CONF_CLOCK_PIN,
|
|
|
|
CONF_DATA_PIN,
|
|
|
|
CONF_CO2,
|
|
|
|
CONF_TEMPERATURE,
|
|
|
|
CONF_HUMIDITY,
|
|
|
|
DEVICE_CLASS_EMPTY,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
ICON_EMPTY,
|
|
|
|
UNIT_PARTS_PER_MILLION,
|
|
|
|
UNIT_CELSIUS,
|
|
|
|
UNIT_PERCENT,
|
|
|
|
ICON_MOLECULE_CO2,
|
|
|
|
)
|
2019-07-03 16:33:18 +02:00
|
|
|
from esphome.cpp_helpers import gpio_pin_expression
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
zyaura_ns = cg.esphome_ns.namespace("zyaura")
|
|
|
|
ZyAuraSensor = zyaura_ns.class_("ZyAuraSensor", cg.PollingComponent)
|
2019-07-03 16:33:18 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
CONFIG_SCHEMA = cv.Schema(
|
|
|
|
{
|
|
|
|
cv.GenerateID(): cv.declare_id(ZyAuraSensor),
|
|
|
|
cv.Required(CONF_CLOCK_PIN): cv.All(
|
|
|
|
pins.internal_gpio_input_pin_schema, pins.validate_has_interrupt
|
|
|
|
),
|
|
|
|
cv.Required(CONF_DATA_PIN): cv.All(
|
|
|
|
pins.internal_gpio_input_pin_schema, pins.validate_has_interrupt
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_CO2): sensor.sensor_schema(
|
|
|
|
UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
|
|
|
UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
|
|
|
UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY
|
|
|
|
),
|
|
|
|
}
|
|
|
|
).extend(cv.polling_component_schema("60s"))
|
2019-07-03 16:33:18 +02:00
|
|
|
|
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
async def to_code(config):
|
2019-07-03 16:33:18 +02:00
|
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
2021-05-24 10:58:29 +02:00
|
|
|
await cg.register_component(var, config)
|
2019-07-03 16:33:18 +02:00
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
pin_clock = await gpio_pin_expression(config[CONF_CLOCK_PIN])
|
2019-07-03 16:33:18 +02:00
|
|
|
cg.add(var.set_pin_clock(pin_clock))
|
2021-05-24 10:58:29 +02:00
|
|
|
pin_data = await gpio_pin_expression(config[CONF_DATA_PIN])
|
2019-07-03 16:33:18 +02:00
|
|
|
cg.add(var.set_pin_data(pin_data))
|
|
|
|
|
|
|
|
if CONF_CO2 in config:
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await sensor.new_sensor(config[CONF_CO2])
|
2019-07-03 16:33:18 +02:00
|
|
|
cg.add(var.set_co2_sensor(sens))
|
|
|
|
if CONF_TEMPERATURE in config:
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
|
2019-07-03 16:33:18 +02:00
|
|
|
cg.add(var.set_temperature_sensor(sens))
|
|
|
|
if CONF_HUMIDITY in config:
|
2021-05-24 10:58:29 +02:00
|
|
|
sens = await sensor.new_sensor(config[CONF_HUMIDITY])
|
2019-07-03 16:33:18 +02:00
|
|
|
cg.add(var.set_humidity_sensor(sens))
|