Convert core components to async-def coroutine syntax (#1658)

Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Otto Winter 2021-05-23 22:10:30 +02:00 committed by GitHub
parent 514d11d46f
commit aebad04c0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 313 additions and 359 deletions

View file

@ -68,9 +68,9 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(40.0) @coroutine_with_priority(40.0)
def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.set_port(config[CONF_PORT])) cg.add(var.set_port(config[CONF_PORT]))
cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.set_password(config[CONF_PASSWORD]))
@ -90,7 +90,7 @@ def to_code(config):
conf[CONF_TRIGGER_ID], templ, conf[CONF_SERVICE], service_arg_names conf[CONF_TRIGGER_ID], templ, conf[CONF_SERVICE], service_arg_names
) )
cg.add(var.register_user_service(trigger)) cg.add(var.register_user_service(trigger))
yield automation.build_automation(trigger, func_args, conf) await automation.build_automation(trigger, func_args, conf)
cg.add_define("USE_API") cg.add_define("USE_API")
cg.add_global(api_ns.using) cg.add_global(api_ns.using)
@ -116,21 +116,21 @@ HOMEASSISTANT_SERVICE_ACTION_SCHEMA = cv.Schema(
HomeAssistantServiceCallAction, HomeAssistantServiceCallAction,
HOMEASSISTANT_SERVICE_ACTION_SCHEMA, HOMEASSISTANT_SERVICE_ACTION_SCHEMA,
) )
def homeassistant_service_to_code(config, action_id, template_arg, args): async def homeassistant_service_to_code(config, action_id, template_arg, args):
serv = yield cg.get_variable(config[CONF_ID]) serv = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, serv, False) var = cg.new_Pvariable(action_id, template_arg, serv, False)
templ = yield cg.templatable(config[CONF_SERVICE], args, None) templ = await cg.templatable(config[CONF_SERVICE], args, None)
cg.add(var.set_service(templ)) cg.add(var.set_service(templ))
for key, value in config[CONF_DATA].items(): for key, value in config[CONF_DATA].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_data(key, templ)) cg.add(var.add_data(key, templ))
for key, value in config[CONF_DATA_TEMPLATE].items(): for key, value in config[CONF_DATA_TEMPLATE].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_data_template(key, templ)) cg.add(var.add_data_template(key, templ))
for key, value in config[CONF_VARIABLES].items(): for key, value in config[CONF_VARIABLES].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_variable(key, templ)) cg.add(var.add_variable(key, templ))
yield var return var
def validate_homeassistant_event(value): def validate_homeassistant_event(value):
@ -159,21 +159,21 @@ HOMEASSISTANT_EVENT_ACTION_SCHEMA = cv.Schema(
HomeAssistantServiceCallAction, HomeAssistantServiceCallAction,
HOMEASSISTANT_EVENT_ACTION_SCHEMA, HOMEASSISTANT_EVENT_ACTION_SCHEMA,
) )
def homeassistant_event_to_code(config, action_id, template_arg, args): async def homeassistant_event_to_code(config, action_id, template_arg, args):
serv = yield cg.get_variable(config[CONF_ID]) serv = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, serv, True) var = cg.new_Pvariable(action_id, template_arg, serv, True)
templ = yield cg.templatable(config[CONF_EVENT], args, None) templ = await cg.templatable(config[CONF_EVENT], args, None)
cg.add(var.set_service(templ)) cg.add(var.set_service(templ))
for key, value in config[CONF_DATA].items(): for key, value in config[CONF_DATA].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_data(key, templ)) cg.add(var.add_data(key, templ))
for key, value in config[CONF_DATA_TEMPLATE].items(): for key, value in config[CONF_DATA_TEMPLATE].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_data_template(key, templ)) cg.add(var.add_data_template(key, templ))
for key, value in config[CONF_VARIABLES].items(): for key, value in config[CONF_VARIABLES].items():
templ = yield cg.templatable(value, args, None) templ = await cg.templatable(value, args, None)
cg.add(var.add_variable(key, templ)) cg.add(var.add_variable(key, templ))
yield var return var
HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA = cv.maybe_simple_value( HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA = cv.maybe_simple_value(
@ -190,15 +190,15 @@ HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA = cv.maybe_simple_value(
HomeAssistantServiceCallAction, HomeAssistantServiceCallAction,
HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA, HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA,
) )
def homeassistant_tag_scanned_to_code(config, action_id, template_arg, args): async def homeassistant_tag_scanned_to_code(config, action_id, template_arg, args):
serv = yield cg.get_variable(config[CONF_ID]) serv = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, serv, True) var = cg.new_Pvariable(action_id, template_arg, serv, True)
cg.add(var.set_service("esphome.tag_scanned")) cg.add(var.set_service("esphome.tag_scanned"))
templ = yield cg.templatable(config[CONF_TAG], args, cg.std_string) templ = await cg.templatable(config[CONF_TAG], args, cg.std_string)
cg.add(var.add_data("tag_id", templ)) cg.add(var.add_data("tag_id", templ))
yield var return var
@automation.register_condition("api.connected", APIConnectedCondition, {}) @automation.register_condition("api.connected", APIConnectedCondition, {})
def api_connected_to_code(config, condition_id, template_arg, args): async def api_connected_to_code(config, condition_id, template_arg, args):
yield cg.new_Pvariable(condition_id, template_arg) return cg.new_Pvariable(condition_id, template_arg)

View file

@ -11,7 +11,6 @@ from esphome.const import (
CONF_DIV_RATIO, CONF_DIV_RATIO,
CONF_CAPACITANCE, CONF_CAPACITANCE,
) )
from esphome.core import coroutine
AUTO_LOAD = ["sensor", "binary_sensor"] AUTO_LOAD = ["sensor", "binary_sensor"]
MULTI_CONF = True MULTI_CONF = True
@ -40,11 +39,10 @@ AS3935_SCHEMA = cv.Schema(
) )
@coroutine async def setup_as3935(var, config):
def setup_as3935(var, config): await cg.register_component(var, config)
yield cg.register_component(var, config)
irq_pin = yield cg.gpio_pin_expression(config[CONF_IRQ_PIN]) irq_pin = await cg.gpio_pin_expression(config[CONF_IRQ_PIN])
cg.add(var.set_irq_pin(irq_pin)) cg.add(var.set_irq_pin(irq_pin))
cg.add(var.set_indoor(config[CONF_INDOOR])) cg.add(var.set_indoor(config[CONF_INDOOR]))
cg.add(var.set_noise_level(config[CONF_NOISE_LEVEL])) cg.add(var.set_noise_level(config[CONF_NOISE_LEVEL]))

View file

@ -6,7 +6,7 @@ CODEOWNERS = ["@OttoWinter"]
@coroutine_with_priority(200.0) @coroutine_with_priority(200.0)
def to_code(config): async def to_code(config):
if CORE.is_esp32: if CORE.is_esp32:
# https://github.com/esphome/AsyncTCP/blob/master/library.json # https://github.com/esphome/AsyncTCP/blob/master/library.json
cg.add_library("esphome/AsyncTCP-esphome", "1.2.2") cg.add_library("esphome/AsyncTCP-esphome", "1.2.2")

View file

