From e86f2e993f94954a6c96d55cb09a0004a46f32d0 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 4 Dec 2019 15:59:27 +0100 Subject: [PATCH] Pulse counter validate not both disabled (#902) --- esphome/components/pulse_counter/sensor.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index e73bc36036..61d3f3d5b5 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -38,16 +38,25 @@ def validate_pulse_counter_pin(value): return value +def validate_count_mode(value): + rising_edge = value[CONF_RISING_EDGE] + falling_edge = value[CONF_FALLING_EDGE] + if rising_edge == 'DISABLE' and falling_edge == 'DISABLE': + raise cv.Invalid("Can't set both count modes to DISABLE! This means no counting occurs at " + "all!") + return value + + CONFIG_SCHEMA = sensor.sensor_schema(UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2).extend({ cv.GenerateID(): cv.declare_id(PulseCounterSensor), cv.Required(CONF_PIN): validate_pulse_counter_pin, cv.Optional(CONF_COUNT_MODE, default={ CONF_RISING_EDGE: 'INCREMENT', CONF_FALLING_EDGE: 'DISABLE', - }): cv.Schema({ + }): cv.All(cv.Schema({ cv.Required(CONF_RISING_EDGE): COUNT_MODE_SCHEMA, cv.Required(CONF_FALLING_EDGE): COUNT_MODE_SCHEMA, - }), + }), validate_count_mode), cv.Optional(CONF_INTERNAL_FILTER, default='13us'): validate_internal_filter, }).extend(cv.polling_component_schema('60s'))