More sensor filters

This commit is contained in:
Otto Winter 2018-05-18 09:44:25 +02:00
parent 48e42cf478
commit 61b3ead2df
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
2 changed files with 15 additions and 13 deletions

View file

@ -1,11 +1,11 @@
import voluptuous as vol import voluptuous as vol
import esphomeyaml.config_validation as cv 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_DEBOUNCE, CONF_DELTA, \
CONF_EXPONENTIAL_MOVING_AVERAGE, CONF_FILTERS, CONF_FILTER_NAN, CONF_FILTER_OUT, CONF_ICON, \ CONF_EXPIRE_AFTER, CONF_EXPONENTIAL_MOVING_AVERAGE, CONF_FILTERS, CONF_FILTER_NAN, \
CONF_LAMBDA, CONF_MQTT_ID, CONF_MULTIPLY, CONF_NAME, CONF_OFFSET, CONF_SEND_EVERY, \ CONF_FILTER_OUT, CONF_HEARTBEAT, CONF_ICON, CONF_LAMBDA, CONF_MQTT_ID, CONF_MULTIPLY, \
CONF_SLIDING_WINDOW_MOVING_AVERAGE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE, CONF_ID, \ CONF_NAME, CONF_OFFSET, CONF_OR, CONF_SEND_EVERY, CONF_SLIDING_WINDOW_MOVING_AVERAGE, \
CONF_THROTTLE, CONF_DELTA, CONF_OR, CONF_AND, CONF_UNIQUE CONF_THROTTLE, CONF_UNIQUE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE
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
@ -15,7 +15,6 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
def validate_recursive_filter(value): def validate_recursive_filter(value):
print(value)
return FILTERS_SCHEMA(value) return FILTERS_SCHEMA(value)
@ -40,10 +39,9 @@ FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.Any(
vol.Schema({vol.Required(CONF_THROTTLE): cv.positive_time_period_milliseconds}), 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_DELTA): vol.Coerce(float)}),
vol.Schema({vol.Required(CONF_UNIQUE): None}), vol.Schema({vol.Required(CONF_UNIQUE): None}),
vol.Schema({vol.Required(CONF_HEARTBEAT): cv.positive_time_period_milliseconds}),
# No ensure_list here as OR/AND filters only make sense with multiple children vol.Schema({vol.Required(CONF_DEBOUNCE): cv.positive_time_period_milliseconds}),
vol.Schema({vol.Required(CONF_OR): validate_recursive_filter}), 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({
@ -70,7 +68,8 @@ LambdaFilter = MockObj('new sensor::LambdaFilter')
ThrottleFilter = MockObj('new sensor::ThrottleFilter') ThrottleFilter = MockObj('new sensor::ThrottleFilter')
DeltaFilter = MockObj('new sensor::DeltaFilter') DeltaFilter = MockObj('new sensor::DeltaFilter')
OrFilter = MockObj('new sensor::OrFilter') OrFilter = MockObj('new sensor::OrFilter')
AndFilter = MockObj('new sensor::AndFilter') HeartbeatFilter = MockObj('new sensor::HeartbeatFilter')
DebounceFilter = MockObj('new sensor::DebounceFilter')
UniqueFilter = MockObj('new sensor::UniqueFilter') UniqueFilter = MockObj('new sensor::UniqueFilter')
@ -98,8 +97,10 @@ def setup_filter(config):
return DeltaFilter(config[CONF_DELTA]) return DeltaFilter(config[CONF_DELTA])
if CONF_OR in config: if CONF_OR in config:
return OrFilter(setup_filters(config[CONF_OR])) return OrFilter(setup_filters(config[CONF_OR]))
if CONF_AND in config: if CONF_HEARTBEAT in config:
return OrFilter(setup_filters(config[CONF_AND])) return App.register_component(HeartbeatFilter(config[CONF_HEARTBEAT]))
if CONF_DEBOUNCE in config:
return App.register_component(DebounceFilter(config[CONF_DEBOUNCE]))
if CONF_UNIQUE in config: if CONF_UNIQUE in config:
return UniqueFilter() return UniqueFilter()
raise ValueError(u"Filter unsupported: {}".format(config)) raise ValueError(u"Filter unsupported: {}".format(config))

View file

@ -112,8 +112,9 @@ CONF_LAMBDA = 'lambda'
CONF_THROTTLE = 'throttle' CONF_THROTTLE = 'throttle'
CONF_DELTA = 'delta' CONF_DELTA = 'delta'
CONF_OR = 'or' CONF_OR = 'or'
CONF_AND = 'and'
CONF_UNIQUE = 'unique' CONF_UNIQUE = 'unique'
CONF_HEARTBEAT = 'heartbeat'
CONF_DEBOUNCE = 'debounce'
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'