@ -51,7 +51,7 @@ from esphome.const import (
DEVICE_CLASS_VIBRATION, DEVICE_CLASS_VIBRATION,
DEVICE_CLASS_WINDOW, DEVICE_CLASS_WINDOW,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
from esphome.util import Registry from esphome.util import Registry
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
@ -381,8 +381,7 @@ BINARY_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_binary_sensor_core_(var, config):
def setup_binary_sensor_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -391,28 +390,28 @@ def setup_binary_sensor_core_(var, config):
if CONF_INVERTED in config: if CONF_INVERTED in config:
cg.add(var.set_inverted(config[CONF_INVERTED])) cg.add(var.set_inverted(config[CONF_INVERTED]))
if CONF_FILTERS in config: if CONF_FILTERS in config:
filters = yield cg.build_registry_list(FILTER_REGISTRY, config[CONF_FILTERS]) filters = await cg.build_registry_list(FILTER_REGISTRY, config[CONF_FILTERS])
cg.add(var.add_filters(filters)) cg.add(var.add_filters(filters))
for conf in config.get(CONF_ON_PRESS, []): for conf in config.get(CONF_ON_PRESS, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_RELEASE, []): for conf in config.get(CONF_ON_RELEASE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_CLICK, []): for conf in config.get(CONF_ON_CLICK, []):
trigger = cg.new_Pvariable( trigger = cg.new_Pvariable(
conf[CONF_TRIGGER_ID], var, conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH] conf[CONF_TRIGGER_ID], var, conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]
) )
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_DOUBLE_CLICK, []): for conf in config.get(CONF_ON_DOUBLE_CLICK, []):
trigger = cg.new_Pvariable( trigger = cg.new_Pvariable(
conf[CONF_TRIGGER_ID], var, conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH] conf[CONF_TRIGGER_ID], var, conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]
) )
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_MULTI_CLICK, []): for conf in config.get(CONF_ON_MULTI_CLICK, []):
timings = [] timings = []
@ -428,31 +427,29 @@ def setup_binary_sensor_core_(var, config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var, timings) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var, timings)
if CONF_INVALID_COOLDOWN in conf: if CONF_INVALID_COOLDOWN in conf:
cg.add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN])) cg.add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN]))
yield cg.register_component(trigger, conf) await cg.register_component(trigger, conf)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_STATE, []): for conf in config.get(CONF_ON_STATE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(bool, "x")], conf) await automation.build_automation(trigger, [(bool, "x")], conf)
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_binary_sensor(var, config):
def register_binary_sensor(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_binary_sensor(var)) cg.add(cg.App.register_binary_sensor(var))
yield setup_binary_sensor_core_(var, config) await setup_binary_sensor_core_(var, config)
@coroutine async def new_binary_sensor(config):
def new_binary_sensor(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME]) var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME])
yield register_binary_sensor(var, config) await register_binary_sensor(var, config)
yield var return var
BINARY_SENSOR_CONDITION_SCHEMA = maybe_simple_id( BINARY_SENSOR_CONDITION_SCHEMA = maybe_simple_id(
@ -483,6 +480,6 @@ def binary_sensor_is_off_to_code(config, condition_id, template_arg, args):
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_BINARY_SENSOR") cg.add_define("USE_BINARY_SENSOR")
cg.add_global(binary_sensor_ns.using) cg.add_global(binary_sensor_ns.using)

View file

@ -23,9 +23,9 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(64.0) @coroutine_with_priority(64.0)
def to_code(config): async def to_code(config):
paren = yield cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])
var = cg.new_Pvariable(config[CONF_ID], paren) var = cg.new_Pvariable(config[CONF_ID], paren)
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add_define("USE_CAPTIVE_PORTAL") cg.add_define("USE_CAPTIVE_PORTAL")

View file

