mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 17:05:36 +00:00
Refactor automation building to iterate tuples
This commit is contained in:
@@ -529,23 +529,14 @@ def binary_sensor_schema(
|
||||
|
||||
@coroutine_with_priority(CoroPriority.AUTOMATION)
|
||||
async def _build_binary_sensor_automations(var, config):
|
||||
for conf in config.get(CONF_ON_PRESS, []):
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_state_callback",
|
||||
[],
|
||||
conf,
|
||||
forwarder=automation.TriggerOnTrueForwarder,
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_RELEASE, []):
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_state_callback",
|
||||
[],
|
||||
conf,
|
||||
forwarder=automation.TriggerOnFalseForwarder,
|
||||
)
|
||||
for conf_key, forwarder in (
|
||||
(CONF_ON_PRESS, automation.TriggerOnTrueForwarder),
|
||||
(CONF_ON_RELEASE, automation.TriggerOnFalseForwarder),
|
||||
):
|
||||
for conf in config.get(conf_key, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_state_callback", [], conf, forwarder=forwarder
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_CLICK, []):
|
||||
trigger = cg.new_Pvariable(
|
||||
|
||||
@@ -881,14 +881,14 @@ async def build_filters(config):
|
||||
|
||||
@coroutine_with_priority(CoroPriority.AUTOMATION)
|
||||
async def _build_sensor_automations(var, config):
|
||||
for conf in config.get(CONF_ON_VALUE, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_state_callback", [(float, "x")], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_RAW_VALUE, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_raw_state_callback", [(float, "x")], conf
|
||||
)
|
||||
for conf_key, callback in (
|
||||
(CONF_ON_VALUE, "add_on_state_callback"),
|
||||
(CONF_ON_RAW_VALUE, "add_on_raw_state_callback"),
|
||||
):
|
||||
for conf in config.get(conf_key, []):
|
||||
await automation.build_callback_automation(
|
||||
var, callback, [(float, "x")], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_VALUE_RANGE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await cg.register_component(trigger, conf)
|
||||
|
||||
@@ -123,26 +123,15 @@ def switch_schema(
|
||||
|
||||
@coroutine_with_priority(CoroPriority.AUTOMATION)
|
||||
async def _build_switch_automations(var, config):
|
||||
for conf in config.get(CONF_ON_STATE, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_state_callback", [(bool, "x")], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_TURN_ON, []):
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_state_callback",
|
||||
[],
|
||||
conf,
|
||||
forwarder=automation.TriggerOnTrueForwarder,
|
||||
)
|
||||
for conf in config.get(CONF_ON_TURN_OFF, []):
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_state_callback",
|
||||
[],
|
||||
conf,
|
||||
forwarder=automation.TriggerOnFalseForwarder,
|
||||
)
|
||||
for conf_key, args, forwarder in (
|
||||
(CONF_ON_STATE, [(bool, "x")], None),
|
||||
(CONF_ON_TURN_ON, [], automation.TriggerOnTrueForwarder),
|
||||
(CONF_ON_TURN_OFF, [], automation.TriggerOnFalseForwarder),
|
||||
):
|
||||
for conf in config.get(conf_key, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_state_callback", args, conf, forwarder=forwarder
|
||||
)
|
||||
|
||||
|
||||
@setup_entity("switch")
|
||||
|
||||
@@ -184,15 +184,14 @@ async def build_filters(config):
|
||||
|
||||
@coroutine_with_priority(CoroPriority.AUTOMATION)
|
||||
async def _build_text_sensor_automations(var, config):
|
||||
for conf in config.get(CONF_ON_VALUE, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_state_callback", [(cg.std_string, "x")], conf
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_RAW_VALUE, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_raw_state_callback", [(cg.std_string, "x")], conf
|
||||
)
|
||||
for conf_key, callback in (
|
||||
(CONF_ON_VALUE, "add_on_state_callback"),
|
||||
(CONF_ON_RAW_VALUE, "add_on_raw_state_callback"),
|
||||
):
|
||||
for conf in config.get(conf_key, []):
|
||||
await automation.build_callback_automation(
|
||||
var, callback, [(cg.std_string, "x")], conf
|
||||
)
|
||||
|
||||
|
||||
@setup_entity("text_sensor")
|
||||
|
||||
Reference in New Issue
Block a user