[valve] Build on_open and on_closed with build_callback_automation (#20442)

This commit is contained in:
J. Nick Koston
2026-10-09 15:43:22 -10:00
committed by GitHub
parent 073394b1eb
commit 4d187a2bb7
4 changed files with 25 additions and 63 deletions
+13 -26
View File
@@ -15,7 +15,6 @@ from esphome.const import (
CONF_POSITION_STATE_TOPIC,
CONF_STATE,
CONF_STOP,
CONF_TRIGGER_ID,
CONF_WEB_SERVER,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_GAS,
@@ -62,17 +61,18 @@ VALVE_OPERATIONS = {
}
validate_valve_operation = cv.enum(VALVE_OPERATIONS, upper=True)
# Actions
ValvePublishAction = valve_ns.class_("ValvePublishAction", automation.Action)
# Triggers
ValveOpenTrigger = valve_ns.class_("ValveOpenTrigger", automation.Trigger.template())
ValveClosedTrigger = valve_ns.class_(
"ValveClosedTrigger", automation.Trigger.template()
)
CONF_ON_CLOSED = "on_closed"
# The state callback carries nothing; each automation fires only in its end state.
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(
CONF_ON_OPEN, "add_on_state_callback", when="{parent}->is_fully_open()"
),
automation.CallbackAutomation(
CONF_ON_CLOSED, "add_on_state_callback", when="{parent}->is_fully_closed()"
),
)
_VALVE_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
@@ -89,16 +89,8 @@ _VALVE_SCHEMA = (
cv.Optional(CONF_POSITION_STATE_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.subscribe_topic
),
cv.Optional(CONF_ON_OPEN): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ValveOpenTrigger),
}
),
cv.Optional(CONF_ON_CLOSED): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ValveClosedTrigger),
}
),
cv.Optional(CONF_ON_OPEN): automation.validate_automation({}),
cv.Optional(CONF_ON_CLOSED): automation.validate_automation({}),
}
)
)
@@ -134,12 +126,7 @@ def valve_schema(
async def _setup_valve_core(var, config):
setup_device_class(config)
for conf in config.get(CONF_ON_OPEN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_CLOSED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
if mqtt_id_config := config.get(CONF_MQTT_ID):
mqtt_ = cg.new_Pvariable(mqtt_id_config, var)
-37
View File
@@ -1,37 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "valve.h"
namespace esphome::valve {
class ValveOpenTrigger final : public Trigger<> {
public:
ValveOpenTrigger(Valve *a_valve) : valve_(a_valve) {
a_valve->add_on_state_callback([this]() {
if (this->valve_->is_fully_open()) {
this->trigger();
}
});
}
protected:
Valve *valve_;
};
class ValveClosedTrigger final : public Trigger<> {
public:
ValveClosedTrigger(Valve *a_valve) : valve_(a_valve) {
a_valve->add_on_state_callback([this]() {
if (this->valve_->is_fully_closed()) {
this->trigger();
}
});
}
protected:
Valve *valve_;
};
} // namespace esphome::valve
@@ -0,0 +1,9 @@
valve:
- platform: template
name: Trigger Test Valve
id: trigger_test_valve
optimistic: true
on_open:
- logger.log: Valve fully open
on_closed:
- logger.log: Valve fully closed
@@ -0,0 +1,3 @@
packages:
valve: !include common.yaml
triggers: !include common-triggers.yaml