esphome/esphomeyaml/components/binary_sensor/__init__.py

155 lines
6.6 KiB
Python
Raw Normal View History

2018-04-07 01:23:03 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
2018-05-20 12:41:52 +02:00
from esphomeyaml import automation
2018-06-12 21:18:04 +02:00
from esphomeyaml.const import CONF_DEVICE_CLASS, CONF_ID, CONF_INTERNAL, CONF_INVERTED, \
CONF_MAX_LENGTH, CONF_MIN_LENGTH, CONF_MQTT_ID, CONF_ON_CLICK, CONF_ON_DOUBLE_CLICK, \
CONF_ON_PRESS, CONF_ON_RELEASE, CONF_TRIGGER_ID, CONF_FILTERS, CONF_INVERT, CONF_DELAYED_ON, \
CONF_DELAYED_OFF, CONF_LAMBDA
from esphomeyaml.helpers import App, NoArg, Pvariable, add, add_job, esphomelib_ns, \
setup_mqtt_component, bool_, process_lambda, ArrayInitializer
2018-04-07 01:23:03 +02:00
DEVICE_CLASSES = [
'', 'battery', 'cold', 'connectivity', 'door', 'garage_door', 'gas',
'heat', 'light', 'lock', 'moisture', 'motion', 'moving', 'occupancy',
'opening', 'plug', 'power', 'presence', 'problem', 'safety', 'smoke',
'sound', 'vibration', 'window'
]
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
2018-04-07 01:23:03 +02:00
})
2018-05-20 12:41:52 +02:00
binary_sensor_ns = esphomelib_ns.namespace('binary_sensor')
PressTrigger = binary_sensor_ns.PressTrigger
ReleaseTrigger = binary_sensor_ns.ReleaseTrigger
ClickTrigger = binary_sensor_ns.ClickTrigger
DoubleClickTrigger = binary_sensor_ns.DoubleClickTrigger
BinarySensor = binary_sensor_ns.BinarySensor
2018-06-12 21:18:04 +02:00
InvertFilter = binary_sensor_ns.InvertFilter
LambdaFilter = binary_sensor_ns.LambdaFilter
DelayedOnFilter = binary_sensor_ns.DelayedOnFilter
DelayedOffFilter = binary_sensor_ns.DelayedOffFilter
2018-05-20 12:41:52 +02:00
MQTTBinarySensorComponent = binary_sensor_ns.MQTTBinarySensorComponent
2018-06-12 21:18:04 +02:00
FILTER_KEYS = [CONF_INVERT, CONF_DELAYED_ON, CONF_DELAYED_OFF, CONF_LAMBDA]
FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.All({
vol.Optional(CONF_INVERT): None,
vol.Optional(CONF_DELAYED_ON): cv.positive_time_period_milliseconds,
vol.Optional(CONF_DELAYED_OFF): cv.positive_time_period_milliseconds,
vol.Optional(CONF_LAMBDA): cv.lambda_,
}, cv.has_exactly_one_key(*FILTER_KEYS))])
2018-05-20 12:41:52 +02:00
BINARY_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTBinarySensorComponent),
cv.GenerateID(): cv.declare_variable_id(BinarySensor),
2018-06-12 21:18:04 +02:00
2018-05-21 20:55:39 +02:00
vol.Optional(CONF_DEVICE_CLASS): vol.All(vol.Lower, cv.one_of(*DEVICE_CLASSES)),
2018-06-12 21:18:04 +02:00
vol.Optional(CONF_FILTERS): FILTERS_SCHEMA,
2018-06-13 21:27:58 +02:00
vol.Optional(CONF_ON_PRESS): vol.All(cv.ensure_list, [automation.validate_automation({
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(PressTrigger),
})]),
2018-06-13 21:27:58 +02:00
vol.Optional(CONF_ON_RELEASE): vol.All(cv.ensure_list, [automation.validate_automation({
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(ReleaseTrigger),
})]),
2018-06-13 21:27:58 +02:00
vol.Optional(CONF_ON_CLICK): vol.All(cv.ensure_list, [automation.validate_automation({
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(ClickTrigger),
2018-05-20 12:41:52 +02:00
vol.Optional(CONF_MIN_LENGTH, default='50ms'): cv.positive_time_period_milliseconds,
vol.Optional(CONF_MAX_LENGTH, default='350ms'): cv.positive_time_period_milliseconds,
})]),
vol.Optional(CONF_ON_DOUBLE_CLICK):
2018-06-13 21:27:58 +02:00
vol.All(cv.ensure_list, [automation.validate_automation({
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(DoubleClickTrigger),
2018-05-20 12:41:52 +02:00
vol.Optional(CONF_MIN_LENGTH, default='50ms'): cv.positive_time_period_milliseconds,
vol.Optional(CONF_MAX_LENGTH, default='350ms'): cv.positive_time_period_milliseconds,
})]),
2018-06-12 21:18:04 +02:00
vol.Optional(CONF_INVERTED): cv.invalid(
"The inverted binary_sensor property has been replaced by the "
"new 'invert' binary sensor filter. Please see "
"https://esphomelib.com/esphomeyaml/components/binary_sensor/index.html."
),
2018-05-14 11:50:56 +02:00
})
BINARY_SENSOR_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(BINARY_SENSOR_SCHEMA.schema)
2018-06-12 21:18:04 +02:00
def setup_filter(config):
if CONF_INVERT in config:
yield InvertFilter.new()
elif CONF_DELAYED_OFF in config:
yield App.register_component(DelayedOffFilter.new(config[CONF_DELAYED_OFF]))
elif CONF_DELAYED_ON in config:
yield App.register_component(DelayedOnFilter.new(config[CONF_DELAYED_ON]))
elif CONF_LAMBDA in config:
lambda_ = None
for lambda_ in process_lambda(config[CONF_LAMBDA], [(bool_, 'x')]):
yield None
yield LambdaFilter.new(lambda_)
def setup_filters(config):
filters = []
for conf in config:
filter = None
for filter in setup_filter(conf):
yield None
filters.append(filter)
yield ArrayInitializer(*filters)
2018-05-20 12:41:52 +02:00
def setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config):
if CONF_INTERNAL in config:
add(binary_sensor_var.set_internal(CONF_INTERNAL))
if CONF_DEVICE_CLASS in config:
2018-05-20 12:41:52 +02:00
add(binary_sensor_var.set_device_class(config[CONF_DEVICE_CLASS]))
if CONF_INVERTED in config:
2018-05-20 12:41:52 +02:00
add(binary_sensor_var.set_inverted(config[CONF_INVERTED]))
2018-06-12 21:18:04 +02:00
if CONF_FILTERS in config:
filters = None
for filters in setup_filters(config[CONF_FILTERS]):
yield
add(binary_sensor_var.add_filters(filters))
2018-05-20 12:41:52 +02:00
for conf in config.get(CONF_ON_PRESS, []):
rhs = binary_sensor_var.make_press_trigger()
2018-06-02 22:22:20 +02:00
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
2018-06-03 07:11:11 +02:00
automation.build_automation(trigger, NoArg, conf)
2018-05-20 12:41:52 +02:00
for conf in config.get(CONF_ON_RELEASE, []):
rhs = binary_sensor_var.make_release_trigger()
2018-06-02 22:22:20 +02:00
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
2018-06-03 07:11:11 +02:00
automation.build_automation(trigger, NoArg, conf)
2018-05-20 12:41:52 +02:00
for conf in config.get(CONF_ON_CLICK, []):
2018-06-01 18:06:18 +02:00
rhs = binary_sensor_var.make_click_trigger(conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH])
2018-06-02 22:22:20 +02:00
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
2018-06-03 07:11:11 +02:00
automation.build_automation(trigger, NoArg, conf)
2018-05-20 12:41:52 +02:00
for conf in config.get(CONF_ON_DOUBLE_CLICK, []):
rhs = binary_sensor_var.make_double_click_trigger(conf[CONF_MIN_LENGTH],
conf[CONF_MAX_LENGTH])
2018-06-02 22:22:20 +02:00
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
2018-06-03 07:11:11 +02:00
automation.build_automation(trigger, NoArg, conf)
2018-05-20 12:41:52 +02:00
setup_mqtt_component(mqtt_var, config)
2018-05-20 12:41:52 +02:00
def setup_binary_sensor(binary_sensor_obj, mqtt_obj, config):
2018-06-02 22:22:20 +02:00
binary_sensor_var = Pvariable(config[CONF_ID], binary_sensor_obj,
2018-05-20 12:41:52 +02:00
has_side_effects=False)
2018-06-02 22:22:20 +02:00
mqtt_var = Pvariable(config[CONF_MQTT_ID], mqtt_obj,
2018-05-20 12:41:52 +02:00
has_side_effects=False)
2018-06-03 07:11:11 +02:00
add_job(setup_binary_sensor_core_, binary_sensor_var, mqtt_var, config)
2018-05-14 11:50:56 +02:00
def register_binary_sensor(var, config):
2018-06-02 22:22:20 +02:00
binary_sensor_var = Pvariable(config[CONF_ID], var, has_side_effects=True)
2018-05-20 12:41:52 +02:00
rhs = App.register_binary_sensor(binary_sensor_var)
2018-06-02 22:22:20 +02:00
mqtt_var = Pvariable(config[CONF_MQTT_ID], rhs, has_side_effects=True)
2018-06-03 07:11:11 +02:00
add_job(setup_binary_sensor_core_, binary_sensor_var, mqtt_var, config)
2018-05-14 11:50:56 +02:00
2018-05-06 15:56:12 +02:00
BUILD_FLAGS = '-DUSE_BINARY_SENSOR'