ESPHome  2024.8.3
template_valve.cpp
Go to the documentation of this file.
1 #include "template_valve.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace template_ {
6 
7 using namespace esphome::valve;
8 
9 static const char *const TAG = "template.valve";
10 
12  : open_trigger_(new Trigger<>()),
13  close_trigger_(new Trigger<>),
14  stop_trigger_(new Trigger<>()),
15  toggle_trigger_(new Trigger<>()),
16  position_trigger_(new Trigger<float>()) {}
17 
19  ESP_LOGCONFIG(TAG, "Setting up template valve '%s'...", this->name_.c_str());
20  switch (this->restore_mode_) {
21  case VALVE_NO_RESTORE:
22  break;
23  case VALVE_RESTORE: {
24  auto restore = this->restore_state_();
25  if (restore.has_value())
26  restore->apply(this);
27  break;
28  }
30  auto restore = this->restore_state_();
31  if (restore.has_value()) {
32  restore->to_call(this).perform();
33  }
34  break;
35  }
36  }
37 }
38 
40  bool changed = false;
41 
42  if (this->state_f_.has_value()) {
43  auto s = (*this->state_f_)();
44  if (s.has_value()) {
45  auto pos = clamp(*s, 0.0f, 1.0f);
46  if (pos != this->position) {
47  this->position = pos;
48  changed = true;
49  }
50  }
51  }
52 
53  if (changed)
54  this->publish_state();
55 }
56 
57 void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
58 void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
59 void TemplateValve::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
61 
66 
68  LOG_VALVE("", "Template Valve", this);
69  ESP_LOGCONFIG(TAG, " Has position: %s", YESNO(this->has_position_));
70  ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
71 }
72 
74  if (call.get_stop()) {
75  this->stop_prev_trigger_();
76  this->stop_trigger_->trigger();
78  this->publish_state();
79  }
80  if (call.get_toggle().has_value()) {
81  this->stop_prev_trigger_();
82  this->toggle_trigger_->trigger();
84  this->publish_state();
85  }
86  if (call.get_position().has_value()) {
87  auto pos = *call.get_position();
88  this->stop_prev_trigger_();
89 
90  if (pos == VALVE_OPEN) {
91  this->open_trigger_->trigger();
93  } else if (pos == VALVE_CLOSED) {
94  this->close_trigger_->trigger();
96  } else {
97  this->position_trigger_->trigger(pos);
98  }
99 
100  if (this->optimistic_) {
101  this->position = pos;
102  }
103  }
104 
105  this->publish_state();
106 }
107 
109  auto traits = ValveTraits();
110  traits.set_is_assumed_state(this->assumed_state_);
111  traits.set_supports_stop(this->has_stop_);
112  traits.set_supports_toggle(this->has_toggle_);
113  traits.set_supports_position(this->has_position_);
114  return traits;
115 }
116 
118 
119 void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
120 void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
121 void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
122 
124  if (this->prev_command_trigger_ != nullptr) {
126  this->prev_command_trigger_ = nullptr;
127  }
128 }
129 
130 } // namespace template_
131 } // namespace esphome
void set_has_position(bool has_position)
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition: valve.h:116
void set_has_toggle(bool has_toggle)
void set_state_lambda(std::function< optional< float >()> &&f)
void control(const valve::ValveCall &call) override
const optional< bool > & get_toggle() const
Definition: valve.cpp:91
bool has_value() const
Definition: optional.h:87
float get_setup_priority() const override
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void set_optimistic(bool optimistic)
optional< std::function< optional< float >)> > state_f_
const char *const TAG
Definition: spi.cpp:8
Trigger< float > * position_trigger_
bool get_stop() const
Definition: valve.cpp:125
const float VALVE_OPEN
Definition: valve.cpp:9
Trigger< float > * get_position_trigger() const
optional< ValveRestoreState > restore_state_()
Definition: valve.cpp:157
constexpr const char * c_str() const
Definition: string_ref.h:68
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
valve::ValveTraits get_traits() override
const optional< float > & get_position() const
Definition: valve.cpp:90
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void publish_state(bool save=true)
Publish the current state of the valve.
Definition: valve.cpp:130
TemplateValveRestoreMode restore_mode_
void stop_action()
Stop any action connected to this trigger.
Definition: automation.h:103
const float VALVE_CLOSED
Definition: valve.cpp:10
void set_assumed_state(bool assumed_state)