[valve] Tidy up template publish action location (#8731)

This commit is contained in:
Jesse Hills 2025-05-09 18:05:26 +12:00 committed by GitHub
parent 1da8e99d27
commit 2b3757dff8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 23 deletions

View File

@ -21,6 +21,10 @@ from .. import template_ns
TemplateValve = template_ns.class_("TemplateValve", valve.Valve, cg.Component)
TemplateValvePublishAction = template_ns.class_(
"TemplateValvePublishAction", automation.Action, cg.Parented.template(TemplateValve)
)
TemplateValveRestoreMode = template_ns.enum("TemplateValveRestoreMode")
RESTORE_MODES = {
"NO_RESTORE": TemplateValveRestoreMode.VALVE_NO_RESTORE,
@ -90,10 +94,10 @@ async def to_code(config):
@automation.register_action(
"valve.template.publish",
valve.ValvePublishAction,
TemplateValvePublishAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(valve.Valve),
cv.GenerateID(): cv.use_id(TemplateValve),
cv.Exclusive(CONF_STATE, "pos"): cv.templatable(valve.validate_valve_state),
cv.Exclusive(CONF_POSITION, "pos"): cv.templatable(cv.percentage),
cv.Optional(CONF_CURRENT_OPERATION): cv.templatable(
@ -103,8 +107,8 @@ async def to_code(config):
),
)
async def valve_template_publish_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
if state_config := config.get(CONF_STATE):
template_ = await cg.templatable(state_config, args, float)
cg.add(var.set_position(template_))

View File

@ -0,0 +1,24 @@
#pragma once
#include "template_valve.h"
#include "esphome/core/automation.h"
namespace esphome {
namespace template_ {
template<typename... Ts> class TemplateValvePublishAction : public Action<Ts...>, public Parented<TemplateValve> {
TEMPLATABLE_VALUE(float, position)
TEMPLATABLE_VALUE(valve::ValveOperation, current_operation)
void play(Ts... x) override {
if (this->position_.has_value())
this->parent_->position = this->position_.value(x...);
if (this->current_operation_.has_value())
this->parent_->current_operation = this->current_operation_.value(x...);
this->parent_->publish_state();
}
};
} // namespace template_
} // namespace esphome

View File

@ -1,7 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "valve.h"
namespace esphome {
@ -67,24 +67,6 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
Valve *valve_;
};
template<typename... Ts> class ValvePublishAction : public Action<Ts...> {
public:
ValvePublishAction(Valve *valve) : valve_(valve) {}
TEMPLATABLE_VALUE(float, position)
TEMPLATABLE_VALUE(ValveOperation, current_operation)
void play(Ts... x) override {
if (this->position_.has_value())
this->valve_->position = this->position_.value(x...);
if (this->current_operation_.has_value())
this->valve_->current_operation = this->current_operation_.value(x...);
this->valve_->publish_state();
}
protected:
Valve *valve_;
};
template<typename... Ts> class ValveIsOpenCondition : public Condition<Ts...> {
public:
ValveIsOpenCondition(Valve *valve) : valve_(valve) {}

View File

@ -174,6 +174,8 @@ valve:
- logger.log: open_action
close_action:
- logger.log: close_action
- valve.template.publish:
state: CLOSED
stop_action:
- logger.log: stop_action
optimistic: true