From 2fab7e73b91b8d4c965a4fe6aad5e19116d56d84 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 26 Oct 2018 22:59:03 +0200 Subject: [PATCH] Add send_first_at option to sliding window sensor filter (#207) * Add send_first_at option to sliding window sensor filter * Lint --- esphomeyaml/components/sensor/__init__.py | 20 ++++++++++++++++---- esphomeyaml/const.py | 1 + tests/test1.yaml | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/esphomeyaml/components/sensor/__init__.py b/esphomeyaml/components/sensor/__init__.py index 01732ea1c6..dc12106135 100644 --- a/esphomeyaml/components/sensor/__init__.py +++ b/esphomeyaml/components/sensor/__init__.py @@ -7,7 +7,8 @@ from esphomeyaml.const import CONF_ABOVE, CONF_ACCURACY_DECIMALS, CONF_ALPHA, CO CONF_FILTER_NAN, CONF_FILTER_OUT, CONF_HEARTBEAT, CONF_ICON, CONF_ID, CONF_INTERNAL, \ CONF_LAMBDA, CONF_MQTT_ID, CONF_MULTIPLY, CONF_OFFSET, CONF_ON_RAW_VALUE, CONF_ON_VALUE, \ CONF_ON_VALUE_RANGE, CONF_OR, CONF_SEND_EVERY, CONF_SLIDING_WINDOW_MOVING_AVERAGE, \ - CONF_THROTTLE, CONF_TRIGGER_ID, CONF_UNIQUE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE + CONF_THROTTLE, CONF_TRIGGER_ID, CONF_UNIQUE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE, \ + CONF_SEND_FIRST_AT from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, add, add_job, esphomelib_ns, \ float_, process_lambda, setup_mqtt_component, templatable @@ -20,6 +21,15 @@ def validate_recursive_filter(value): return FILTERS_SCHEMA(value) +def validate_send_first_at(value): + send_first_at = value.get(CONF_SEND_FIRST_AT) + send_every = value[CONF_SEND_EVERY] + if send_first_at is not None and send_first_at > send_every: + raise vol.Invalid("send_first_at must be smaller than or equal to send_every! {} <= {}" + "".format(send_first_at, send_every)) + return value + + FILTER_KEYS = [CONF_OFFSET, CONF_MULTIPLY, CONF_FILTER_OUT, CONF_FILTER_NAN, CONF_SLIDING_WINDOW_MOVING_AVERAGE, CONF_EXPONENTIAL_MOVING_AVERAGE, CONF_LAMBDA, CONF_THROTTLE, CONF_DELTA, CONF_UNIQUE, CONF_HEARTBEAT, CONF_DEBOUNCE, CONF_OR] @@ -29,10 +39,11 @@ FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.All({ vol.Optional(CONF_MULTIPLY): vol.Coerce(float), vol.Optional(CONF_FILTER_OUT): vol.Coerce(float), vol.Optional(CONF_FILTER_NAN): None, - vol.Optional(CONF_SLIDING_WINDOW_MOVING_AVERAGE): vol.Schema({ + vol.Optional(CONF_SLIDING_WINDOW_MOVING_AVERAGE): vol.All(vol.Schema({ vol.Required(CONF_WINDOW_SIZE): cv.positive_not_null_int, vol.Required(CONF_SEND_EVERY): cv.positive_not_null_int, - }), + vol.Optional(CONF_SEND_FIRST_AT): cv.positive_not_null_int, + }), validate_send_first_at), vol.Optional(CONF_EXPONENTIAL_MOVING_AVERAGE): vol.Schema({ vol.Required(CONF_ALPHA): cv.positive_float, vol.Required(CONF_SEND_EVERY): cv.positive_not_null_int, @@ -103,7 +114,8 @@ def setup_filter(config): yield FilterOutNANFilter.new() elif CONF_SLIDING_WINDOW_MOVING_AVERAGE in config: conf = config[CONF_SLIDING_WINDOW_MOVING_AVERAGE] - yield SlidingWindowMovingAverageFilter.new(conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY]) + yield SlidingWindowMovingAverageFilter.new(conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY], + conf.get(CONF_SEND_FIRST_AT)) elif CONF_EXPONENTIAL_MOVING_AVERAGE in config: conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE] yield ExponentialMovingAverageFilter.new(conf[CONF_ALPHA], conf[CONF_SEND_EVERY]) diff --git a/esphomeyaml/const.py b/esphomeyaml/const.py index 539e0e4ba0..e289bd10a2 100644 --- a/esphomeyaml/const.py +++ b/esphomeyaml/const.py @@ -358,6 +358,7 @@ CONF_POSITION = 'position' CONF_STEP_PIN = 'step_pin' CONF_DIR_PIN = 'dir_pin' CONF_SLEEP_PIN = 'sleep_pin' +CONF_SEND_FIRST_AT = 'send_first_at' ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_' ARDUINO_VERSION_ESP32_DEV = 'https://github.com/platformio/platform-espressif32.git#feature/stage' diff --git a/tests/test1.yaml b/tests/test1.yaml index d25abda030..9d7ca7cb8a 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -165,6 +165,7 @@ sensor: - sliding_window_moving_average: window_size: 15 send_every: 15 + send_first_at: 15 - exponential_moving_average: alpha: 0.1 send_every: 15