esphome/esphomeyaml/components/custom_component.py

35 lines
1.3 KiB
Python
Raw Normal View History

2018-11-26 20:43:02 +01:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
2018-11-28 21:33:24 +01:00
from esphomeyaml.const import CONF_ID, CONF_LAMBDA, CONF_COMPONENTS
2018-11-26 20:43:02 +01:00
from esphomeyaml.cpp_generator import process_lambda, variable
2018-11-28 21:33:24 +01:00
from esphomeyaml.cpp_helpers import setup_component
2018-11-26 20:43:02 +01:00
from esphomeyaml.cpp_types import Component, ComponentPtr, esphomelib_ns, std_vector
CustomComponentConstructor = esphomelib_ns.class_('CustomComponentConstructor')
CUSTOM_COMPONENT_SCHEMA = vol.Schema({
2018-11-28 21:33:24 +01:00
cv.GenerateID(): cv.declare_variable_id(CustomComponentConstructor),
2018-11-26 20:43:02 +01:00
vol.Required(CONF_LAMBDA): cv.lambda_,
2018-11-28 21:33:24 +01:00
vol.Optional(CONF_COMPONENTS): vol.All(cv.ensure_list, [vol.Schema({
cv.GenerateID(): cv.declare_variable_id(Component)
}).extend(cv.COMPONENT_SCHEMA.schema)]),
2018-11-26 20:43:02 +01:00
})
2018-11-28 21:33:24 +01:00
CONFIG_SCHEMA = vol.All(cv.ensure_list, [CUSTOM_COMPONENT_SCHEMA])
2018-11-26 20:43:02 +01:00
2018-11-28 21:33:24 +01:00
def to_code(config):
for conf in config:
for template_ in process_lambda(conf[CONF_LAMBDA], [],
return_type=std_vector.template(ComponentPtr)):
yield
rhs = CustomComponentConstructor(template_)
custom = variable(conf[CONF_ID], rhs)
for i, comp in enumerate(conf.get(CONF_COMPONENTS, [])):
setup_component(custom.get_component(i), comp)
2018-11-26 20:43:02 +01:00
BUILD_FLAGS = '-DUSE_CUSTOM_COMPONENT'