|
2 | 2 | import esphome.config_validation as cv |
3 | 3 | from esphome import cpp_generator as cpp |
4 | 4 | from esphome.const import CONF_INTERNAL, CONF_LAMBDA |
5 | | -from esphome.core import Lambda |
6 | 5 |
|
7 | 6 | CONF_NAMESPACE = "namespace" |
8 | | - |
| 7 | +CONF_INCLUDE = "include" |
9 | 8 |
|
10 | 9 | DECLARATION_SCHEMA = cv.Schema( |
11 | 10 | { |
12 | 11 | cv.Optional(CONF_NAMESPACE): cv.ensure_list(cv.valid_name), |
13 | 12 | cv.Optional(CONF_INTERNAL): cv.boolean, |
14 | | - cv.Required(CONF_LAMBDA): cv.lambda_, |
| 13 | + cv.Optional(CONF_LAMBDA): cv.lambda_, |
| 14 | + cv.Optional(CONF_INCLUDE): cv.ensure_list(cv.string), |
15 | 15 | } |
| 16 | +).add_extra( |
| 17 | + cv.has_at_least_one_key( |
| 18 | + CONF_LAMBDA, |
| 19 | + CONF_INCLUDE, |
| 20 | + ) |
16 | 21 | ) |
17 | 22 |
|
18 | | - |
19 | | -def _validate_declaration(conf): |
20 | | - if not isinstance(conf, dict): |
21 | | - conf = {CONF_LAMBDA: conf} |
22 | | - if not isinstance(conf[CONF_LAMBDA], Lambda): |
23 | | - conf[CONF_LAMBDA] = cv.lambda_(conf[CONF_LAMBDA]) |
24 | | - return conf |
25 | | - |
26 | | - |
27 | | -CONFIG_SCHEMA = cv.ensure_list(_validate_declaration) |
| 23 | +CONFIG_SCHEMA = cv.ensure_list(DECLARATION_SCHEMA) |
28 | 24 |
|
29 | 25 |
|
30 | 26 | async def to_code(config): |
| 27 | + includes = set() |
| 28 | + # process includes first |
31 | 29 | for conf in config: |
| 30 | + for inc in conf.get(CONF_INCLUDE, []): |
| 31 | + includes.add(inc) |
| 32 | + for inc in includes: |
| 33 | + cg.add_global(cg.RawStatement(f'#include "{inc}"')) |
| 34 | + |
| 35 | + for conf in config: |
| 36 | + if CONF_LAMBDA not in conf: |
| 37 | + continue |
| 38 | + |
32 | 39 | lambda_: cpp.LambdaExpression = await cg.process_lambda(conf[CONF_LAMBDA], []) |
33 | 40 | content = lambda_.content |
34 | 41 |
|
|
0 commit comments