2019-04-17 12:06:00 +02:00
|
|
|
import esphome.codegen as cg
|
|
|
|
import esphome.config_validation as cv
|
|
|
|
from esphome import pins
|
|
|
|
from esphome.components import sensor
|
2021-06-03 03:49:56 +02:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID,
|
|
|
|
CONF_PIN,
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
UNIT_SECOND,
|
|
|
|
ICON_TIMER,
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
pulse_width_ns = cg.esphome_ns.namespace("pulse_width")
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
PulseWidthSensor = pulse_width_ns.class_(
|
|
|
|
"PulseWidthSensor", sensor.Sensor, cg.PollingComponent
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
CONFIG_SCHEMA = (
|
2021-06-03 03:49:56 +02:00
|
|
|
sensor.sensor_schema(
|
2021-08-01 12:21:32 +02:00
|
|
|
unit_of_measurement=UNIT_SECOND,
|
|
|
|
icon=ICON_TIMER,
|
|
|
|
accuracy_decimals=3,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
2021-06-03 03:49:56 +02:00
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
.extend(
|
|
|
|
{
|
|
|
|
cv.GenerateID(): cv.declare_id(PulseWidthSensor),
|
2021-09-20 11:47:51 +02:00
|
|
|
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema),
|
2021-03-07 20:03:16 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.extend(cv.polling_component_schema("60s"))
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
async def to_code(config):
|
2019-04-22 21:56:30 +02:00
|
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
2021-05-24 10:58:29 +02:00
|
|
|
await cg.register_component(var, config)
|
|
|
|
await sensor.register_sensor(var, config)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
2019-04-17 12:06:00 +02:00
|
|
|
cg.add(var.set_pin(pin))
|