-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
__init__.py
58 lines (51 loc) · 1.62 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import core
from esphome import cpp_generator as cpp
from esphome.const import CONF_ID, CONF_LAMBDA
CONF_DEPENDS = "depends"
CONFIG_SCHEMA = cv.ensure_list(
cv.Any(
cv.lambda_,
cv.Schema(
{
cv.Optional(CONF_DEPENDS): cv.ensure_list(cv.string),
cv.Required(CONF_LAMBDA): cv.lambda_,
}
),
)
)
def _get_deps(config):
deps = []
for dep in config.get(CONF_DEPENDS, []):
parts = str(dep).split(" as ")
deps.append(
{
"key": parts[0].strip(),
"var": parts[1].strip() if len(parts) == 2 else "",
}
)
return deps
@core.coroutine_with_priority(-300.0)
async def to_code(config):
for conf in config:
if isinstance(conf, cv.Lambda):
conf = {CONF_LAMBDA: conf}
deps = _get_deps(conf)
if not set([dep["key"] for dep in deps]).issubset(core.CORE.config):
continue
lambda_: cpp.LambdaExpression = await cg.process_lambda(conf[CONF_LAMBDA], [])
content = lambda_.content
if lambda_.source:
content = f"{lambda_.source.as_line_directive}\n{content}"
vars = "\n".join(
[
f"auto *{dep['var']} = {core.CORE.config[dep['key']][CONF_ID]};"
for dep in deps
if dep["var"]
]
)
if vars:
vars += "\n"
content = cpp.indent_all_but_first_and_last(f"{{\n{vars}{content}\n}}")
cg.add(cg.RawStatement(content))