mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	[valve] Tidy up template publish action location (#8731)
This commit is contained in:
		@@ -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_))
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										24
									
								
								esphome/components/template/valve/automation.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								esphome/components/template/valve/automation.h
									
									
									
									
									
										Normal 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
 | 
			
		||||
@@ -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) {}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user