From 9af30061cbc023d4b31d9e5694f188e43f383a50 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 17 May 2018 21:31:39 +0200 Subject: [PATCH] More filters --- esphomeyaml/components/sensor/__init__.py | 38 +++++++++++++++++++++-- esphomeyaml/config_validation.py | 26 +++++----------- esphomeyaml/const.py | 5 +++ 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/esphomeyaml/components/sensor/__init__.py b/esphomeyaml/components/sensor/__init__.py index bd14c3c744..a33269fbbf 100644 --- a/esphomeyaml/components/sensor/__init__.py +++ b/esphomeyaml/components/sensor/__init__.py @@ -4,7 +4,8 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ACCURACY_DECIMALS, CONF_ALPHA, CONF_EXPIRE_AFTER, \ CONF_EXPONENTIAL_MOVING_AVERAGE, CONF_FILTERS, CONF_FILTER_NAN, CONF_FILTER_OUT, CONF_ICON, \ CONF_LAMBDA, CONF_MQTT_ID, CONF_MULTIPLY, CONF_NAME, CONF_OFFSET, CONF_SEND_EVERY, \ - CONF_SLIDING_WINDOW_MOVING_AVERAGE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE, CONF_ID + CONF_SLIDING_WINDOW_MOVING_AVERAGE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE, CONF_ID, \ + CONF_THROTTLE, CONF_DELTA, CONF_OR, CONF_AND, CONF_UNIQUE from esphomeyaml.helpers import App, ArrayInitializer, MockObj, Pvariable, RawExpression, add, \ setup_mqtt_component @@ -12,6 +13,12 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) + +def validate_recursive_filter(value): + print(value) + return FILTERS_SCHEMA(value) + + FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.Any( vol.Schema({vol.Required(CONF_OFFSET): vol.Coerce(float)}), vol.Schema({vol.Required(CONF_MULTIPLY): vol.Coerce(float)}), @@ -30,6 +37,13 @@ FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.Any( }) }), vol.Schema({vol.Required(CONF_LAMBDA): cv.string_strict}), + vol.Schema({vol.Required(CONF_THROTTLE): cv.positive_time_period_milliseconds}), + vol.Schema({vol.Required(CONF_DELTA): vol.Coerce(float)}), + vol.Schema({vol.Required(CONF_UNIQUE): None}), + + # No ensure_list here as OR/AND filters only make sense with multiple children + vol.Schema({vol.Required(CONF_OR): validate_recursive_filter}), + vol.Schema({vol.Required(CONF_AND): validate_recursive_filter}), )]) MQTT_SENSOR_SCHEMA = vol.Schema({ @@ -53,6 +67,11 @@ FilterOutNANFilter = MockObj('new sensor::FilterOutNANFilter') SlidingWindowMovingAverageFilter = MockObj('new sensor::SlidingWindowMovingAverageFilter') ExponentialMovingAverageFilter = MockObj('new sensor::ExponentialMovingAverageFilter') LambdaFilter = MockObj('new sensor::LambdaFilter') +ThrottleFilter = MockObj('new sensor::ThrottleFilter') +DeltaFilter = MockObj('new sensor::DeltaFilter') +OrFilter = MockObj('new sensor::OrFilter') +AndFilter = MockObj('new sensor::AndFilter') +UniqueFilter = MockObj('new sensor::UniqueFilter') def setup_filter(config): @@ -73,9 +92,23 @@ def setup_filter(config): if CONF_LAMBDA in config: s = u'[](float x) -> Optional {{ return {}; }}'.format(config[CONF_LAMBDA]) return LambdaFilter(RawExpression(s)) + if CONF_THROTTLE in config: + return ThrottleFilter(config[CONF_THROTTLE]) + if CONF_DELTA in config: + return DeltaFilter(config[CONF_DELTA]) + if CONF_OR in config: + return OrFilter(setup_filters(config[CONF_OR])) + if CONF_AND in config: + return OrFilter(setup_filters(config[CONF_AND])) + if CONF_UNIQUE in config: + return UniqueFilter() raise ValueError(u"Filter unsupported: {}".format(config)) +def setup_filters(config): + return ArrayInitializer(*[setup_filter(x) for x in config]) + + def setup_mqtt_sensor_component(obj, config): if CONF_EXPIRE_AFTER in config: if config[CONF_EXPIRE_AFTER] is None: @@ -93,8 +126,7 @@ def setup_sensor(obj, config): if CONF_ACCURACY_DECIMALS in config: add(obj.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS])) if CONF_FILTERS in config: - filters = [setup_filter(x) for x in config[CONF_FILTERS]] - add(obj.set_filters(ArrayInitializer(*filters))) + add(obj.set_filters(setup_filters(config[CONF_FILTERS]))) def register_sensor(var, config): diff --git a/esphomeyaml/config_validation.py b/esphomeyaml/config_validation.py index 5d8b3bfa11..455a5e125b 100644 --- a/esphomeyaml/config_validation.py +++ b/esphomeyaml/config_validation.py @@ -209,13 +209,6 @@ def time_period_str_colon(value): elif not isinstance(value, str): raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) - negative_offset = False - if value.startswith('-'): - negative_offset = True - value = value[1:] - elif value.startswith('+'): - value = value[1:] - try: parsed = [int(x) for x in value.split(':')] except ValueError: @@ -229,12 +222,7 @@ def time_period_str_colon(value): else: raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) - offset = TimePeriod(hours=hour, minutes=minute, seconds=second) - - if negative_offset: - offset *= -1 - - return offset + return TimePeriod(hours=hour, minutes=minute, seconds=second) def time_period_str_unit(value): @@ -277,17 +265,17 @@ def time_period_str_unit(value): return TimePeriod(**{kwarg: float(match.group(1))}) -def time_period_in_milliseconds(value): +def time_period_in_milliseconds_(value): if value.microseconds is not None and value.microseconds != 0: raise vol.Invalid("Maximum precision is milliseconds") return TimePeriodMilliseconds(**value.as_dict()) -def time_period_in_microseconds(value): +def time_period_in_microseconds_(value): return TimePeriodMicroseconds(**value.as_dict()) -def time_period_in_seconds(value): +def time_period_in_seconds_(value): if value.microseconds is not None and value.microseconds != 0: raise vol.Invalid("Maximum precision is seconds") if value.milliseconds is not None and value.milliseconds != 0: @@ -297,9 +285,9 @@ def time_period_in_seconds(value): time_period = vol.Any(time_period_str_unit, time_period_str_colon, time_period_dict) positive_time_period = vol.All(time_period, vol.Range(min=TimePeriod())) -positive_time_period_milliseconds = vol.All(positive_time_period, time_period_in_milliseconds) -positive_time_period_seconds = vol.All(positive_time_period, time_period_in_seconds) -positive_time_period_microseconds = vol.All(positive_time_period, time_period_in_microseconds) +positive_time_period_milliseconds = vol.All(positive_time_period, time_period_in_milliseconds_) +positive_time_period_seconds = vol.All(positive_time_period, time_period_in_seconds_) +positive_time_period_microseconds = vol.All(positive_time_period, time_period_in_microseconds_) positive_not_null_time_period = vol.All(time_period, vol.Range(min=TimePeriod(), min_included=False)) diff --git a/esphomeyaml/const.py b/esphomeyaml/const.py index ecde750020..69fb837a76 100644 --- a/esphomeyaml/const.py +++ b/esphomeyaml/const.py @@ -109,6 +109,11 @@ CONF_WINDOW_SIZE = 'window_size' CONF_SEND_EVERY = 'send_every' CONF_ALPHA = 'alpha' CONF_LAMBDA = 'lambda' +CONF_THROTTLE = 'throttle' +CONF_DELTA = 'delta' +CONF_OR = 'or' +CONF_AND = 'and' +CONF_UNIQUE = 'unique' CONF_UPDATE_INTERVAL = 'update_interval' CONF_PULL_MODE = 'pull_mode' CONF_COUNT_MODE = 'count_mode'