@ -19,7 +19,7 @@ from esphome.const import (
CONF_FAN_MODE, CONF_FAN_MODE,
CONF_SWING_MODE, CONF_SWING_MODE,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -85,8 +85,7 @@ CLIMATE_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_climate_core_(var, config):
def setup_climate_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -100,15 +99,14 @@ def setup_climate_core_(var, config):
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_climate(var, config):
def register_climate(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_climate(var)) cg.add(cg.App.register_climate(var))
yield setup_climate_core_(var, config) await setup_climate_core_(var, config)
CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema( CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema(
@ -128,40 +126,40 @@ CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema(
@automation.register_action( @automation.register_action(
"climate.control", ControlAction, CLIMATE_CONTROL_ACTION_SCHEMA "climate.control", ControlAction, CLIMATE_CONTROL_ACTION_SCHEMA
) )
def climate_control_to_code(config, action_id, template_arg, args): async def climate_control_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
if CONF_MODE in config: if CONF_MODE in config:
template_ = yield cg.templatable(config[CONF_MODE], args, ClimateMode) template_ = await cg.templatable(config[CONF_MODE], args, ClimateMode)
cg.add(var.set_mode(template_)) cg.add(var.set_mode(template_))
if CONF_TARGET_TEMPERATURE in config: if CONF_TARGET_TEMPERATURE in config:
template_ = yield cg.templatable(config[CONF_TARGET_TEMPERATURE], args, float) template_ = await cg.templatable(config[CONF_TARGET_TEMPERATURE], args, float)
cg.add(var.set_target_temperature(template_)) cg.add(var.set_target_temperature(template_))
if CONF_TARGET_TEMPERATURE_LOW in config: if CONF_TARGET_TEMPERATURE_LOW in config:
template_ = yield cg.templatable( template_ = await cg.templatable(
config[CONF_TARGET_TEMPERATURE_LOW], args, float config[CONF_TARGET_TEMPERATURE_LOW], args, float
) )
cg.add(var.set_target_temperature_low(template_)) cg.add(var.set_target_temperature_low(template_))
if CONF_TARGET_TEMPERATURE_HIGH in config: if CONF_TARGET_TEMPERATURE_HIGH in config:
template_ = yield cg.templatable( template_ = await cg.templatable(
config[CONF_TARGET_TEMPERATURE_HIGH], args, float config[CONF_TARGET_TEMPERATURE_HIGH], args, float
) )
cg.add(var.set_target_temperature_high(template_)) cg.add(var.set_target_temperature_high(template_))
if CONF_AWAY in config: if CONF_AWAY in config:
template_ = yield cg.templatable(config[CONF_AWAY], args, bool) template_ = await cg.templatable(config[CONF_AWAY], args, bool)
cg.add(var.set_away(template_)) cg.add(var.set_away(template_))
if CONF_FAN_MODE in config: if CONF_FAN_MODE in config:
template_ = yield cg.templatable(config[CONF_FAN_MODE], args, ClimateFanMode) template_ = await cg.templatable(config[CONF_FAN_MODE], args, ClimateFanMode)
cg.add(var.set_fan_mode(template_)) cg.add(var.set_fan_mode(template_))
if CONF_SWING_MODE in config: if CONF_SWING_MODE in config:
template_ = yield cg.templatable( template_ = await cg.templatable(
config[CONF_SWING_MODE], args, ClimateSwingMode config[CONF_SWING_MODE], args, ClimateSwingMode
) )
cg.add(var.set_swing_mode(template_)) cg.add(var.set_swing_mode(template_))
yield var return var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_CLIMATE") cg.add_define("USE_CLIMATE")
cg.add_global(climate_ns.using) cg.add_global(climate_ns.using)

View file

@ -9,7 +9,6 @@ from esphome.components import (
) )
from esphome.components.remote_base import CONF_RECEIVER_ID, CONF_TRANSMITTER_ID from esphome.components.remote_base import CONF_RECEIVER_ID, CONF_TRANSMITTER_ID
from esphome.const import CONF_SUPPORTS_COOL, CONF_SUPPORTS_HEAT, CONF_SENSOR from esphome.const import CONF_SUPPORTS_COOL, CONF_SUPPORTS_HEAT, CONF_SENSOR
from esphome.core import coroutine
AUTO_LOAD = ["sensor", "remote_base"] AUTO_LOAD = ["sensor", "remote_base"]
CODEOWNERS = ["@glmnet"] CODEOWNERS = ["@glmnet"]
@ -39,19 +38,18 @@ CLIMATE_IR_WITH_RECEIVER_SCHEMA = CLIMATE_IR_SCHEMA.extend(
) )
@coroutine async def register_climate_ir(var, config):
def register_climate_ir(var, config): await cg.register_component(var, config)
yield cg.register_component(var, config) await climate.register_climate(var, config)
yield climate.register_climate(var, config)
cg.add(var.set_supports_cool(config[CONF_SUPPORTS_COOL])) cg.add(var.set_supports_cool(config[CONF_SUPPORTS_COOL]))
cg.add(var.set_supports_heat(config[CONF_SUPPORTS_HEAT])) cg.add(var.set_supports_heat(config[CONF_SUPPORTS_HEAT]))
if CONF_SENSOR in config: if CONF_SENSOR in config:
sens = yield cg.get_variable(config[CONF_SENSOR]) sens = await cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_sensor(sens)) cg.add(var.set_sensor(sens))
if CONF_RECEIVER_ID in config: if CONF_RECEIVER_ID in config:
receiver = yield cg.get_variable(config[CONF_RECEIVER_ID]) receiver = await cg.get_variable(config[CONF_RECEIVER_ID])
cg.add(receiver.register_listener(var)) cg.add(receiver.register_listener(var))
transmitter = yield cg.get_variable(config[CONF_TRANSMITTER_ID]) transmitter = await cg.get_variable(config[CONF_TRANSMITTER_ID])
cg.add(var.set_transmitter(transmitter)) cg.add(var.set_transmitter(transmitter))

View file

@ -14,7 +14,7 @@ from esphome.const import (
CONF_MQTT_ID, CONF_MQTT_ID,
CONF_NAME, CONF_NAME,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -73,8 +73,7 @@ COVER_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_cover_core_(var, config):
def setup_cover_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -83,15 +82,14 @@ def setup_cover_core_(var, config):
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_cover(var, config):
def register_cover(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_cover(var)) cg.add(cg.App.register_cover(var))
yield setup_cover_core_(var, config) await setup_cover_core_(var, config)
COVER_ACTION_SCHEMA = maybe_simple_id( COVER_ACTION_SCHEMA = maybe_simple_id(
@ -102,21 +100,21 @@ COVER_ACTION_SCHEMA = maybe_simple_id(
@automation.register_action("cover.open", OpenAction, COVER_ACTION_SCHEMA) @automation.register_action("cover.open", OpenAction, COVER_ACTION_SCHEMA)
def cover_open_to_code(config, action_id, template_arg, args): async def cover_open_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action("cover.close", CloseAction, COVER_ACTION_SCHEMA) @automation.register_action("cover.close", CloseAction, COVER_ACTION_SCHEMA)
def cover_close_to_code(config, action_id, template_arg, args): async def cover_close_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action("cover.stop", StopAction, COVER_ACTION_SCHEMA) @automation.register_action("cover.stop", StopAction, COVER_ACTION_SCHEMA)
def cover_stop_to_code(config, action_id, template_arg, args): async def cover_stop_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
COVER_CONTROL_ACTION_SCHEMA = cv.Schema( COVER_CONTROL_ACTION_SCHEMA = cv.Schema(
@ -131,25 +129,25 @@ COVER_CONTROL_ACTION_SCHEMA = cv.Schema(
@automation.register_action("cover.control", ControlAction, COVER_CONTROL_ACTION_SCHEMA) @automation.register_action("cover.control", ControlAction, COVER_CONTROL_ACTION_SCHEMA)
def cover_control_to_code(config, action_id, template_arg, args): async def cover_control_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
if CONF_STOP in config: if CONF_STOP in config:
template_ = yield cg.templatable(config[CONF_STOP], args, bool) template_ = await cg.templatable(config[CONF_STOP], args, bool)
cg.add(var.set_stop(template_)) cg.add(var.set_stop(template_))
if CONF_STATE in config: if CONF_STATE in config:
template_ = yield cg.templatable(config[CONF_STATE], args, float) template_ = await cg.templatable(config[CONF_STATE], args, float)
cg.add(var.set_position(template_)) cg.add(var.set_position(template_))
if CONF_POSITION in config: if CONF_POSITION in config:
template_ = yield cg.templatable(config[CONF_POSITION], args, float) template_ = await cg.templatable(config[CONF_POSITION], args, float)
cg.add(var.set_position(template_)) cg.add(var.set_position(template_))
if CONF_TILT in config: if CONF_TILT in config:
template_ = yield cg.templatable(config[CONF_TILT], args, float) template_ = await cg.templatable(config[CONF_TILT], args, float)
cg.add(var.set_tilt(template_)) cg.add(var.set_tilt(template_))
yield var return var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_COVER") cg.add_define("USE_COVER")
cg.add_global(cover_ns.using) cg.add_global(cover_ns.using)

View file

@ -3,7 +3,7 @@ import esphome.config_validation as cv
from esphome import core, automation from esphome import core, automation
from esphome.automation import maybe_simple_id from esphome.automation import maybe_simple_id
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES, CONF_PAGE_ID, CONF_ROTATION from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES, CONF_PAGE_ID, CONF_ROTATION
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine_with_priority
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -60,14 +60,13 @@ FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
) )
@coroutine async def setup_display_core_(var, config):
def setup_display_core_(var, config):
if CONF_ROTATION in config: if CONF_ROTATION in config:
cg.add(var.set_rotation(DISPLAY_ROTATIONS[config[CONF_ROTATION]])) cg.add(var.set_rotation(DISPLAY_ROTATIONS[config[CONF_ROTATION]]))
if CONF_PAGES in config: if CONF_PAGES in config:
pages = [] pages = []
for conf in config[CONF_PAGES]: for conf in config[CONF_PAGES]:
lambda_ = yield cg.process_lambda( lambda_ = await cg.process_lambda(
conf[CONF_LAMBDA], [(DisplayBufferRef, "it")], return_type=cg.void conf[CONF_LAMBDA], [(DisplayBufferRef, "it")], return_type=cg.void
) )
page = cg.new_Pvariable(conf[CONF_ID], lambda_) page = cg.new_Pvariable(conf[CONF_ID], lambda_)
@ -75,9 +74,8 @@ def setup_display_core_(var, config):
cg.add(var.set_pages(pages)) cg.add(var.set_pages(pages))
@coroutine async def register_display(var, config):
def register_display(var, config): await setup_display_core_(var, config)
yield setup_display_core_(var, config)
@automation.register_action( @automation.register_action(
@ -89,15 +87,15 @@ def register_display(var, config):
} }
), ),
) )
def display_page_show_to_code(config, action_id, template_arg, args): async def display_page_show_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg) var = cg.new_Pvariable(action_id, template_arg)
if isinstance(config[CONF_ID], core.Lambda): if isinstance(config[CONF_ID], core.Lambda):
template_ = yield cg.templatable(config[CONF_ID], args, DisplayPagePtr) template_ = await cg.templatable(config[CONF_ID], args, DisplayPagePtr)
cg.add(var.set_page(template_)) cg.add(var.set_page(template_))
else: else:
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
cg.add(var.set_page(paren)) cg.add(var.set_page(paren))
yield var return var
@automation.register_action( @automation.register_action(
@ -109,9 +107,9 @@ def display_page_show_to_code(config, action_id, template_arg, args):
} }
), ),
) )
def display_page_show_next_to_code(config, action_id, template_arg, args): async def display_page_show_next_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action( @automation.register_action(
@ -123,9 +121,9 @@ def display_page_show_next_to_code(config, action_id, template_arg, args):
} }
), ),
) )
def display_page_show_previous_to_code(config, action_id, template_arg, args): async def display_page_show_previous_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_condition( @automation.register_condition(
@ -149,5 +147,5 @@ def display_is_displaying_page_to_code(config, condition_id, template_arg, args)
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_global(display_ns.using) cg.add_global(display_ns.using)

View file

@ -18,7 +18,7 @@ from esphome.const import (
CONF_ON_TURN_ON, CONF_ON_TURN_ON,
CONF_TRIGGER_ID, CONF_TRIGGER_ID,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -64,15 +64,14 @@ FAN_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_fan_core_(var, config):
def setup_fan_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
if CONF_OSCILLATION_STATE_TOPIC in config: if CONF_OSCILLATION_STATE_TOPIC in config:
cg.add( cg.add(
@ -95,26 +94,24 @@ def setup_fan_core_(var, config):
for conf in config.get(CONF_ON_TURN_ON, []): for conf in config.get(CONF_ON_TURN_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TURN_OFF, []): for conf in config.get(CONF_ON_TURN_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
@coroutine async def register_fan(var, config):
def register_fan(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_fan(var)) cg.add(cg.App.register_fan(var))
yield cg.register_component(var, config) await cg.register_component(var, config)
yield setup_fan_core_(var, config) await setup_fan_core_(var, config)
@coroutine async def create_fan_state(config):
def create_fan_state(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield register_fan(var, config) await register_fan(var, config)
yield var return var
FAN_ACTION_SCHEMA = maybe_simple_id( FAN_ACTION_SCHEMA = maybe_simple_id(
@ -125,15 +122,15 @@ FAN_ACTION_SCHEMA = maybe_simple_id(
@automation.register_action("fan.toggle", ToggleAction, FAN_ACTION_SCHEMA) @automation.register_action("fan.toggle", ToggleAction, FAN_ACTION_SCHEMA)
def fan_toggle_to_code(config, action_id, template_arg, args): async def fan_toggle_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action("fan.turn_off", TurnOffAction, FAN_ACTION_SCHEMA) @automation.register_action("fan.turn_off", TurnOffAction, FAN_ACTION_SCHEMA)
def fan_turn_off_to_code(config, action_id, template_arg, args): async def fan_turn_off_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action( @automation.register_action(
@ -147,19 +144,19 @@ def fan_turn_off_to_code(config, action_id, template_arg, args):
} }
), ),
) )
def fan_turn_on_to_code(config, action_id, template_arg, args): async def fan_turn_on_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
if CONF_OSCILLATING in config: if CONF_OSCILLATING in config:
template_ = yield cg.templatable(config[CONF_OSCILLATING], args, bool) template_ = await cg.templatable(config[CONF_OSCILLATING], args, bool)
cg.add(var.set_oscillating(template_)) cg.add(var.set_oscillating(template_))
if CONF_SPEED in config: if CONF_SPEED in config:
template_ = yield cg.templatable(config[CONF_SPEED], args, int) template_ = await cg.templatable(config[CONF_SPEED], args, int)
cg.add(var.set_speed(template_)) cg.add(var.set_speed(template_))
yield var return var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_FAN") cg.add_define("USE_FAN")
cg.add_global(fan_ns.using) cg.add_global(fan_ns.using)

View file

@ -29,7 +29,7 @@ CONFIG_SCHEMA = cv.Schema(
# Run with low priority so that namespaces are registered first # Run with low priority so that namespaces are registered first
@coroutine_with_priority(-100.0) @coroutine_with_priority(-100.0)
def to_code(config): async def to_code(config):
type_ = cg.RawExpression(config[CONF_TYPE]) type_ = cg.RawExpression(config[CONF_TYPE])
template_args = cg.TemplateArguments(type_) template_args = cg.TemplateArguments(type_)
res_type = GlobalsComponent.template(template_args) res_type = GlobalsComponent.template(template_args)
@ -40,7 +40,7 @@ def to_code(config):
rhs = GlobalsComponent.new(template_args, initial_value) rhs = GlobalsComponent.new(template_args, initial_value)
glob = cg.Pvariable(config[CONF_ID], rhs, res_type) glob = cg.Pvariable(config[CONF_ID], rhs, res_type)
yield cg.register_component(glob, config) await cg.register_component(glob, config)
if config[CONF_RESTORE_VALUE]: if config[CONF_RESTORE_VALUE]:
value = config[CONF_ID].id value = config[CONF_ID].id
@ -60,12 +60,12 @@ def to_code(config):
} }
), ),
) )
def globals_set_to_code(config, action_id, template_arg, args): async def globals_set_to_code(config, action_id, template_arg, args):
full_id, paren = yield cg.get_variable_with_full_id(config[CONF_ID]) full_id, paren = await cg.get_variable_with_full_id(config[CONF_ID])
template_arg = cg.TemplateArguments(full_id.type, *template_arg) template_arg = cg.TemplateArguments(full_id.type, *template_arg)
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
templ = yield cg.templatable( templ = await cg.templatable(
config[CONF_VALUE], args, None, to_exp=cg.RawExpression config[CONF_VALUE], args, None, to_exp=cg.RawExpression
) )
cg.add(var.set_value(templ)) cg.add(var.set_value(templ))
yield var return var

View file

@ -12,7 +12,7 @@ from esphome.const import (
CONF_I2C_ID, CONF_I2C_ID,
CONF_MULTIPLEXER, CONF_MULTIPLEXER,
) )
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine_with_priority
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
i2c_ns = cg.esphome_ns.namespace("i2c") i2c_ns = cg.esphome_ns.namespace("i2c")
@ -42,10 +42,10 @@ I2CMULTIPLEXER_SCHEMA = cv.Schema(
@coroutine_with_priority(1.0) @coroutine_with_priority(1.0)
def to_code(config): async def to_code(config):
cg.add_global(i2c_ns.using) cg.add_global(i2c_ns.using)
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.set_sda_pin(config[CONF_SDA])) cg.add(var.set_sda_pin(config[CONF_SDA]))
cg.add(var.set_scl_pin(config[CONF_SCL])) cg.add(var.set_scl_pin(config[CONF_SCL]))
@ -72,19 +72,18 @@ def i2c_device_schema(default_address):
return cv.Schema(schema) return cv.Schema(schema)
@coroutine async def register_i2c_device(var, config):
def register_i2c_device(var, config):
"""Register an i2c device with the given config. """Register an i2c device with the given config.
Sets the i2c bus to use and the i2c address. Sets the i2c bus to use and the i2c address.
This is a coroutine, you need to await it with a 'yield' expression! This is a coroutine, you need to await it with a 'yield' expression!
""" """
parent = yield cg.get_variable(config[CONF_I2C_ID]) parent = await cg.get_variable(config[CONF_I2C_ID])
cg.add(var.set_i2c_parent(parent)) cg.add(var.set_i2c_parent(parent))
cg.add(var.set_i2c_address(config[CONF_ADDRESS])) cg.add(var.set_i2c_address(config[CONF_ADDRESS]))
if CONF_MULTIPLEXER in config: if CONF_MULTIPLEXER in config:
multiplexer = yield cg.get_variable(config[CONF_MULTIPLEXER][CONF_ID]) multiplexer = await cg.get_variable(config[CONF_MULTIPLEXER][CONF_ID])
cg.add( cg.add(
var.set_i2c_multiplexer(multiplexer, config[CONF_MULTIPLEXER][CONF_CHANNEL]) var.set_i2c_multiplexer(multiplexer, config[CONF_MULTIPLEXER][CONF_CHANNEL])
) )

