1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 17:22:20 +01:00

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
29 changed files with 313 additions and 359 deletions

View File

@@ -45,7 +45,7 @@ from esphome.const import (
DEVICE_CLASS_TIMESTAMP,
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
CODEOWNERS = ["@esphome/core"]
@@ -441,13 +441,11 @@ def calibrate_polynomial_filter_to_code(config, filter_id):
yield cg.new_Pvariable(filter_id, res)
@coroutine
def build_filters(config):
yield cg.build_registry_list(FILTER_REGISTRY, config)
async def build_filters(config):
return await cg.build_registry_list(FILTER_REGISTRY, config)
@coroutine
def setup_sensor_core_(var, config):
async def setup_sensor_core_(var, config):
cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config:
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_force_update(config[CONF_FORCE_UPDATE]))
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))
for conf in config.get(CONF_ON_VALUE, []):
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, []):
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, []):
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:
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_))
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_))
yield automation.build_automation(trigger, [(float, "x")], conf)
await automation.build_automation(trigger, [(float, "x")], conf)
if CONF_MQTT_ID in config:
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 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]))
@coroutine
def register_sensor(var, config):
async def register_sensor(var, config):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_sensor(var))
yield setup_sensor_core_(var, config)
await setup_sensor_core_(var, config)
@coroutine
def new_sensor(config):
async def new_sensor(config):
var = cg.new_Pvariable(config[CONF_ID])
yield register_sensor(var, config)
yield var
await register_sensor(var, config)
return var
SENSOR_IN_RANGE_CONDITION_SCHEMA = cv.All(
@@ -520,8 +516,8 @@ SENSOR_IN_RANGE_CONDITION_SCHEMA = cv.All(
@automation.register_condition(
"sensor.in_range", SensorInRangeCondition, SENSOR_IN_RANGE_CONDITION_SCHEMA
)
def sensor_in_range_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
async def sensor_in_range_to_code(config, condition_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(condition_id, template_arg, paren)
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:
cg.add(var.set_max(config[CONF_BELOW]))
yield var
return var
def _mean(xs):
@@ -618,6 +614,6 @@ def _lstsq(a, b):
@coroutine_with_priority(40.0)
def to_code(config):
async def to_code(config):
cg.add_define("USE_SENSOR")
cg.add_global(sensor_ns.using)