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,
|
2021-09-07 00:12:26 +02:00
|
|
|
DEVICE_CLASS_CARBON_DIOXIDE,
|
2021-03-07 20:03:16 +01:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2021-06-03 03:49:56 +02:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
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),
|
2021-09-20 11:47:51 +02:00
|
|
|
cv.Required(CONF_CLOCK_PIN): cv.All(pins.internal_gpio_input_pin_schema),
|
|
|
|
cv.Required(CONF_DATA_PIN): cv.All(pins.internal_gpio_input_pin_schema),
|
2021-03-07 20:03:16 +01:00
|
|
|
cv.Optional(CONF_CO2): 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.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_CELSIUS,
|
|
|
|
accuracy_decimals=1,
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
),
|
|
|
|
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_PERCENT,
|
|
|
|
accuracy_decimals=1,
|
|
|
|
device_class=DEVICE_CLASS_HUMIDITY,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-03-07 20:03:16 +01:00
|
|
|
),
|
|
|
|
}
|
|
|
|
).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))
|