2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_INVERTED,
|
|
|
|
CONF_MODE,
|
|
|
|
CONF_NUMBER,
|
|
|
|
CONF_SETUP_PRIORITY,
|
|
|
|
CONF_UPDATE_INTERVAL,
|
|
|
|
CONF_TYPE_ID,
|
|
|
|
)
|
|
|
|
|
2019-12-07 18:28:55 +01:00
|
|
|
# pylint: disable=unused-import
|
|
|
|
from esphome.core import coroutine, ID, CORE, ConfigType
|
2019-05-11 12:31:00 +02:00
|
|
|
from esphome.cpp_generator import RawExpression, add, get_variable
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.cpp_types import App, GPIOPin
|
2019-12-07 18:28:55 +01:00
|
|
|
from esphome.util import Registry, RegistryEntry
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def gpio_pin_expression(conf):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Generate an expression for the given pin option.
|
|
|
|
|
2021-05-24 21:45:31 +02:00
|
|
|
This is a coroutine, you must await it with a 'await' expression!
|
2019-04-22 21:56:30 +02:00
|
|
|
"""
|
2018-12-05 21:22:06 +01:00
|
|
|
if conf is None:
|
|
|
|
return
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome import pins
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
for key, (func, _) in pins.PIN_SCHEMA_REGISTRY.items():
|
2019-04-17 12:06:00 +02:00
|
|
|
if key in conf:
|
2021-05-23 22:10:30 +02:00
|
|
|
return await coroutine(func)(conf)
|
2019-03-03 16:51:55 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
number = conf[CONF_NUMBER]
|
|
|
|
mode = conf[CONF_MODE]
|
|
|
|
inverted = conf.get(CONF_INVERTED)
|
2021-05-23 22:10:30 +02:00
|
|
|
return GPIOPin.new(number, RawExpression(mode), inverted)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def register_component(var, config):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Register the given obj as a component.
|
|
|
|
|
2021-05-24 21:45:31 +02:00
|
|
|
This is a coroutine, you must await it with a 'await' expression!
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
:param var: The variable representing the component.
|
|
|
|
:param config: The configuration for the component.
|
|
|
|
"""
|
2019-12-07 18:28:55 +01:00
|
|
|
id_ = str(var.base)
|
2019-05-21 12:23:38 +02:00
|
|
|
if id_ not in CORE.component_ids:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise ValueError(
|
|
|
|
"Component ID {} was not declared to inherit from Component, "
|
|
|
|
"or was registered twice. Please create a bug report with your "
|
|
|
|
"configuration.".format(id_)
|
|
|
|
)
|
2019-05-21 12:23:38 +02:00
|
|
|
CORE.component_ids.remove(id_)
|
2019-04-17 12:06:00 +02:00
|
|
|
if CONF_SETUP_PRIORITY in config:
|
2019-04-22 21:56:30 +02:00
|
|
|
add(var.set_setup_priority(config[CONF_SETUP_PRIORITY]))
|
|
|
|
if CONF_UPDATE_INTERVAL in config:
|
|
|
|
add(var.set_update_interval(config[CONF_UPDATE_INTERVAL]))
|
|
|
|
add(App.register_component(var))
|
2021-05-23 22:10:30 +02:00
|
|
|
return var
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def register_parented(var, value):
|
2019-05-11 12:31:00 +02:00
|
|
|
if isinstance(value, ID):
|
2021-05-23 22:10:30 +02:00
|
|
|
paren = await get_variable(value)
|
2019-05-11 12:31:00 +02:00
|
|
|
else:
|
|
|
|
paren = value
|
|
|
|
add(var.set_parent(paren))
|
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
def extract_registry_entry_config(registry, full_config):
|
2019-12-07 18:28:55 +01:00
|
|
|
# type: (Registry, ConfigType) -> RegistryEntry
|
2019-04-22 21:56:30 +02:00
|
|
|
key, config = next((k, v) for k, v in full_config.items() if k in registry)
|
|
|
|
return registry[key], config
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def build_registry_entry(registry, full_config):
|
2019-04-22 21:56:30 +02:00
|
|
|
registry_entry, config = extract_registry_entry_config(registry, full_config)
|
|
|
|
type_id = full_config[CONF_TYPE_ID]
|
|
|
|
builder = registry_entry.coroutine_fun
|
2021-05-23 22:10:30 +02:00
|
|
|
return await builder(config, type_id)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def build_registry_list(registry, config):
|
2019-04-17 12:06:00 +02:00
|
|
|
actions = []
|
|
|
|
for conf in config:
|
2021-05-23 22:10:30 +02:00
|
|
|
action = await build_registry_entry(registry, conf)
|
2019-04-17 12:06:00 +02:00
|
|
|
actions.append(action)
|
2021-05-23 22:10:30 +02:00
|
|
|
return actions
|