1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00

Simplify coroutine syntax (#503)

* Simplify coroutine syntax

* More

* Lint

* Fix

* More

* Lint
This commit is contained in:
Otto Winter
2019-04-09 14:30:12 +02:00
committed by GitHub
parent e90829eef2
commit be5330b6ae
103 changed files with 351 additions and 588 deletions

View File

@@ -87,8 +87,7 @@ COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({
@ACTION_REGISTRY.register(CONF_COVER_OPEN, COVER_OPEN_ACTION_SCHEMA)
def cover_open_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
var = yield get_variable(config[CONF_ID])
type = OpenAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
@@ -102,8 +101,7 @@ COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({
@ACTION_REGISTRY.register(CONF_COVER_CLOSE, COVER_CLOSE_ACTION_SCHEMA)
def cover_close_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
var = yield get_variable(config[CONF_ID])
type = CloseAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
@@ -117,8 +115,7 @@ COVER_STOP_ACTION_SCHEMA = maybe_simple_id({
@ACTION_REGISTRY.register(CONF_COVER_STOP, COVER_STOP_ACTION_SCHEMA)
def cover_stop_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
var = yield get_variable(config[CONF_ID])
type = StopAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
@@ -136,26 +133,21 @@ COVER_CONTROL_ACTION_SCHEMA = cv.Schema({
@ACTION_REGISTRY.register(CONF_COVER_CONTROL, COVER_CONTROL_ACTION_SCHEMA)
def cover_control_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
var = yield get_variable(config[CONF_ID])
type = StopAction.template(template_arg)
rhs = type.new(var)
action = Pvariable(action_id, rhs, type=type)
if CONF_STOP in config:
for template_ in templatable(config[CONF_STOP], args, bool):
yield None
template_ = yield templatable(config[CONF_STOP], args, bool)
add(action.set_stop(template_))
if CONF_STATE in config:
for template_ in templatable(config[CONF_STATE], args, float,
to_exp=COVER_STATES):
yield None
template_ = yield templatable(config[CONF_STATE], args, float,
to_exp=COVER_STATES)
add(action.set_position(template_))
if CONF_POSITION in config:
for template_ in templatable(config[CONF_POSITION], args, float):
yield None
template_ = yield templatable(config[CONF_POSITION], args, float)
add(action.set_position(template_))
if CONF_TILT in config:
for template_ in templatable(config[CONF_TILT], args, float):
yield None
template_ = yield templatable(config[CONF_TILT], args, float)
add(action.set_tilt(template_))
yield action

View File

@@ -36,15 +36,13 @@ def to_code(config):
automation.build_automations(var.get_stop_trigger(), [],
config[CONF_STOP_ACTION])
for bin in get_variable(config[CONF_OPEN_ENDSTOP]):
yield
bin = yield get_variable(config[CONF_OPEN_ENDSTOP])
add(var.set_open_endstop(bin))
add(var.set_open_duration(config[CONF_OPEN_DURATION]))
automation.build_automations(var.get_open_trigger(), [],
config[CONF_OPEN_ACTION])
for bin in get_variable(config[CONF_CLOSE_ENDSTOP]):
yield
bin = yield get_variable(config[CONF_CLOSE_ENDSTOP])
add(var.set_close_endstop(bin))
add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
automation.build_automations(var.get_close_trigger(), [],

View File

@@ -4,9 +4,9 @@ from esphome import automation
from esphome.automation import ACTION_REGISTRY
from esphome.components import cover
import esphome.config_validation as cv
from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_ID, CONF_LAMBDA, CONF_NAME, \
CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_STATE, CONF_STOP_ACTION, CONF_POSITION, \
CONF_CURRENT_OPERATION, CONF_RESTORE_MODE
from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_CURRENT_OPERATION, CONF_ID, \
CONF_LAMBDA, CONF_NAME, CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_POSITION, CONF_RESTORE_MODE, \
CONF_STATE, CONF_STOP_ACTION
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import Action, App, optional
@@ -40,9 +40,8 @@ def to_code(config):
setup_component(var, config)
if CONF_LAMBDA in config:
for template_ in process_lambda(config[CONF_LAMBDA], [],
return_type=optional.template(float)):
yield
template_ = yield process_lambda(config[CONF_LAMBDA], [],
return_type=optional.template(float))
add(var.set_state_lambda(template_))
if CONF_OPEN_ACTION in config:
automation.build_automations(var.get_open_trigger(), [],
@@ -75,24 +74,20 @@ COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
@ACTION_REGISTRY.register(CONF_COVER_TEMPLATE_PUBLISH,
COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA)
def cover_template_publish_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
var = yield get_variable(config[CONF_ID])
type = CoverPublishAction.template(template_arg)
rhs = type.new(var)
action = Pvariable(action_id, rhs, type=type)
if CONF_STATE in config:
for template_ in templatable(config[CONF_STATE], args, float,
to_exp=cover.COVER_STATES):
yield None
template_ = yield templatable(config[CONF_STATE], args, float,
to_exp=cover.COVER_STATES)
add(action.set_position(template_))
if CONF_POSITION in config:
for template_ in templatable(config[CONF_POSITION], args, float,
to_exp=cover.COVER_STATES):
yield None
template_ = yield templatable(config[CONF_POSITION], args, float,
to_exp=cover.COVER_STATES)
add(action.set_position(template_))
if CONF_CURRENT_OPERATION in config:
for template_ in templatable(config[CONF_CURRENT_OPERATION], args, cover.CoverOperation,
to_exp=cover.COVER_OPERATIONS):
yield None
template_ = yield templatable(config[CONF_CURRENT_OPERATION], args, cover.CoverOperation,
to_exp=cover.COVER_OPERATIONS)
add(action.set_current_operation(template_))
yield action