-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
__init__.py
56 lines (45 loc) · 1.59 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
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import cpp_generator as cpp
from esphome.const import CONF_INTERNAL, CONF_LAMBDA
CONF_NAMESPACE = "namespace"
CONF_INCLUDE = "include"
DECLARATION_SCHEMA = cv.Schema(
{
cv.Optional(CONF_NAMESPACE): cv.ensure_list(cv.valid_name),
cv.Optional(CONF_INTERNAL): cv.boolean,
cv.Optional(CONF_LAMBDA): cv.lambda_,
cv.Optional(CONF_INCLUDE): cv.ensure_list(cv.string),
}
).add_extra(
cv.has_at_least_one_key(
CONF_LAMBDA,
CONF_INCLUDE,
)
)
CONFIG_SCHEMA = cv.ensure_list(DECLARATION_SCHEMA)
async def to_code(config):
includes = set()
# process includes first
for conf in config:
for inc in conf.get(CONF_INCLUDE, []):
includes.add(inc)
for inc in includes:
cg.add_global(cg.RawStatement(f'#include "{inc}"'))
for conf in config:
if CONF_LAMBDA not in conf:
continue
lambda_: cpp.LambdaExpression = await cg.process_lambda(conf[CONF_LAMBDA], [])
content = lambda_.content
namespaces = conf.get(CONF_NAMESPACE, [])
if conf.get(CONF_INTERNAL, True):
namespaces = ["/* anonymous */", *namespaces]
ns_prefix = ""
ns_suffix = ""
for ns in namespaces:
ns_prefix += f"namespace {ns} {{\n"
for ns in reversed(namespaces):
ns_suffix += f"\n}} // namespace {ns}"
line = lambda_.source.as_line_directive
content = f"{line}\n{content}"
cg.add_global(cg.RawStatement(f"{ns_prefix}{content}{ns_suffix}"))