esphome/esphomeyaml/components/switch/custom.py

37 lines
1.2 KiB
Python
Raw Normal View History

2018-11-19 22:12:24 +01:00
import voluptuous as vol
2018-11-27 16:54:11 +01:00
from esphomeyaml.components import switch
2018-11-19 22:12:24 +01:00
import esphomeyaml.config_validation as cv
2018-11-27 16:54:11 +01:00
from esphomeyaml.const import CONF_ID, CONF_LAMBDA, CONF_SWITCHES
2018-11-19 22:12:24 +01:00
from esphomeyaml.cpp_generator import process_lambda, variable
from esphomeyaml.cpp_types import std_vector
CustomSwitchConstructor = switch.switch_ns.class_('CustomSwitchConstructor')
2018-11-27 16:54:11 +01:00
PLATFORM_SCHEMA = switch.PLATFORM_SCHEMA.extend({
2018-11-19 22:12:24 +01:00
cv.GenerateID(): cv.declare_variable_id(CustomSwitchConstructor),
vol.Required(CONF_LAMBDA): cv.lambda_,
vol.Required(CONF_SWITCHES):
vol.All(cv.ensure_list, [switch.SWITCH_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(switch.Switch),
})]),
})
def to_code(config):
for template_ in process_lambda(config[CONF_LAMBDA], [],
return_type=std_vector.template(switch.SwitchPtr)):
yield
rhs = CustomSwitchConstructor(template_)
custom = variable(config[CONF_ID], rhs)
for i, sens in enumerate(config[CONF_SWITCHES]):
switch.register_switch(custom.get_switch(i), sens)
2018-11-27 16:54:11 +01:00
BUILD_FLAGS = '-DUSE_CUSTOM_SWITCH'
2018-11-19 22:12:24 +01:00
def to_hass_config(data, config):
2018-11-27 16:54:11 +01:00
return [switch.core_to_hass_config(data, swi) for swi in config[CONF_SWITCHES]]