2021-07-28 10:41:21 +02:00
|
|
|
import esphome.codegen as cg
|
|
|
|
import esphome.config_validation as cv
|
|
|
|
from esphome.components import sensor, uart
|
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID,
|
|
|
|
CONF_PM_2_5,
|
2021-09-08 23:35:00 +02:00
|
|
|
CONF_UPDATE_INTERVAL,
|
2021-09-07 00:12:26 +02:00
|
|
|
DEVICE_CLASS_PM25,
|
2021-07-28 10:41:21 +02:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
UNIT_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
ICON_BLUR,
|
|
|
|
)
|
2021-09-08 23:35:00 +02:00
|
|
|
from esphome.core import TimePeriodMilliseconds
|
2021-07-28 10:41:21 +02:00
|
|
|
|
2021-09-08 23:35:00 +02:00
|
|
|
CODEOWNERS = ["@habbie"]
|
2021-07-28 10:41:21 +02:00
|
|
|
DEPENDENCIES = ["uart"]
|
|
|
|
|
|
|
|
pm1006_ns = cg.esphome_ns.namespace("pm1006")
|
2022-02-17 21:47:31 +01:00
|
|
|
PM1006Component = pm1006_ns.class_(
|
|
|
|
"PM1006Component", uart.UARTDevice, cg.PollingComponent
|
|
|
|
)
|
2021-07-28 10:41:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = cv.All(
|
|
|
|
cv.Schema(
|
|
|
|
{
|
|
|
|
cv.GenerateID(): cv.declare_id(PM1006Component),
|
|
|
|
cv.Optional(CONF_PM_2_5): sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
icon=ICON_BLUR,
|
|
|
|
accuracy_decimals=0,
|
2021-09-07 00:12:26 +02:00
|
|
|
device_class=DEVICE_CLASS_PM25,
|
2021-08-01 12:21:32 +02:00
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-07-28 10:41:21 +02:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.extend(cv.COMPONENT_SCHEMA)
|
2021-09-08 23:35:00 +02:00
|
|
|
.extend(uart.UART_DEVICE_SCHEMA)
|
|
|
|
.extend(cv.polling_component_schema("never")),
|
2021-07-28 10:41:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-08 23:35:00 +02:00
|
|
|
def validate_interval_uart(config):
|
|
|
|
require_tx = False
|
|
|
|
|
|
|
|
interval = config.get(CONF_UPDATE_INTERVAL)
|
|
|
|
|
|
|
|
if isinstance(interval, TimePeriodMilliseconds):
|
|
|
|
# 'never' is encoded as a very large int, not as a TimePeriodMilliseconds objects
|
|
|
|
require_tx = True
|
|
|
|
|
|
|
|
uart.final_validate_device_schema(
|
|
|
|
"pm1006", baud_rate=9600, require_rx=True, require_tx=require_tx
|
|
|
|
)(config)
|
|
|
|
|
|
|
|
|
|
|
|
FINAL_VALIDATE_SCHEMA = validate_interval_uart
|
|
|
|
|
|
|
|
|
2021-07-28 10:41:21 +02:00
|
|
|
async def to_code(config):
|
|
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
|
|
await cg.register_component(var, config)
|
|
|
|
await uart.register_uart_device(var, config)
|
|
|
|
|
|
|
|
if CONF_PM_2_5 in config:
|
|
|
|
sens = await sensor.new_sensor(config[CONF_PM_2_5])
|
|
|
|
cg.add(var.set_pm_2_5_sensor(sens))
|