ESPHome 2025.6.2
Loading...
Searching...
No Matches
template_valve.cpp
Go to the documentation of this file.
1#include "template_valve.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace template_ {
6
7using namespace esphome::valve;
8
9static 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, "Running setup for '%s'", this->name_.c_str());
20 switch (this->restore_mode_) {
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
57void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
58void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
59void 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,
70 " Has position: %s\n"
71 " Optimistic: %s",
72 YESNO(this->has_position_), YESNO(this->optimistic_));
73}
74
76 if (call.get_stop()) {
77 this->stop_prev_trigger_();
78 this->stop_trigger_->trigger();
80 this->publish_state();
81 }
82 if (call.get_toggle().has_value()) {
83 this->stop_prev_trigger_();
84 this->toggle_trigger_->trigger();
86 this->publish_state();
87 }
88 if (call.get_position().has_value()) {
89 auto pos = *call.get_position();
90 this->stop_prev_trigger_();
91
92 if (pos == VALVE_OPEN) {
93 this->open_trigger_->trigger();
95 } else if (pos == VALVE_CLOSED) {
96 this->close_trigger_->trigger();
98 } else {
99 this->position_trigger_->trigger(pos);
100 }
101
102 if (this->optimistic_) {
103 this->position = pos;
104 }
105 }
106
107 this->publish_state();
108}
109
111 auto traits = ValveTraits();
112 traits.set_is_assumed_state(this->assumed_state_);
113 traits.set_supports_stop(this->has_stop_);
114 traits.set_supports_toggle(this->has_toggle_);
115 traits.set_supports_position(this->has_position_);
116 return traits;
117}
118
120
121void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
122void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
123void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
124
126 if (this->prev_command_trigger_ != nullptr) {
128 this->prev_command_trigger_ = nullptr;
129 }
130}
131
132} // namespace template_
133} // namespace esphome
constexpr const char * c_str() const
Definition string_ref.h:69
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:104
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
bool has_value() const
Definition optional.h:87
void set_optimistic(bool optimistic)
float get_setup_priority() const override
optional< std::function< optional< float >()> > state_f_
void set_state_lambda(std::function< optional< float >()> &&f)
void set_has_position(bool has_position)
void set_assumed_state(bool assumed_state)
valve::ValveTraits get_traits() override
void control(const valve::ValveCall &call) override
TemplateValveRestoreMode restore_mode_
Trigger< float > * get_position_trigger() const
const optional< bool > & get_toggle() const
Definition valve.cpp:91
const optional< float > & get_position() const
Definition valve.cpp:90
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:157
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:130
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:116
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:19
const char *const TAG
Definition spi.cpp:8
const float VALVE_OPEN
Definition valve.cpp:9
const float VALVE_CLOSED
Definition valve.cpp:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:102