esphome/esphome/cpp_helpers.py
Otto Winter f1a0e5a313
Sun support (#531)
* Sun

* Add sun support

* Lint

* Updates

* Fix elevation

* Lint

* Update mqtt_climate.cpp
2019-05-11 12:31:00 +02:00

75 lines
2.2 KiB
Python

from esphome.const import CONF_INVERTED, CONF_MODE, CONF_NUMBER, CONF_SETUP_PRIORITY, \
CONF_UPDATE_INTERVAL, CONF_TYPE_ID
from esphome.core import coroutine, ID
from esphome.cpp_generator import RawExpression, add, get_variable
from esphome.cpp_types import App, GPIOPin
@coroutine
def gpio_pin_expression(conf):
"""Generate an expression for the given pin option.
This is a coroutine, you must await it with a 'yield' expression!
"""
if conf is None:
return
from esphome import pins
for key, (func, _) in pins.PIN_SCHEMA_REGISTRY.items():
if key in conf:
yield coroutine(func)(conf)
return
number = conf[CONF_NUMBER]
mode = conf[CONF_MODE]
inverted = conf.get(CONF_INVERTED)
yield GPIOPin.new(number, RawExpression(mode), inverted)
@coroutine
def register_component(var, config):
"""Register the given obj as a component.
This is a coroutine, you must await it with a 'yield' expression!
:param var: The variable representing the component.
:param config: The configuration for the component.
"""
if CONF_SETUP_PRIORITY in config:
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))
yield var
@coroutine
def register_parented(var, value):
if isinstance(value, ID):
paren = yield get_variable(value)
else:
paren = value
add(var.set_parent(paren))
def extract_registry_entry_config(registry, full_config):
# type: (Registry, ConfigType) -> RegistryEntry
key, config = next((k, v) for k, v in full_config.items() if k in registry)
return registry[key], config
@coroutine
def build_registry_entry(registry, full_config):
registry_entry, config = extract_registry_entry_config(registry, full_config)
type_id = full_config[CONF_TYPE_ID]
builder = registry_entry.coroutine_fun
yield builder(config, type_id)
@coroutine
def build_registry_list(registry, config):
actions = []
for conf in config:
action = yield build_registry_entry(registry, conf)
actions.append(action)
yield actions