mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Simplify coroutine syntax (#503)
* Simplify coroutine syntax * More * Lint * Fix * More * Lint
This commit is contained in:
@@ -4,10 +4,10 @@ import voluptuous as vol
|
||||
from esphome import core
|
||||
from esphome.automation import ACTION_REGISTRY, maybe_simple_id
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_LAMBDA, CONF_ROTATION, CONF_UPDATE_INTERVAL, CONF_PAGES, CONF_ID
|
||||
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES, CONF_ROTATION, CONF_UPDATE_INTERVAL
|
||||
from esphome.core import CORE
|
||||
from esphome.cpp_generator import add, process_lambda, Pvariable, templatable, get_variable
|
||||
from esphome.cpp_types import esphome_ns, void, Action
|
||||
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
|
||||
from esphome.cpp_types import Action, esphome_ns, void
|
||||
|
||||
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
||||
|
||||
@@ -63,9 +63,8 @@ def setup_display_core_(display_var, config):
|
||||
if CONF_PAGES in config:
|
||||
pages = []
|
||||
for conf in config[CONF_PAGES]:
|
||||
for lambda_ in process_lambda(conf[CONF_LAMBDA], [(DisplayBufferRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(conf[CONF_LAMBDA], [(DisplayBufferRef, 'it')],
|
||||
return_type=void)
|
||||
var = Pvariable(conf[CONF_ID], DisplayPage.new(lambda_))
|
||||
pages.append(var)
|
||||
add(display_var.set_pages(pages))
|
||||
@@ -82,12 +81,10 @@ def display_page_show_to_code(config, action_id, template_arg, args):
|
||||
type = DisplayPageShowAction.template(template_arg)
|
||||
action = Pvariable(action_id, type.new(), type=type)
|
||||
if isinstance(config[CONF_ID], core.Lambda):
|
||||
for template_ in templatable(config[CONF_ID], args, DisplayPagePtr):
|
||||
yield None
|
||||
template_ = yield templatable(config[CONF_ID], args, DisplayPagePtr)
|
||||
add(action.set_page(template_))
|
||||
else:
|
||||
for var in get_variable(config[CONF_ID]):
|
||||
yield None
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
add(action.set_page(var))
|
||||
yield action
|
||||
|
||||
@@ -100,8 +97,7 @@ DISPLAY_PAGE_SHOW_NEXT_ACTION_SCHEMA = maybe_simple_id({
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW_NEXT, DISPLAY_PAGE_SHOW_NEXT_ACTION_SCHEMA)
|
||||
def display_page_show_next_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 = DisplayPageShowNextAction.template(template_arg)
|
||||
yield Pvariable(action_id, type.new(var), type=type)
|
||||
|
||||
@@ -114,8 +110,7 @@ DISPLAY_PAGE_SHOW_PREVIOUS_ACTION_SCHEMA = maybe_simple_id({
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW_PREVIOUS, DISPLAY_PAGE_SHOW_PREVIOUS_ACTION_SCHEMA)
|
||||
def display_page_show_previous_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 = DisplayPageShowPrevAction.template(template_arg)
|
||||
yield Pvariable(action_id, type.new(var), type=type)
|
||||
|
||||
|
||||
@@ -46,27 +46,21 @@ def to_code(config):
|
||||
lcd = Pvariable(config[CONF_ID], rhs)
|
||||
pins_ = []
|
||||
for conf in config[CONF_DATA_PINS]:
|
||||
for pin in gpio_output_pin_expression(conf):
|
||||
yield
|
||||
pins_.append(pin)
|
||||
pins_.append((yield gpio_output_pin_expression(conf)))
|
||||
add(lcd.set_data_pins(*pins_))
|
||||
for enable in gpio_output_pin_expression(config[CONF_ENABLE_PIN]):
|
||||
yield
|
||||
enable = yield gpio_output_pin_expression(config[CONF_ENABLE_PIN])
|
||||
add(lcd.set_enable_pin(enable))
|
||||
|
||||
for rs in gpio_output_pin_expression(config[CONF_RS_PIN]):
|
||||
yield
|
||||
rs = yield gpio_output_pin_expression(config[CONF_RS_PIN])
|
||||
add(lcd.set_rs_pin(rs))
|
||||
|
||||
if CONF_RW_PIN in config:
|
||||
for rw in gpio_output_pin_expression(config[CONF_RW_PIN]):
|
||||
yield
|
||||
rw = yield gpio_output_pin_expression(config[CONF_RW_PIN])
|
||||
add(lcd.set_rw_pin(rw))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')],
|
||||
return_type=void)
|
||||
add(lcd.set_writer(lambda_))
|
||||
|
||||
display.setup_display(lcd, config)
|
||||
|
||||
@@ -28,9 +28,8 @@ def to_code(config):
|
||||
add(lcd.set_address(config[CONF_ADDRESS]))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')],
|
||||
return_type=void)
|
||||
add(lcd.set_writer(lambda_))
|
||||
|
||||
display.setup_display(lcd, config)
|
||||
|
||||
@@ -26,10 +26,8 @@ PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for spi_ in get_variable(config[CONF_SPI_ID]):
|
||||
yield
|
||||
for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
|
||||
yield
|
||||
spi_ = yield get_variable(config[CONF_SPI_ID])
|
||||
cs = yield gpio_output_pin_expression(config[CONF_CS_PIN])
|
||||
rhs = App.make_max7219(spi_, cs)
|
||||
max7219 = Pvariable(config[CONF_ID], rhs)
|
||||
|
||||
@@ -39,9 +37,8 @@ def to_code(config):
|
||||
add(max7219.set_intensity(config[CONF_INTENSITY]))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(MAX7219ComponentRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(MAX7219ComponentRef, 'it')],
|
||||
return_type=void)
|
||||
add(max7219.set_writer(lambda_))
|
||||
|
||||
display.setup_display(max7219, config)
|
||||
|
||||
@@ -18,15 +18,13 @@ PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for uart_ in get_variable(config[CONF_UART_ID]):
|
||||
yield
|
||||
uart_ = yield get_variable(config[CONF_UART_ID])
|
||||
rhs = App.make_nextion(uart_)
|
||||
nextion = Pvariable(config[CONF_ID], rhs)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(NextionRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(NextionRef, 'it')],
|
||||
return_type=void)
|
||||
add(nextion.set_writer(lambda_))
|
||||
|
||||
display.setup_display(nextion, config)
|
||||
|
||||
@@ -28,17 +28,15 @@ def to_code(config):
|
||||
add(ssd.set_model(ssd1306_spi.MODELS[config[CONF_MODEL]]))
|
||||
|
||||
if CONF_RESET_PIN in config:
|
||||
for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
|
||||
yield
|
||||
reset = yield gpio_output_pin_expression(config[CONF_RESET_PIN])
|
||||
add(ssd.set_reset_pin(reset))
|
||||
if CONF_EXTERNAL_VCC in config:
|
||||
add(ssd.set_external_vcc(config[CONF_EXTERNAL_VCC]))
|
||||
if CONF_ADDRESS in config:
|
||||
add(ssd.set_address(config[CONF_ADDRESS]))
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA],
|
||||
[(display.DisplayBufferRef, 'it')], return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA],
|
||||
[(display.DisplayBufferRef, 'it')], return_type=void)
|
||||
add(ssd.set_writer(lambda_))
|
||||
|
||||
display.setup_display(ssd, config)
|
||||
|
||||
@@ -5,7 +5,7 @@ from esphome.components import display, spi
|
||||
from esphome.components.spi import SPIComponent
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_CS_PIN, CONF_DC_PIN, CONF_EXTERNAL_VCC, CONF_ID, CONF_LAMBDA, \
|
||||
CONF_MODEL, CONF_RESET_PIN, CONF_SPI_ID, CONF_PAGES
|
||||
CONF_MODEL, CONF_PAGES, CONF_RESET_PIN, CONF_SPI_ID
|
||||
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda
|
||||
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
|
||||
from esphome.cpp_types import App, PollingComponent, void
|
||||
@@ -41,27 +41,22 @@ PLATFORM_SCHEMA = vol.All(display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for spi_ in get_variable(config[CONF_SPI_ID]):
|
||||
yield
|
||||
for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
|
||||
yield
|
||||
for dc in gpio_output_pin_expression(config[CONF_DC_PIN]):
|
||||
yield
|
||||
spi_ = yield get_variable(config[CONF_SPI_ID])
|
||||
cs = yield gpio_output_pin_expression(config[CONF_CS_PIN])
|
||||
dc = yield gpio_output_pin_expression(config[CONF_DC_PIN])
|
||||
|
||||
rhs = App.make_spi_ssd1306(spi_, cs, dc)
|
||||
ssd = Pvariable(config[CONF_ID], rhs)
|
||||
add(ssd.set_model(MODELS[config[CONF_MODEL]]))
|
||||
|
||||
if CONF_RESET_PIN in config:
|
||||
for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
|
||||
yield
|
||||
reset = yield gpio_output_pin_expression(config[CONF_RESET_PIN])
|
||||
add(ssd.set_reset_pin(reset))
|
||||
if CONF_EXTERNAL_VCC in config:
|
||||
add(ssd.set_external_vcc(config[CONF_EXTERNAL_VCC]))
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA],
|
||||
[(display.DisplayBufferRef, 'it')], return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA],
|
||||
[(display.DisplayBufferRef, 'it')], return_type=void)
|
||||
add(ssd.set_writer(lambda_))
|
||||
|
||||
display.setup_display(ssd, config)
|
||||
|
||||
@@ -53,12 +53,9 @@ PLATFORM_SCHEMA = vol.All(display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for spi_ in get_variable(config[CONF_SPI_ID]):
|
||||
yield
|
||||
for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
|
||||
yield
|
||||
for dc in gpio_output_pin_expression(config[CONF_DC_PIN]):
|
||||
yield
|
||||
spi_ = yield get_variable(config[CONF_SPI_ID])
|
||||
cs = yield gpio_output_pin_expression(config[CONF_CS_PIN])
|
||||
dc = yield gpio_output_pin_expression(config[CONF_DC_PIN])
|
||||
|
||||
model_type, model = MODELS[config[CONF_MODEL]]
|
||||
if model_type == 'a':
|
||||
@@ -71,17 +68,14 @@ def to_code(config):
|
||||
raise NotImplementedError()
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')],
|
||||
return_type=void):
|
||||
yield
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')],
|
||||
return_type=void)
|
||||
add(epaper.set_writer(lambda_))
|
||||
if CONF_RESET_PIN in config:
|
||||
for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]):
|
||||
yield
|
||||
reset = yield gpio_output_pin_expression(config[CONF_RESET_PIN])
|
||||
add(epaper.set_reset_pin(reset))
|
||||
if CONF_BUSY_PIN in config:
|
||||
for reset in gpio_input_pin_expression(config[CONF_BUSY_PIN]):
|
||||
yield
|
||||
reset = yield gpio_input_pin_expression(config[CONF_BUSY_PIN])
|
||||
add(epaper.set_busy_pin(reset))
|
||||
if CONF_FULL_UPDATE_EVERY in config:
|
||||
add(epaper.set_full_update_every(config[CONF_FULL_UPDATE_EVERY]))
|
||||
|
||||
Reference in New Issue
Block a user