View file

@ -6,7 +6,7 @@ json_ns = cg.esphome_ns.namespace("json")
@coroutine_with_priority(1.0) @coroutine_with_priority(1.0)
def to_code(config): async def to_code(config):
cg.add_library("ArduinoJson-esphomelib", "5.13.3") cg.add_library("ArduinoJson-esphomelib", "5.13.3")
cg.add_define("USE_JSON") cg.add_define("USE_JSON")
cg.add_global(json_ns.using) cg.add_global(json_ns.using)

View file

@ -17,7 +17,7 @@ from esphome.const import (
CONF_ON_TURN_ON, CONF_ON_TURN_ON,
CONF_TRIGGER_ID, CONF_TRIGGER_ID,
) )
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine_with_priority
from .automation import light_control_to_code # noqa from .automation import light_control_to_code # noqa
from .effects import ( from .effects import (
validate_effects, validate_effects,
@ -102,8 +102,7 @@ ADDRESSABLE_LIGHT_SCHEMA = RGB_LIGHT_SCHEMA.extend(
) )
@coroutine async def setup_light_core_(light_var, output_var, config):
def setup_light_core_(light_var, output_var, config):
cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE])) cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(light_var.set_internal(config[CONF_INTERNAL])) cg.add(light_var.set_internal(config[CONF_INTERNAL]))
@ -115,39 +114,38 @@ def setup_light_core_(light_var, output_var, config):
) )
if CONF_GAMMA_CORRECT in config: if CONF_GAMMA_CORRECT in config:
cg.add(light_var.set_gamma_correct(config[CONF_GAMMA_CORRECT])) cg.add(light_var.set_gamma_correct(config[CONF_GAMMA_CORRECT]))
effects = yield cg.build_registry_list( effects = await cg.build_registry_list(
EFFECTS_REGISTRY, config.get(CONF_EFFECTS, []) EFFECTS_REGISTRY, config.get(CONF_EFFECTS, [])
) )
cg.add(light_var.add_effects(effects)) cg.add(light_var.add_effects(effects))
for conf in config.get(CONF_ON_TURN_ON, []): for conf in config.get(CONF_ON_TURN_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var)
yield auto.build_automation(trigger, [], conf) await auto.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TURN_OFF, []): for conf in config.get(CONF_ON_TURN_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var)
yield auto.build_automation(trigger, [], conf) await auto.build_automation(trigger, [], conf)
if CONF_COLOR_CORRECT in config: if CONF_COLOR_CORRECT in config:
cg.add(output_var.set_correction(*config[CONF_COLOR_CORRECT])) cg.add(output_var.set_correction(*config[CONF_COLOR_CORRECT]))
if CONF_POWER_SUPPLY in config: if CONF_POWER_SUPPLY in config:
var_ = yield cg.get_variable(config[CONF_POWER_SUPPLY]) var_ = await cg.get_variable(config[CONF_POWER_SUPPLY])
cg.add(output_var.set_power_supply(var_)) cg.add(output_var.set_power_supply(var_))
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], light_var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], light_var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_light(output_var, config):
def register_light(output_var, config):
light_var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], output_var) light_var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], output_var)
cg.add(cg.App.register_light(light_var)) cg.add(cg.App.register_light(light_var))
yield cg.register_component(light_var, config) await cg.register_component(light_var, config)
yield setup_light_core_(light_var, output_var, config) await setup_light_core_(light_var, output_var, config)
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_LIGHT") cg.add_define("USE_LIGHT")
cg.add_global(light_ns.using) cg.add_global(light_ns.using)

View file

