From 7ebfcd38074c0306fc850ab2434367ad339d997e Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 5 Mar 2019 13:51:53 +0100 Subject: [PATCH] Add for to binary sensor conditions (#471) --- esphome/components/binary_sensor/__init__.py | 8 +++++--- esphome/const.py | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 048e9503df..63016359e0 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -9,7 +9,7 @@ from esphome.const import CONF_DELAYED_OFF, CONF_DELAYED_ON, CONF_DEVICE_CLASS, CONF_HEARTBEAT, CONF_ID, CONF_INTERNAL, CONF_INVALID_COOLDOWN, CONF_INVERT, CONF_INVERTED, \ CONF_LAMBDA, CONF_MAX_LENGTH, CONF_MIN_LENGTH, CONF_MQTT_ID, CONF_ON_CLICK, \ CONF_ON_DOUBLE_CLICK, CONF_ON_MULTI_CLICK, CONF_ON_PRESS, CONF_ON_RELEASE, CONF_ON_STATE, \ - CONF_STATE, CONF_TIMING, CONF_TRIGGER_ID + CONF_STATE, CONF_TIMING, CONF_TRIGGER_ID, CONF_FOR from esphome.core import CORE from esphome.cpp_generator import Pvariable, StructInitializer, add, get_variable, process_lambda from esphome.cpp_types import App, Component, Nameable, Trigger, bool_, esphome_ns, optional @@ -302,6 +302,7 @@ BUILD_FLAGS = '-DUSE_BINARY_SENSOR' CONF_BINARY_SENSOR_IS_ON = 'binary_sensor.is_on' BINARY_SENSOR_IS_ON_CONDITION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.use_variable_id(BinarySensor), + vol.Optional(CONF_FOR): cv.positive_time_period_milliseconds, }) @@ -309,7 +310,7 @@ BINARY_SENSOR_IS_ON_CONDITION_SCHEMA = maybe_simple_id({ def binary_sensor_is_on_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None - rhs = var.make_binary_sensor_is_on_condition(template_arg) + rhs = var.make_binary_sensor_is_on_condition(template_arg, config.get(CONF_FOR)) type = BinarySensorCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) @@ -317,6 +318,7 @@ def binary_sensor_is_on_to_code(config, condition_id, template_arg, args): CONF_BINARY_SENSOR_IS_OFF = 'binary_sensor.is_off' BINARY_SENSOR_IS_OFF_CONDITION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.use_variable_id(BinarySensor), + vol.Optional(CONF_FOR): cv.positive_time_period_milliseconds, }) @@ -324,6 +326,6 @@ BINARY_SENSOR_IS_OFF_CONDITION_SCHEMA = maybe_simple_id({ def binary_sensor_is_off_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None - rhs = var.make_binary_sensor_is_off_condition(template_arg) + rhs = var.make_binary_sensor_is_off_condition(template_arg, config.get(CONF_FOR)) type = BinarySensorCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) diff --git a/esphome/const.py b/esphome/const.py index 79a122698c..6d955c650d 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -363,6 +363,7 @@ CONF_FORMALDEHYDE = 'formaldehyde' CONF_ON_TAG = 'on_tag' CONF_ARGS = 'args' CONF_FORMAT = 'format' +CONF_FOR = 'for' CONF_COLOR_CORRECT = 'color_correct' CONF_ON_JSON_MESSAGE = 'on_json_message' CONF_ACCELERATION = 'acceleration'