mirror of
https://github.com/esphome/esphome.git
synced 2025-09-19 11:42:20 +01:00
Simplify coroutine syntax (#503)
* Simplify coroutine syntax * More * Lint * Fix * More * Lint
This commit is contained in:
@@ -15,7 +15,7 @@ from esphome.const import CONF_ABOVE, CONF_ACCURACY_DECIMALS, CONF_ALPHA, CONF_B
|
||||
CONF_SEND_EVERY, CONF_SEND_FIRST_AT, CONF_SLIDING_WINDOW_MOVING_AVERAGE, \
|
||||
CONF_THROTTLE, CONF_TO, CONF_TRIGGER_ID, CONF_UNIQUE, CONF_UNIT_OF_MEASUREMENT, \
|
||||
CONF_WINDOW_SIZE
|
||||
from esphome.core import CORE
|
||||
from esphome.core import CORE, coroutine
|
||||
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
|
||||
from esphome.cpp_types import App, Component, Nameable, PollingComponent, Trigger, \
|
||||
esphome_ns, float_, optional
|
||||
@@ -143,6 +143,7 @@ SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
|
||||
SENSOR_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(SENSOR_SCHEMA.schema)
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_filter(config):
|
||||
if CONF_OFFSET in config:
|
||||
yield OffsetFilter.new(config[CONF_OFFSET])
|
||||
@@ -158,17 +159,15 @@ def setup_filter(config):
|
||||
conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE]
|
||||
yield ExponentialMovingAverageFilter.new(conf[CONF_ALPHA], conf[CONF_SEND_EVERY])
|
||||
elif CONF_LAMBDA in config:
|
||||
for lambda_ in process_lambda(config[CONF_LAMBDA], [(float_, 'x')],
|
||||
return_type=optional.template(float_)):
|
||||
yield None
|
||||
lambda_ = yield process_lambda(config[CONF_LAMBDA], [(float_, 'x')],
|
||||
return_type=optional.template(float_))
|
||||
yield LambdaFilter.new(lambda_)
|
||||
elif CONF_THROTTLE in config:
|
||||
yield ThrottleFilter.new(config[CONF_THROTTLE])
|
||||
elif CONF_DELTA in config:
|
||||
yield DeltaFilter.new(config[CONF_DELTA])
|
||||
elif CONF_OR in config:
|
||||
for filters in setup_filters(config[CONF_OR]):
|
||||
yield None
|
||||
filters = yield setup_filters(config[CONF_OR])
|
||||
yield OrFilter.new(filters)
|
||||
elif CONF_HEARTBEAT in config:
|
||||
yield App.register_component(HeartbeatFilter.new(config[CONF_HEARTBEAT]))
|
||||
@@ -181,15 +180,15 @@ def setup_filter(config):
|
||||
yield CalibrateLinearFilter.new(k, b)
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_filters(config):
|
||||
filters = []
|
||||
for conf in config:
|
||||
for filter in setup_filter(conf):
|
||||
yield None
|
||||
filters.append(filter)
|
||||
filters.append((yield setup_filter(conf)))
|
||||
yield filters
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_sensor_core_(sensor_var, config):
|
||||
if CONF_INTERNAL in config:
|
||||
add(sensor_var.set_internal(config[CONF_INTERNAL]))
|
||||
@@ -200,8 +199,7 @@ def setup_sensor_core_(sensor_var, config):
|
||||
if CONF_ACCURACY_DECIMALS in config:
|
||||
add(sensor_var.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
|
||||
if CONF_FILTERS in config:
|
||||
for filters in setup_filters(config[CONF_FILTERS]):
|
||||
yield
|
||||
filters = yield setup_filters(config[CONF_FILTERS])
|
||||
add(sensor_var.set_filters(filters))
|
||||
|
||||
for conf in config.get(CONF_ON_VALUE, []):
|
||||
@@ -217,12 +215,10 @@ def setup_sensor_core_(sensor_var, config):
|
||||
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
|
||||
add(App.register_component(trigger))
|
||||
if CONF_ABOVE in conf:
|
||||
for template_ in templatable(conf[CONF_ABOVE], float_, float_):
|
||||
yield
|
||||
template_ = yield templatable(conf[CONF_ABOVE], float_, float_)
|
||||
add(trigger.set_min(template_))
|
||||
if CONF_BELOW in conf:
|
||||
for template_ in templatable(conf[CONF_BELOW], float_, float_):
|
||||
yield
|
||||
template_ = yield templatable(conf[CONF_BELOW], float_, float_)
|
||||
add(trigger.set_max(template_))
|
||||
automation.build_automations(trigger, [(float_, 'x')], conf)
|
||||
|
||||
@@ -260,8 +256,7 @@ SENSOR_IN_RANGE_CONDITION_SCHEMA = vol.All({
|
||||
|
||||
@CONDITION_REGISTRY.register(CONF_SENSOR_IN_RANGE, SENSOR_IN_RANGE_CONDITION_SCHEMA)
|
||||
def sensor_in_range_to_code(config, condition_id, template_arg, args):
|
||||
for var in get_variable(config[CONF_ID]):
|
||||
yield None
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
rhs = var.make_sensor_in_range_condition(template_arg)
|
||||
type = SensorInRangeCondition.template(template_arg)
|
||||
cond = Pvariable(condition_id, rhs, type=type)
|
||||
|
@@ -60,8 +60,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_ADS1115_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_ADS1115_ID])
|
||||
|
||||
mux = MUX[config[CONF_MULTIPLEXER]]
|
||||
gain = GAIN[config[CONF_GAIN]]
|
||||
|
@@ -24,8 +24,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_APDS9960_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_APDS9960_ID])
|
||||
func = getattr(hub, TYPES[config[CONF_TYPE]])
|
||||
rhs = func(config[CONF_NAME])
|
||||
sensor.register_sensor(rhs, config)
|
||||
|
@@ -20,7 +20,6 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_ESP32_BLE_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_ESP32_BLE_ID])
|
||||
rhs = hub.make_rssi_sensor(config[CONF_NAME], make_address_array(config[CONF_MAC_ADDRESS]))
|
||||
sensor.register_sensor(rhs, config)
|
||||
|
@@ -38,8 +38,7 @@ PLATFORM_SCHEMA = vol.All(sensor.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_cse7766(uart_, config.get(CONF_UPDATE_INTERVAL))
|
||||
cse = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -18,9 +18,8 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for template_ in process_lambda(config[CONF_LAMBDA], [],
|
||||
return_type=std_vector.template(sensor.SensorPtr)):
|
||||
yield
|
||||
template_ = yield process_lambda(config[CONF_LAMBDA], [],
|
||||
return_type=std_vector.template(sensor.SensorPtr))
|
||||
|
||||
rhs = CustomSensorConstructor(template_)
|
||||
custom = variable(config[CONF_ID], rhs)
|
||||
|
@@ -20,8 +20,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_DALLAS_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_DALLAS_ID])
|
||||
if CONF_ADDRESS in config:
|
||||
address = HexIntLiteral(config[CONF_ADDRESS])
|
||||
rhs = hub.Pget_sensor_by_address(config[CONF_NAME], address, config.get(CONF_RESOLUTION))
|
||||
|
@@ -40,8 +40,7 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for pin in gpio_output_pin_expression(config[CONF_PIN]):
|
||||
yield
|
||||
pin = yield gpio_output_pin_expression(config[CONF_PIN])
|
||||
rhs = App.make_dht_sensor(config[CONF_TEMPERATURE][CONF_NAME],
|
||||
config[CONF_HUMIDITY][CONF_NAME],
|
||||
pin, config.get(CONF_UPDATE_INTERVAL))
|
||||
|
@@ -19,8 +19,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for pin in gpio_input_pin_expression(config[CONF_PIN]):
|
||||
yield
|
||||
pin = yield gpio_input_pin_expression(config[CONF_PIN])
|
||||
rhs = App.make_duty_cycle_sensor(config[CONF_NAME], pin,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
duty = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -40,8 +40,7 @@ PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for sel in gpio_output_pin_expression(config[CONF_SEL_PIN]):
|
||||
yield
|
||||
sel = yield gpio_output_pin_expression(config[CONF_SEL_PIN])
|
||||
|
||||
rhs = App.make_hlw8012(sel, config[CONF_CF_PIN], config[CONF_CF1_PIN],
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
|
@@ -30,10 +30,8 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for dout_pin in gpio_input_pin_expression(config[CONF_DOUT_PIN]):
|
||||
yield
|
||||
for sck_pin in gpio_input_pin_expression(config[CONF_CLK_PIN]):
|
||||
yield
|
||||
dout_pin = yield gpio_input_pin_expression(config[CONF_DOUT_PIN])
|
||||
sck_pin = yield gpio_input_pin_expression(config[CONF_CLK_PIN])
|
||||
|
||||
rhs = App.make_hx711_sensor(config[CONF_NAME], dout_pin, sck_pin,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
|
@@ -21,10 +21,8 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_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_max31855_sensor(config[CONF_NAME], spi_, cs,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
max31855 = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -22,10 +22,8 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_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_max6675_sensor(config[CONF_NAME], spi_, cs,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
max6675 = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -30,8 +30,7 @@ PLATFORM_SCHEMA = sensor.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_mhz19_sensor(uart_, config[CONF_CO2][CONF_NAME],
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
mhz19 = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -61,8 +61,7 @@ PLATFORM_SCHEMA = vol.All(sensor.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_pmsx003(uart_, PMSX003_TYPES[config[CONF_TYPE]])
|
||||
pms = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -58,8 +58,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for pin in gpio_input_pin_expression(config[CONF_PIN]):
|
||||
yield
|
||||
pin = yield gpio_input_pin_expression(config[CONF_PIN])
|
||||
rhs = App.make_pulse_counter_sensor(config[CONF_NAME], pin,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
pcnt = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -46,16 +46,13 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for pin_a in gpio_input_pin_expression(config[CONF_PIN_A]):
|
||||
yield
|
||||
for pin_b in gpio_input_pin_expression(config[CONF_PIN_B]):
|
||||
yield
|
||||
pin_a = yield gpio_input_pin_expression(config[CONF_PIN_A])
|
||||
pin_b = yield gpio_input_pin_expression(config[CONF_PIN_B])
|
||||
rhs = App.make_rotary_encoder_sensor(config[CONF_NAME], pin_a, pin_b)
|
||||
encoder = Pvariable(config[CONF_ID], rhs)
|
||||
|
||||
if CONF_PIN_RESET in config:
|
||||
for pin_i in gpio_input_pin_expression(config[CONF_PIN_RESET]):
|
||||
yield
|
||||
pin_i = yield gpio_input_pin_expression(config[CONF_PIN_RESET])
|
||||
add(encoder.set_reset_pin(pin_i))
|
||||
if CONF_RESOLUTION in config:
|
||||
resolution = RESOLUTIONS[config[CONF_RESOLUTION]]
|
||||
|
@@ -46,8 +46,7 @@ PLATFORM_SCHEMA = vol.All(sensor.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_sds011(uart_)
|
||||
sds011 = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -26,9 +26,8 @@ def to_code(config):
|
||||
setup_component(template, 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(template.set_template(template_))
|
||||
|
||||
|
||||
@@ -43,12 +42,10 @@ SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_SENSOR_TEMPLATE_PUBLISH, SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA)
|
||||
def sensor_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])
|
||||
rhs = var.make_sensor_publish_action(template_arg)
|
||||
type = SensorPublishAction.template(template_arg)
|
||||
action = Pvariable(action_id, rhs, type=type)
|
||||
for template_ in templatable(config[CONF_STATE], args, float_):
|
||||
yield None
|
||||
template_ = yield templatable(config[CONF_STATE], args, float_)
|
||||
add(action.set_state(template_))
|
||||
yield action
|
||||
|
@@ -20,10 +20,8 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for time_ in get_variable(config[CONF_TIME_ID]):
|
||||
yield
|
||||
for sens in get_variable(config[CONF_POWER_ID]):
|
||||
yield
|
||||
time_ = yield get_variable(config[CONF_TIME_ID])
|
||||
sens = yield get_variable(config[CONF_POWER_ID])
|
||||
rhs = App.make_total_daily_energy_sensor(config[CONF_NAME], time_, sens)
|
||||
total_energy = Pvariable(config[CONF_ID], rhs)
|
||||
|
||||
|
@@ -33,10 +33,8 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for trigger in gpio_output_pin_expression(config[CONF_TRIGGER_PIN]):
|
||||
yield
|
||||
for echo in gpio_input_pin_expression(config[CONF_ECHO_PIN]):
|
||||
yield
|
||||
trigger = yield gpio_output_pin_expression(config[CONF_TRIGGER_PIN])
|
||||
echo = yield gpio_input_pin_expression(config[CONF_ECHO_PIN])
|
||||
rhs = App.make_ultrasonic_sensor(config[CONF_NAME], trigger, echo,
|
||||
config.get(CONF_UPDATE_INTERVAL))
|
||||
ultrasonic = Pvariable(config[CONF_ID], rhs)
|
||||
|
@@ -23,8 +23,7 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_ESP32_BLE_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_ESP32_BLE_ID])
|
||||
rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS]))
|
||||
dev = Pvariable(config[CONF_ID], rhs)
|
||||
if CONF_TEMPERATURE in config:
|
||||
|
@@ -21,8 +21,7 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
for hub in get_variable(config[CONF_ESP32_BLE_ID]):
|
||||
yield
|
||||
hub = yield get_variable(config[CONF_ESP32_BLE_ID])
|
||||
rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS]))
|
||||
dev = Pvariable(config[CONF_MAKE_ID], rhs)
|
||||
if CONF_TEMPERATURE in config:
|
||||
|
Reference in New Issue
Block a user