@ -127,7 +127,7 @@ CONFIG_SCHEMA = cv.All(
@coroutine_with_priority(90.0) @coroutine_with_priority(90.0)
def to_code(config): async def to_code(config):
baud_rate = config[CONF_BAUD_RATE] baud_rate = config[CONF_BAUD_RATE]
rhs = Logger.new( rhs = Logger.new(
baud_rate, baud_rate,
@ -177,13 +177,13 @@ def to_code(config):
cg.add_build_flag("-DUSE_STORE_LOG_STR_IN_FLASH") cg.add_build_flag("-DUSE_STORE_LOG_STR_IN_FLASH")
# Register at end for safe mode # Register at end for safe mode
yield cg.register_component(log, config) await cg.register_component(log, config)
for conf in config.get(CONF_ON_MESSAGE, []): for conf in config.get(CONF_ON_MESSAGE, []):
trigger = cg.new_Pvariable( trigger = cg.new_Pvariable(
conf[CONF_TRIGGER_ID], log, LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL]) conf[CONF_TRIGGER_ID], log, LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL])
) )
yield automation.build_automation( await automation.build_automation(
trigger, trigger,
[ [
(cg.int_, "level"), (cg.int_, "level"),
@ -242,11 +242,11 @@ LOGGER_LOG_ACTION_SCHEMA = cv.All(
@automation.register_action(CONF_LOGGER_LOG, LambdaAction, LOGGER_LOG_ACTION_SCHEMA) @automation.register_action(CONF_LOGGER_LOG, LambdaAction, LOGGER_LOG_ACTION_SCHEMA)
def logger_log_action_to_code(config, action_id, template_arg, args): async def logger_log_action_to_code(config, action_id, template_arg, args):
esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]]
args_ = [cg.RawExpression(str(x)) for x in config[CONF_ARGS]] args_ = [cg.RawExpression(str(x)) for x in config[CONF_ARGS]]
text = str(cg.statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args_))) text = str(cg.statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args_)))
lambda_ = yield cg.process_lambda(Lambda(text), args, return_type=cg.void) lambda_ = await cg.process_lambda(Lambda(text), args, return_type=cg.void)
yield cg.new_Pvariable(action_id, template_arg, lambda_) return cg.new_Pvariable(action_id, template_arg, lambda_)

View file

@ -37,7 +37,7 @@ from esphome.const import (
CONF_USERNAME, CONF_USERNAME,
CONF_WILL_MESSAGE, CONF_WILL_MESSAGE,
) )
from esphome.core import coroutine_with_priority, coroutine, CORE from esphome.core import coroutine_with_priority, CORE
DEPENDENCIES = ["network"] DEPENDENCIES = ["network"]
AUTO_LOAD = ["json", "async_tcp"] AUTO_LOAD = ["json", "async_tcp"]
@ -207,9 +207,9 @@ def exp_mqtt_message(config):
@coroutine_with_priority(40.0) @coroutine_with_priority(40.0)
def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
# https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json # https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json
cg.add_library("AsyncMqttClient-esphome", "0.8.4") cg.add_library("AsyncMqttClient-esphome", "0.8.4")
@ -279,12 +279,12 @@ def to_code(config):
cg.add(trig.set_qos(conf[CONF_QOS])) cg.add(trig.set_qos(conf[CONF_QOS]))
if CONF_PAYLOAD in conf: if CONF_PAYLOAD in conf:
cg.add(trig.set_payload(conf[CONF_PAYLOAD])) cg.add(trig.set_payload(conf[CONF_PAYLOAD]))
yield cg.register_component(trig, conf) await cg.register_component(trig, conf)
yield automation.build_automation(trig, [(cg.std_string, "x")], conf) await automation.build_automation(trig, [(cg.std_string, "x")], conf)
for conf in config.get(CONF_ON_JSON_MESSAGE, []): for conf in config.get(CONF_ON_JSON_MESSAGE, []):
trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_TOPIC], conf[CONF_QOS]) trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_TOPIC], conf[CONF_QOS])
yield automation.build_automation(trig, [(cg.JsonObjectConstRef, "x")], conf) await automation.build_automation(trig, [(cg.JsonObjectConstRef, "x")], conf)
MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema( MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema(
@ -301,19 +301,19 @@ MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema(
@automation.register_action( @automation.register_action(
"mqtt.publish", MQTTPublishAction, MQTT_PUBLISH_ACTION_SCHEMA "mqtt.publish", MQTTPublishAction, MQTT_PUBLISH_ACTION_SCHEMA
) )
def mqtt_publish_action_to_code(config, action_id, template_arg, args): async def mqtt_publish_action_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = yield cg.templatable(config[CONF_TOPIC], args, cg.std_string) template_ = await cg.templatable(config[CONF_TOPIC], args, cg.std_string)
cg.add(var.set_topic(template_)) cg.add(var.set_topic(template_))
template_ = yield cg.templatable(config[CONF_PAYLOAD], args, cg.std_string) template_ = await cg.templatable(config[CONF_PAYLOAD], args, cg.std_string)
cg.add(var.set_payload(template_)) cg.add(var.set_payload(template_))
template_ = yield cg.templatable(config[CONF_QOS], args, cg.uint8) template_ = await cg.templatable(config[CONF_QOS], args, cg.uint8)
cg.add(var.set_qos(template_)) cg.add(var.set_qos(template_))
template_ = yield cg.templatable(config[CONF_RETAIN], args, bool) template_ = await cg.templatable(config[CONF_RETAIN], args, bool)
cg.add(var.set_retain(template_)) cg.add(var.set_retain(template_))
yield var return var
MQTT_PUBLISH_JSON_ACTION_SCHEMA = cv.Schema( MQTT_PUBLISH_JSON_ACTION_SCHEMA = cv.Schema(
@ -330,20 +330,20 @@ MQTT_PUBLISH_JSON_ACTION_SCHEMA = cv.Schema(
@automation.register_action( @automation.register_action(
"mqtt.publish_json", MQTTPublishJsonAction, MQTT_PUBLISH_JSON_ACTION_SCHEMA "mqtt.publish_json", MQTTPublishJsonAction, MQTT_PUBLISH_JSON_ACTION_SCHEMA
) )
def mqtt_publish_json_action_to_code(config, action_id, template_arg, args): async def mqtt_publish_json_action_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = yield cg.templatable(config[CONF_TOPIC], args, cg.std_string) template_ = await cg.templatable(config[CONF_TOPIC], args, cg.std_string)
cg.add(var.set_topic(template_)) cg.add(var.set_topic(template_))
args_ = args + [(cg.JsonObjectRef, "root")] args_ = args + [(cg.JsonObjectRef, "root")]
lambda_ = yield cg.process_lambda(config[CONF_PAYLOAD], args_, return_type=cg.void) lambda_ = await cg.process_lambda(config[CONF_PAYLOAD], args_, return_type=cg.void)
cg.add(var.set_payload(lambda_)) cg.add(var.set_payload(lambda_))
template_ = yield cg.templatable(config[CONF_QOS], args, cg.uint8) template_ = await cg.templatable(config[CONF_QOS], args, cg.uint8)
cg.add(var.set_qos(template_)) cg.add(var.set_qos(template_))
template_ = yield cg.templatable(config[CONF_RETAIN], args, bool) template_ = await cg.templatable(config[CONF_RETAIN], args, bool)
cg.add(var.set_retain(template_)) cg.add(var.set_retain(template_))
yield var return var
def get_default_topic_for(data, component_type, name, suffix): def get_default_topic_for(data, component_type, name, suffix):
@ -356,9 +356,8 @@ def get_default_topic_for(data, component_type, name, suffix):
) )
@coroutine async def register_mqtt_component(var, config):
def register_mqtt_component(var, config): await cg.register_component(var, {})
yield cg.register_component(var, {})
if CONF_RETAIN in config: if CONF_RETAIN in config:
cg.add(var.set_retain(config[CONF_RETAIN])) cg.add(var.set_retain(config[CONF_RETAIN]))
@ -391,6 +390,6 @@ def register_mqtt_component(var, config):
} }
), ),
) )
def mqtt_connected_to_code(config, condition_id, template_arg, args): async def mqtt_connected_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(condition_id, template_arg, paren) return cg.new_Pvariable(condition_id, template_arg, paren)

View file

