From 2b3757dff85f8a91583cee25cf43b496927f430e Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Fri, 9 May 2025 18:05:26 +1200 Subject: [PATCH] [valve] Tidy up template publish action location (#8731) --- esphome/components/template/valve/__init__.py | 12 ++++++---- .../components/template/valve/automation.h | 24 +++++++++++++++++++ esphome/components/valve/automation.h | 20 +--------------- tests/components/template/common.yaml | 2 ++ 4 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 esphome/components/template/valve/automation.h diff --git a/esphome/components/template/valve/__init__.py b/esphome/components/template/valve/__init__.py index 12e5174168..c3f529ebd4 100644 --- a/esphome/components/template/valve/__init__.py +++ b/esphome/components/template/valve/__init__.py @@ -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_)) diff --git a/esphome/components/template/valve/automation.h b/esphome/components/template/valve/automation.h new file mode 100644 index 0000000000..af9b070c60 --- /dev/null +++ b/esphome/components/template/valve/automation.h @@ -0,0 +1,24 @@ +#pragma once + +#include "template_valve.h" + +#include "esphome/core/automation.h" + +namespace esphome { +namespace template_ { + +template class TemplateValvePublishAction : public Action, public Parented { + 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 diff --git a/esphome/components/valve/automation.h b/esphome/components/valve/automation.h index 24c94a5570..f2c06270c0 100644 --- a/esphome/components/valve/automation.h +++ b/esphome/components/valve/automation.h @@ -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 class ControlAction : public Action { Valve *valve_; }; -template class ValvePublishAction : public Action { - 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 class ValveIsOpenCondition : public Condition { public: ValveIsOpenCondition(Valve *valve) : valve_(valve) {} diff --git a/tests/components/template/common.yaml b/tests/components/template/common.yaml index 987849a80c..fd9342b3e5 100644 --- a/tests/components/template/common.yaml +++ b/tests/components/template/common.yaml @@ -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