mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 00:18:11 +01:00
More filters
This commit is contained in:
parent
19929fafa5
commit
9af30061cb
3 changed files with 47 additions and 22 deletions
|
@ -4,7 +4,8 @@ import esphomeyaml.config_validation as cv
|
||||||
from esphomeyaml.const import CONF_ACCURACY_DECIMALS, CONF_ALPHA, CONF_EXPIRE_AFTER, \
|
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_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_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, \
|
from esphomeyaml.helpers import App, ArrayInitializer, MockObj, Pvariable, RawExpression, add, \
|
||||||
setup_mqtt_component
|
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(
|
FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.Any(
|
||||||
vol.Schema({vol.Required(CONF_OFFSET): vol.Coerce(float)}),
|
vol.Schema({vol.Required(CONF_OFFSET): vol.Coerce(float)}),
|
||||||
vol.Schema({vol.Required(CONF_MULTIPLY): 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_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({
|
MQTT_SENSOR_SCHEMA = vol.Schema({
|
||||||
|
@ -53,6 +67,11 @@ FilterOutNANFilter = MockObj('new sensor::FilterOutNANFilter')
|
||||||
SlidingWindowMovingAverageFilter = MockObj('new sensor::SlidingWindowMovingAverageFilter')
|
SlidingWindowMovingAverageFilter = MockObj('new sensor::SlidingWindowMovingAverageFilter')
|
||||||
ExponentialMovingAverageFilter = MockObj('new sensor::ExponentialMovingAverageFilter')
|
ExponentialMovingAverageFilter = MockObj('new sensor::ExponentialMovingAverageFilter')
|
||||||
LambdaFilter = MockObj('new sensor::LambdaFilter')
|
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):
|
def setup_filter(config):
|
||||||
|
@ -73,9 +92,23 @@ def setup_filter(config):
|
||||||
if CONF_LAMBDA in config:
|
if CONF_LAMBDA in config:
|
||||||
s = u'[](float x) -> Optional<float> {{ return {}; }}'.format(config[CONF_LAMBDA])
|
s = u'[](float x) -> Optional<float> {{ return {}; }}'.format(config[CONF_LAMBDA])
|
||||||
return LambdaFilter(RawExpression(s))
|
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))
|
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):
|
def setup_mqtt_sensor_component(obj, config):
|
||||||
if CONF_EXPIRE_AFTER in config:
|
if CONF_EXPIRE_AFTER in config:
|
||||||
if config[CONF_EXPIRE_AFTER] is None:
|
if config[CONF_EXPIRE_AFTER] is None:
|
||||||
|
@ -93,8 +126,7 @@ def setup_sensor(obj, config):
|
||||||
if CONF_ACCURACY_DECIMALS in config:
|
if CONF_ACCURACY_DECIMALS in config:
|
||||||
add(obj.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
|
add(obj.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
|
||||||
if CONF_FILTERS in config:
|
if CONF_FILTERS in config:
|
||||||
filters = [setup_filter(x) for x in config[CONF_FILTERS]]
|
add(obj.set_filters(setup_filters(config[CONF_FILTERS])))
|
||||||
add(obj.set_filters(ArrayInitializer(*filters)))
|
|
||||||
|
|
||||||
|
|
||||||
def register_sensor(var, config):
|
def register_sensor(var, config):
|
||||||
|
|
|
@ -209,13 +209,6 @@ def time_period_str_colon(value):
|
||||||
elif not isinstance(value, str):
|
elif not isinstance(value, str):
|
||||||
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
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:
|
try:
|
||||||
parsed = [int(x) for x in value.split(':')]
|
parsed = [int(x) for x in value.split(':')]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -229,12 +222,7 @@ def time_period_str_colon(value):
|
||||||
else:
|
else:
|
||||||
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
|
||||||
|
|
||||||
offset = TimePeriod(hours=hour, minutes=minute, seconds=second)
|
return TimePeriod(hours=hour, minutes=minute, seconds=second)
|
||||||
|
|
||||||
if negative_offset:
|
|
||||||
offset *= -1
|
|
||||||
|
|
||||||
return offset
|
|
||||||
|
|
||||||
|
|
||||||
def time_period_str_unit(value):
|
def time_period_str_unit(value):
|
||||||
|
@ -277,17 +265,17 @@ def time_period_str_unit(value):
|
||||||
return TimePeriod(**{kwarg: float(match.group(1))})
|
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:
|
if value.microseconds is not None and value.microseconds != 0:
|
||||||
raise vol.Invalid("Maximum precision is milliseconds")
|
raise vol.Invalid("Maximum precision is milliseconds")
|
||||||
return TimePeriodMilliseconds(**value.as_dict())
|
return TimePeriodMilliseconds(**value.as_dict())
|
||||||
|
|
||||||
|
|
||||||
def time_period_in_microseconds(value):
|
def time_period_in_microseconds_(value):
|
||||||
return TimePeriodMicroseconds(**value.as_dict())
|
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:
|
if value.microseconds is not None and value.microseconds != 0:
|
||||||
raise vol.Invalid("Maximum precision is seconds")
|
raise vol.Invalid("Maximum precision is seconds")
|
||||||
if value.milliseconds is not None and value.milliseconds != 0:
|
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)
|
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 = vol.All(time_period, vol.Range(min=TimePeriod()))
|
||||||
positive_time_period_milliseconds = vol.All(positive_time_period, time_period_in_milliseconds)
|
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_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_microseconds = vol.All(positive_time_period, time_period_in_microseconds_)
|
||||||
positive_not_null_time_period = vol.All(time_period,
|
positive_not_null_time_period = vol.All(time_period,
|
||||||
vol.Range(min=TimePeriod(), min_included=False))
|
vol.Range(min=TimePeriod(), min_included=False))
|
||||||
|
|
||||||
|
|
|
@ -109,6 +109,11 @@ CONF_WINDOW_SIZE = 'window_size'
|
||||||
CONF_SEND_EVERY = 'send_every'
|
CONF_SEND_EVERY = 'send_every'
|
||||||
CONF_ALPHA = 'alpha'
|
CONF_ALPHA = 'alpha'
|
||||||
CONF_LAMBDA = 'lambda'
|
CONF_LAMBDA = 'lambda'
|
||||||
|
CONF_THROTTLE = 'throttle'
|
||||||
|
CONF_DELTA = 'delta'
|
||||||
|
CONF_OR = 'or'
|
||||||
|
CONF_AND = 'and'
|
||||||
|
CONF_UNIQUE = 'unique'
|
||||||
CONF_UPDATE_INTERVAL = 'update_interval'
|
CONF_UPDATE_INTERVAL = 'update_interval'
|
||||||
CONF_PULL_MODE = 'pull_mode'
|
CONF_PULL_MODE = 'pull_mode'
|
||||||
CONF_COUNT_MODE = 'count_mode'
|
CONF_COUNT_MODE = 'count_mode'
|
||||||
|
|
Loading…
Reference in a new issue