@ -11,7 +11,7 @@ from esphome.const import (
CONF_MIN_POWER, CONF_MIN_POWER,
CONF_POWER_SUPPLY, CONF_POWER_SUPPLY,
) )
from esphome.core import CORE, coroutine from esphome.core import CORE
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
@ -43,12 +43,11 @@ TurnOnAction = output_ns.class_("TurnOnAction", automation.Action)
SetLevelAction = output_ns.class_("SetLevelAction", automation.Action) SetLevelAction = output_ns.class_("SetLevelAction", automation.Action)
@coroutine async def setup_output_platform_(obj, config):
def setup_output_platform_(obj, config):
if CONF_INVERTED in config: if CONF_INVERTED in config:
cg.add(obj.set_inverted(config[CONF_INVERTED])) cg.add(obj.set_inverted(config[CONF_INVERTED]))
if CONF_POWER_SUPPLY in config: if CONF_POWER_SUPPLY in config:
power_supply_ = yield cg.get_variable(config[CONF_POWER_SUPPLY]) power_supply_ = await cg.get_variable(config[CONF_POWER_SUPPLY])
cg.add(obj.set_power_supply(power_supply_)) cg.add(obj.set_power_supply(power_supply_))
if CONF_MAX_POWER in config: if CONF_MAX_POWER in config:
cg.add(obj.set_max_power(config[CONF_MAX_POWER])) cg.add(obj.set_max_power(config[CONF_MAX_POWER]))
@ -56,11 +55,10 @@ def setup_output_platform_(obj, config):
cg.add(obj.set_min_power(config[CONF_MIN_POWER])) cg.add(obj.set_min_power(config[CONF_MIN_POWER]))
@coroutine async def register_output(var, config):
def register_output(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
yield setup_output_platform_(var, config) await setup_output_platform_(var, config)
BINARY_OUTPUT_ACTION_SCHEMA = maybe_simple_id( BINARY_OUTPUT_ACTION_SCHEMA = maybe_simple_id(
@ -71,17 +69,17 @@ BINARY_OUTPUT_ACTION_SCHEMA = maybe_simple_id(
@automation.register_action("output.turn_on", TurnOnAction, BINARY_OUTPUT_ACTION_SCHEMA) @automation.register_action("output.turn_on", TurnOnAction, BINARY_OUTPUT_ACTION_SCHEMA)
def output_turn_on_to_code(config, action_id, template_arg, args): async def output_turn_on_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action( @automation.register_action(
"output.turn_off", TurnOffAction, BINARY_OUTPUT_ACTION_SCHEMA "output.turn_off", TurnOffAction, BINARY_OUTPUT_ACTION_SCHEMA
) )
def output_turn_off_to_code(config, action_id, template_arg, args): async def output_turn_off_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action( @automation.register_action(
@ -94,12 +92,12 @@ def output_turn_off_to_code(config, action_id, template_arg, args):
} }
), ),
) )
def output_set_level_to_code(config, action_id, template_arg, args): async def output_set_level_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = yield cg.templatable(config[CONF_LEVEL], args, float) template_ = await cg.templatable(config[CONF_LEVEL], args, float)
cg.add(var.set_level(template_)) cg.add(var.set_level(template_))
yield var return var
def to_code(config): def to_code(config):

View file

@ -45,7 +45,7 @@ from esphome.const import (
DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLTAGE, DEVICE_CLASS_VOLTAGE,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
from esphome.util import Registry from esphome.util import Registry
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
@ -441,13 +441,11 @@ def calibrate_polynomial_filter_to_code(config, filter_id):
yield cg.new_Pvariable(filter_id, res) yield cg.new_Pvariable(filter_id, res)
@coroutine async def build_filters(config):
def build_filters(config): return await cg.build_registry_list(FILTER_REGISTRY, config)
yield cg.build_registry_list(FILTER_REGISTRY, config)
@coroutine async def setup_sensor_core_(var, config):
def setup_sensor_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -461,29 +459,29 @@ def setup_sensor_core_(var, config):
cg.add(var.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS])) cg.add(var.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
cg.add(var.set_force_update(config[CONF_FORCE_UPDATE])) cg.add(var.set_force_update(config[CONF_FORCE_UPDATE]))
if config.get(CONF_FILTERS): # must exist and not be empty if config.get(CONF_FILTERS): # must exist and not be empty
filters = yield build_filters(config[CONF_FILTERS]) filters = await build_filters(config[CONF_FILTERS])
cg.add(var.set_filters(filters)) cg.add(var.set_filters(filters))
for conf in config.get(CONF_ON_VALUE, []): for conf in config.get(CONF_ON_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(float, "x")], conf) await automation.build_automation(trigger, [(float, "x")], conf)
for conf in config.get(CONF_ON_RAW_VALUE, []): for conf in config.get(CONF_ON_RAW_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(float, "x")], conf) await automation.build_automation(trigger, [(float, "x")], conf)
for conf in config.get(CONF_ON_VALUE_RANGE, []): for conf in config.get(CONF_ON_VALUE_RANGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield cg.register_component(trigger, conf) await cg.register_component(trigger, conf)
if CONF_ABOVE in conf: if CONF_ABOVE in conf:
template_ = yield cg.templatable(conf[CONF_ABOVE], [(float, "x")], float) template_ = await cg.templatable(conf[CONF_ABOVE], [(float, "x")], float)
cg.add(trigger.set_min(template_)) cg.add(trigger.set_min(template_))
if CONF_BELOW in conf: if CONF_BELOW in conf:
template_ = yield cg.templatable(conf[CONF_BELOW], [(float, "x")], float) template_ = await cg.templatable(conf[CONF_BELOW], [(float, "x")], float)
cg.add(trigger.set_max(template_)) cg.add(trigger.set_max(template_))
yield automation.build_automation(trigger, [(float, "x")], conf) await automation.build_automation(trigger, [(float, "x")], conf)
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
if CONF_EXPIRE_AFTER in config: if CONF_EXPIRE_AFTER in config:
if config[CONF_EXPIRE_AFTER] is None: if config[CONF_EXPIRE_AFTER] is None:
@ -492,19 +490,17 @@ def setup_sensor_core_(var, config):
cg.add(mqtt_.set_expire_after(config[CONF_EXPIRE_AFTER])) cg.add(mqtt_.set_expire_after(config[CONF_EXPIRE_AFTER]))
@coroutine async def register_sensor(var, config):
def register_sensor(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_sensor(var)) cg.add(cg.App.register_sensor(var))
yield setup_sensor_core_(var, config) await setup_sensor_core_(var, config)
@coroutine async def new_sensor(config):
def new_sensor(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield register_sensor(var, config) await register_sensor(var, config)
yield var return var
SENSOR_IN_RANGE_CONDITION_SCHEMA = cv.All( SENSOR_IN_RANGE_CONDITION_SCHEMA = cv.All(
@ -520,8 +516,8 @@ SENSOR_IN_RANGE_CONDITION_SCHEMA = cv.All(
@automation.register_condition( @automation.register_condition(
"sensor.in_range", SensorInRangeCondition, SENSOR_IN_RANGE_CONDITION_SCHEMA "sensor.in_range", SensorInRangeCondition, SENSOR_IN_RANGE_CONDITION_SCHEMA
) )
def sensor_in_range_to_code(config, condition_id, template_arg, args): async def sensor_in_range_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(condition_id, template_arg, paren) var = cg.new_Pvariable(condition_id, template_arg, paren)
if CONF_ABOVE in config: if CONF_ABOVE in config:
@ -529,7 +525,7 @@ def sensor_in_range_to_code(config, condition_id, template_arg, args):
if CONF_BELOW in config: if CONF_BELOW in config:
cg.add(var.set_max(config[CONF_BELOW])) cg.add(var.set_max(config[CONF_BELOW]))
yield var return var
def _mean(xs): def _mean(xs):
@ -618,6 +614,6 @@ def _lstsq(a, b):
@coroutine_with_priority(40.0) @coroutine_with_priority(40.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_SENSOR") cg.add_define("USE_SENSOR")
cg.add_global(sensor_ns.using) cg.add_global(sensor_ns.using)

View file

@ -9,7 +9,7 @@ from esphome.const import (
CONF_SPI_ID, CONF_SPI_ID,
CONF_CS_PIN, CONF_CS_PIN,
) )
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine_with_priority
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
spi_ns = cg.esphome_ns.namespace("spi") spi_ns = cg.esphome_ns.namespace("spi")
@ -31,18 +31,18 @@ CONFIG_SCHEMA = cv.All(
@coroutine_with_priority(1.0) @coroutine_with_priority(1.0)
def to_code(config): async def to_code(config):
cg.add_global(spi_ns.using) cg.add_global(spi_ns.using)
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
clk = yield cg.gpio_pin_expression(config[CONF_CLK_PIN]) clk = await cg.gpio_pin_expression(config[CONF_CLK_PIN])
cg.add(var.set_clk(clk)) cg.add(var.set_clk(clk))
if CONF_MISO_PIN in config: if CONF_MISO_PIN in config:
miso = yield cg.gpio_pin_expression(config[CONF_MISO_PIN]) miso = await cg.gpio_pin_expression(config[CONF_MISO_PIN])
cg.add(var.set_miso(miso)) cg.add(var.set_miso(miso))
if CONF_MOSI_PIN in config: if CONF_MOSI_PIN in config:
mosi = yield cg.gpio_pin_expression(config[CONF_MOSI_PIN]) mosi = await cg.gpio_pin_expression(config[CONF_MOSI_PIN])
cg.add(var.set_mosi(mosi)) cg.add(var.set_mosi(mosi))
@ -61,10 +61,9 @@ def spi_device_schema(cs_pin_required=True):
return cv.Schema(schema) return cv.Schema(schema)
@coroutine async def register_spi_device(var, config):
def register_spi_device(var, config): parent = await cg.get_variable(config[CONF_SPI_ID])
parent = yield cg.get_variable(config[CONF_SPI_ID])
cg.add(var.set_spi_parent(parent)) cg.add(var.set_spi_parent(parent))
if CONF_CS_PIN in config: if CONF_CS_PIN in config:
pin = yield cg.gpio_pin_expression(config[CONF_CS_PIN]) pin = await cg.gpio_pin_expression(config[CONF_CS_PIN])
cg.add(var.set_cs_pin(pin)) cg.add(var.set_cs_pin(pin))

View file

@ -16,10 +16,10 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(80.0) @coroutine_with_priority(80.0)
def to_code(config): async def to_code(config):
pin = yield cg.gpio_pin_expression(config[CONF_PIN]) pin = await cg.gpio_pin_expression(config[CONF_PIN])
rhs = StatusLED.new(pin) rhs = StatusLED.new(pin)
var = cg.Pvariable(config[CONF_ID], rhs) var = cg.Pvariable(config[CONF_ID], rhs)
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.pre_setup()) cg.add(var.pre_setup())
cg.add_define("USE_STATUS_LED") cg.add_define("USE_STATUS_LED")

View file

@ -14,7 +14,7 @@ from esphome.const import (
CONF_MQTT_ID, CONF_MQTT_ID,
CONF_NAME, CONF_NAME,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -57,8 +57,7 @@ SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_switch_core_(var, config):
def setup_switch_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -68,22 +67,21 @@ def setup_switch_core_(var, config):
cg.add(var.set_inverted(config[CONF_INVERTED])) cg.add(var.set_inverted(config[CONF_INVERTED]))
for conf in config.get(CONF_ON_TURN_ON, []): for conf in config.get(CONF_ON_TURN_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TURN_OFF, []): for conf in config.get(CONF_ON_TURN_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_switch(var, config):
def register_switch(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_switch(var)) cg.add(cg.App.register_switch(var))
yield setup_switch_core_(var, config) await setup_switch_core_(var, config)
SWITCH_ACTION_SCHEMA = maybe_simple_id( SWITCH_ACTION_SCHEMA = maybe_simple_id(
@ -96,24 +94,24 @@ SWITCH_ACTION_SCHEMA = maybe_simple_id(
@automation.register_action("switch.toggle", ToggleAction, SWITCH_ACTION_SCHEMA) @automation.register_action("switch.toggle", ToggleAction, SWITCH_ACTION_SCHEMA)
@automation.register_action("switch.turn_off", TurnOffAction, SWITCH_ACTION_SCHEMA) @automation.register_action("switch.turn_off", TurnOffAction, SWITCH_ACTION_SCHEMA)
@automation.register_action("switch.turn_on", TurnOnAction, SWITCH_ACTION_SCHEMA) @automation.register_action("switch.turn_on", TurnOnAction, SWITCH_ACTION_SCHEMA)
def switch_toggle_to_code(config, action_id, template_arg, args): async def switch_toggle_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_condition("switch.is_on", SwitchCondition, SWITCH_ACTION_SCHEMA) @automation.register_condition("switch.is_on", SwitchCondition, SWITCH_ACTION_SCHEMA)
def switch_is_on_to_code(config, condition_id, template_arg, args): async def switch_is_on_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(condition_id, template_arg, paren, True) return cg.new_Pvariable(condition_id, template_arg, paren, True)
@automation.register_condition("switch.is_off", SwitchCondition, SWITCH_ACTION_SCHEMA) @automation.register_condition("switch.is_off", SwitchCondition, SWITCH_ACTION_SCHEMA)
def switch_is_off_to_code(config, condition_id, template_arg, args): async def switch_is_off_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(condition_id, template_arg, paren, False) return cg.new_Pvariable(condition_id, template_arg, paren, False)
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_global(switch_ns.using) cg.add_global(switch_ns.using)
cg.add_define("USE_SWITCH") cg.add_define("USE_SWITCH")

View file

@ -12,7 +12,7 @@ from esphome.const import (
CONF_NAME, CONF_NAME,
CONF_STATE, CONF_STATE,
) )
from esphome.core import CORE, coroutine, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
@ -46,8 +46,7 @@ TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend(
) )
@coroutine async def setup_text_sensor_core_(var, config):
def setup_text_sensor_core_(var, config):
cg.add(var.set_name(config[CONF_NAME])) cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config: if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL])) cg.add(var.set_internal(config[CONF_INTERNAL]))
@ -56,23 +55,22 @@ def setup_text_sensor_core_(var, config):
for conf in config.get(CONF_ON_VALUE, []): for conf in config.get(CONF_ON_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.std_string, "x")], conf) await automation.build_automation(trigger, [(cg.std_string, "x")], conf)
if CONF_MQTT_ID in config: if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
@coroutine async def register_text_sensor(var, config):
def register_text_sensor(var, config):
if not CORE.has_id(config[CONF_ID]): if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var) var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_text_sensor(var)) cg.add(cg.App.register_text_sensor(var))
yield setup_text_sensor_core_(var, config) await setup_text_sensor_core_(var, config)
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_TEXT_SENSOR") cg.add_define("USE_TEXT_SENSOR")
cg.add_global(text_sensor_ns.using) cg.add_global(text_sensor_ns.using)
@ -87,9 +85,9 @@ def to_code(config):
} }
), ),
) )
def text_sensor_state_to_code(config, condition_id, template_arg, args): async def text_sensor_state_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(condition_id, template_arg, paren) var = cg.new_Pvariable(condition_id, template_arg, paren)
templ = yield cg.templatable(config[CONF_STATE], args, cg.std_string) templ = await cg.templatable(config[CONF_STATE], args, cg.std_string)
cg.add(var.set_state(templ)) cg.add(var.set_state(templ))
yield var return var

