esphome/esphomeyaml/components/sensor/pulse_counter.py

64 lines
2.4 KiB
Python
Raw Normal View History

2018-04-07 01:23:03 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
from esphomeyaml import pins
from esphomeyaml.components import sensor
2018-05-20 12:41:52 +02:00
from esphomeyaml.const import CONF_COUNT_MODE, CONF_FALLING_EDGE, CONF_INTERNAL_FILTER, \
CONF_MAKE_ID, CONF_NAME, CONF_PIN, CONF_PULL_MODE, CONF_RISING_EDGE, CONF_UPDATE_INTERVAL, \
2018-04-07 01:23:03 +02:00
ESP_PLATFORM_ESP32
2018-05-20 12:41:52 +02:00
from esphomeyaml.helpers import App, add, global_ns, variable, Application
2018-04-07 01:23:03 +02:00
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
GPIO_PULL_MODES = {
2018-05-20 12:41:52 +02:00
'PULLUP': global_ns.GPIO_PULLUP_ONLY,
'PULLDOWN': global_ns.GPIO_PULLDOWN_ONLY,
'PULLUP_PULLDOWN': global_ns.GPIO_PULLUP_PULLDOWN,
'FLOATING': global_ns.GPIO_FLOATING,
2018-04-07 01:23:03 +02:00
}
2018-05-20 12:41:52 +02:00
GPIO_PULL_MODE_SCHEMA = vol.All(vol.Upper, cv.one_of(*GPIO_PULL_MODES))
2018-04-07 01:23:03 +02:00
COUNT_MODES = {
2018-05-20 12:41:52 +02:00
'DISABLE': global_ns.PCNT_COUNT_DIS,
'INCREMENT': global_ns.PCNT_COUNT_INC,
'DECREMENT': global_ns.PCNT_COUNT_DEC,
2018-04-07 01:23:03 +02:00
}
2018-05-21 20:55:39 +02:00
COUNT_MODE_SCHEMA = vol.All(vol.Upper, cv.one_of(*COUNT_MODES))
2018-04-07 01:23:03 +02:00
PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
2018-05-20 12:41:52 +02:00
cv.GenerateID('pulse_counter', CONF_MAKE_ID): cv.register_variable_id,
2018-04-07 01:23:03 +02:00
vol.Required(CONF_PIN): pins.input_pin,
vol.Optional(CONF_PULL_MODE): GPIO_PULL_MODE_SCHEMA,
vol.Optional(CONF_COUNT_MODE): vol.Schema({
vol.Required(CONF_RISING_EDGE): COUNT_MODE_SCHEMA,
vol.Required(CONF_FALLING_EDGE): COUNT_MODE_SCHEMA,
}),
vol.Optional(CONF_INTERNAL_FILTER): vol.All(vol.Coerce(int), vol.Range(min=0, max=1023)),
2018-05-14 11:50:56 +02:00
vol.Optional(CONF_UPDATE_INTERVAL): cv.positive_time_period_milliseconds,
2018-05-20 12:41:52 +02:00
}).extend(sensor.SENSOR_SCHEMA.schema)
MakePulseCounterSensor = Application.MakePulseCounterSensor
2018-04-07 01:23:03 +02:00
def to_code(config):
rhs = App.make_pulse_counter_sensor(config[CONF_NAME], config[CONF_PIN],
2018-04-07 01:23:03 +02:00
config.get(CONF_UPDATE_INTERVAL))
2018-05-20 12:41:52 +02:00
make = variable(MakePulseCounterSensor, config[CONF_MAKE_ID], rhs)
2018-04-07 01:23:03 +02:00
pcnt = make.Ppcnt
if CONF_PULL_MODE in config:
pull_mode = GPIO_PULL_MODES[config[CONF_PULL_MODE]]
2018-05-20 12:41:52 +02:00
add(pcnt.set_pull_mode(pull_mode))
2018-04-07 01:23:03 +02:00
if CONF_COUNT_MODE in config:
count_mode = config[CONF_COUNT_MODE]
rising_edge = COUNT_MODES[count_mode[CONF_RISING_EDGE]]
falling_edge = COUNT_MODES[count_mode[CONF_FALLING_EDGE]]
2018-05-20 12:41:52 +02:00
add(pcnt.set_edge_mode(rising_edge, falling_edge))
2018-04-07 01:23:03 +02:00
if CONF_INTERNAL_FILTER in config:
add(pcnt.set_filter(config[CONF_INTERNAL_FILTER]))
2018-05-20 12:41:52 +02:00
sensor.setup_sensor(make.Ppcnt, make.Pmqtt, config)
2018-05-06 15:56:12 +02:00
BUILD_FLAGS = '-DUSE_PULSE_COUNTER_SENSOR'