View file

@ -28,7 +28,7 @@ from esphome.const import (
CONF_HOUR, CONF_HOUR,
CONF_MINUTE, CONF_MINUTE,
) )
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine_with_priority
from esphome.automation import Condition from esphome.automation import Condition
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -380,8 +380,7 @@ TIME_SCHEMA = cv.Schema(
).extend(cv.polling_component_schema("15min")) ).extend(cv.polling_component_schema("15min"))
@coroutine async def setup_time_core_(time_var, config):
def setup_time_core_(time_var, config):
cg.add(time_var.set_timezone(config[CONF_TIMEZONE])) cg.add(time_var.set_timezone(config[CONF_TIMEZONE]))
for conf in config.get(CONF_ON_TIME, []): for conf in config.get(CONF_ON_TIME, []):
@ -400,23 +399,22 @@ def setup_time_core_(time_var, config):
days_of_week = conf.get(CONF_DAYS_OF_WEEK, list(range(1, 8))) days_of_week = conf.get(CONF_DAYS_OF_WEEK, list(range(1, 8)))
cg.add(trigger.add_days_of_week(days_of_week)) cg.add(trigger.add_days_of_week(days_of_week))
yield cg.register_component(trigger, conf) await cg.register_component(trigger, conf)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TIME_SYNC, []): for conf in config.get(CONF_ON_TIME_SYNC, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
yield cg.register_component(trigger, conf) await cg.register_component(trigger, conf)
yield automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
@coroutine async def register_time(time_var, config):
def register_time(time_var, config): await setup_time_core_(time_var, config)
yield setup_time_core_(time_var, config)
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): async def to_code(config):
cg.add_define("USE_TIME") cg.add_define("USE_TIME")
cg.add_global(time_ns.using) cg.add_global(time_ns.using)
@ -430,6 +428,6 @@ def to_code(config):
} }
), ),
) )
def time_has_time_to_code(config, condition_id, template_arg, args): async def time_has_time_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(condition_id, template_arg, paren) return cg.new_Pvariable(condition_id, template_arg, paren)

View file

@ -11,7 +11,7 @@ from esphome.const import (
CONF_RX_BUFFER_SIZE, CONF_RX_BUFFER_SIZE,
CONF_INVERT, CONF_INVERT,
) )
from esphome.core import CORE, coroutine from esphome.core import CORE
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
uart_ns = cg.esphome_ns.namespace("uart") uart_ns = cg.esphome_ns.namespace("uart")
@ -73,10 +73,10 @@ CONFIG_SCHEMA = cv.All(
) )
def to_code(config): async def to_code(config):
cg.add_global(uart_ns.using) cg.add_global(uart_ns.using)
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.set_baud_rate(config[CONF_BAUD_RATE])) cg.add(var.set_baud_rate(config[CONF_BAUD_RATE]))
@ -100,13 +100,12 @@ UART_DEVICE_SCHEMA = cv.Schema(
) )
@coroutine async def register_uart_device(var, config):
def register_uart_device(var, config):
"""Register a UART device, setting up all the internal values. """Register a UART device, setting up all the internal values.
This is a coroutine, you need to await it with a 'yield' expression! This is a coroutine, you need to await it with a 'yield' expression!
""" """
parent = yield cg.get_variable(config[CONF_UART_ID]) parent = await cg.get_variable(config[CONF_UART_ID])
cg.add(var.set_uart_parent(parent)) cg.add(var.set_uart_parent(parent))
@ -121,16 +120,16 @@ def register_uart_device(var, config):
key=CONF_DATA, key=CONF_DATA,
), ),
) )
def uart_write_to_code(config, action_id, template_arg, args): async def uart_write_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg) var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID]) await cg.register_parented(var, config[CONF_ID])
data = config[CONF_DATA] data = config[CONF_DATA]
if isinstance(data, bytes): if isinstance(data, bytes):
data = list(data) data = list(data)
if cg.is_template(data): if cg.is_template(data):
templ = yield cg.templatable(data, args, cg.std_vector.template(cg.uint8)) templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ)) cg.add(var.set_data_template(templ))
else: else:
cg.add(var.set_data_static(data)) cg.add(var.set_data_static(data))
yield var return var

View file

@ -46,11 +46,11 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(40.0) @coroutine_with_priority(40.0)
def to_code(config): async def to_code(config):
paren = yield cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])
var = cg.new_Pvariable(config[CONF_ID], paren) var = cg.new_Pvariable(config[CONF_ID], paren)
yield cg.register_component(var, config) await cg.register_component(var, config)
cg.add(paren.set_port(config[CONF_PORT])) cg.add(paren.set_port(config[CONF_PORT]))
cg.add_define("WEBSERVER_PORT", config[CONF_PORT]) cg.add_define("WEBSERVER_PORT", config[CONF_PORT])

View file

@ -19,9 +19,9 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(65.0) @coroutine_with_priority(65.0)
def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) await cg.register_component(var, config)
if CORE.is_esp32: if CORE.is_esp32:
cg.add_library("FS", None) cg.add_library("FS", None)

View file

@ -277,7 +277,7 @@ def wifi_network(config, static_ip):
@coroutine_with_priority(60.0) @coroutine_with_priority(60.0)
def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_use_address(config[CONF_USE_ADDRESS])) cg.add(var.set_use_address(config[CONF_USE_ADDRESS]))
@ -305,9 +305,9 @@ def to_code(config):
add_mdns_library() add_mdns_library()
# Register at end for OTA safe mode # Register at end for OTA safe mode
yield cg.register_component(var, config) await cg.register_component(var, config)
@automation.register_condition("wifi.connected", WiFiConnectedCondition, cv.Schema({})) @automation.register_condition("wifi.connected", WiFiConnectedCondition, cv.Schema({}))
def wifi_connected_to_code(config, condition_id, template_arg, args): async def wifi_connected_to_code(config, condition_id, template_arg, args):
yield cg.new_Pvariable(condition_id, template_arg) return cg.new_Pvariable(condition_id, template_arg)

View file

@ -575,8 +575,7 @@ async def get_variable_with_full_id(id_: ID) -> Tuple[ID, "MockObj"]:
return await CORE.get_variable_with_full_id(id_) return await CORE.get_variable_with_full_id(id_)
@coroutine async def process_lambda(
def process_lambda(
value: Lambda, value: Lambda,
parameters: List[Tuple[SafeExpType, str]], parameters: List[Tuple[SafeExpType, str]],
capture: str = "=", capture: str = "=",
@ -596,11 +595,10 @@ def process_lambda(
from esphome.components.globals import GlobalsComponent from esphome.components.globals import GlobalsComponent
if value is None: if value is None:
yield
return return
parts = value.parts[:] parts = value.parts[:]
for i, id in enumerate(value.requires_ids): for i, id in enumerate(value.requires_ids):
full_id, var = yield get_variable_with_full_id(id) full_id, var = await get_variable_with_full_id(id)
if ( if (
full_id is not None full_id is not None
and isinstance(full_id.type, MockObjClass) and isinstance(full_id.type, MockObjClass)
@ -620,7 +618,7 @@ def process_lambda(
location.line += value.content_offset location.line += value.content_offset
else: else:
location = None location = None
yield LambdaExpression(parts, parameters, capture, return_type, location) return LambdaExpression(parts, parameters, capture, return_type, location)
def is_template(value): def is_template(value):
@ -628,8 +626,7 @@ def is_template(value):
return isinstance(value, Lambda) return isinstance(value, Lambda)
@coroutine async def templatable(
def templatable(
value: Any, value: Any,
args: List[Tuple[SafeExpType, str]], args: List[Tuple[SafeExpType, str]],
output_type: Optional[SafeExpType], output_type: Optional[SafeExpType],
@ -647,15 +644,12 @@ def templatable(
:return: The potentially templated value. :return: The potentially templated value.
""" """
if is_template(value): if is_template(value):
lambda_ = yield process_lambda(value, args, return_type=output_type) return await process_lambda(value, args, return_type=output_type)
yield lambda_ if to_exp is None:
else: return value
if to_exp is None: if isinstance(to_exp, dict):
yield value return to_exp[value]
elif isinstance(to_exp, dict): return to_exp(value)
yield to_exp[value]
else:
yield to_exp(value)
class MockObj(Expression): class MockObj(Expression):

View file

@ -14,8 +14,7 @@ from esphome.cpp_types import App, GPIOPin
from esphome.util import Registry, RegistryEntry from esphome.util import Registry, RegistryEntry
@coroutine async def gpio_pin_expression(conf):
def gpio_pin_expression(conf):
"""Generate an expression for the given pin option. """Generate an expression for the given pin option.
This is a coroutine, you must await it with a 'yield' expression! This is a coroutine, you must await it with a 'yield' expression!
@ -26,17 +25,15 @@ def gpio_pin_expression(conf):
for key, (func, _) in pins.PIN_SCHEMA_REGISTRY.items(): for key, (func, _) in pins.PIN_SCHEMA_REGISTRY.items():
if key in conf: if key in conf:
yield coroutine(func)(conf) return await coroutine(func)(conf)
return
number = conf[CONF_NUMBER] number = conf[CONF_NUMBER]
mode = conf[CONF_MODE] mode = conf[CONF_MODE]
inverted = conf.get(CONF_INVERTED) inverted = conf.get(CONF_INVERTED)
yield GPIOPin.new(number, RawExpression(mode), inverted) return GPIOPin.new(number, RawExpression(mode), inverted)
@coroutine async def register_component(var, config):
def register_component(var, config):
"""Register the given obj as a component. """Register the given obj as a component.
This is a coroutine, you must await it with a 'yield' expression! This is a coroutine, you must await it with a 'yield' expression!
@ -57,13 +54,12 @@ def register_component(var, config):
if CONF_UPDATE_INTERVAL in config: if CONF_UPDATE_INTERVAL in config:
add(var.set_update_interval(config[CONF_UPDATE_INTERVAL])) add(var.set_update_interval(config[CONF_UPDATE_INTERVAL]))
add(App.register_component(var)) add(App.register_component(var))
yield var return var
@coroutine async def register_parented(var, value):
def register_parented(var, value):
if isinstance(value, ID): if isinstance(value, ID):
paren = yield get_variable(value) paren = await get_variable(value)
else: else:
paren = value paren = value
add(var.set_parent(paren)) add(var.set_parent(paren))
@ -75,18 +71,16 @@ def extract_registry_entry_config(registry, full_config):
return registry[key], config return registry[key], config
@coroutine async def build_registry_entry(registry, full_config):
def build_registry_entry(registry, full_config):
registry_entry, config = extract_registry_entry_config(registry, full_config) registry_entry, config = extract_registry_entry_config(registry, full_config)
type_id = full_config[CONF_TYPE_ID] type_id = full_config[CONF_TYPE_ID]
builder = registry_entry.coroutine_fun builder = registry_entry.coroutine_fun
yield builder(config, type_id) return await builder(config, type_id)
@coroutine async def build_registry_list(registry, config):
def build_registry_list(registry, config):
actions = [] actions = []
for conf in config: for conf in config:
action = yield build_registry_entry(registry, conf) action = await build_registry_entry(registry, conf)
actions.append(action) actions.append(action)
yield actions return actions