ESPHome 2026.11.0-dev
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::template_ {
5
6using namespace esphome::valve;
7
8ESPHOME_LOG_TAG(TAG, "template.valve");
9
11
13 switch (this->restore_mode_) {
15 break;
16 case VALVE_RESTORE: {
17 auto restore = this->restore_state_();
18 if (restore.has_value())
19 restore->apply(this);
20 break;
21 }
23 auto restore = this->restore_state_();
24 if (restore.has_value()) {
25 restore->to_call(this).perform();
26 }
27 break;
28 }
29 }
30 if (!this->state_f_.has_value())
31 this->disable_loop();
32}
33
35 bool changed = false;
36
37 auto s = this->state_f_();
38 if (s.has_value()) {
39 auto pos = clamp(*s, 0.0f, 1.0f);
40 if (pos != this->position) {
41 this->position = pos;
42 changed = true;
43 }
44 }
45
46 if (changed)
47 this->publish_state();
48}
49
51
56
58 LOG_VALVE("", "Template Valve", this);
59 ESP_LOGCONFIG(TAG,
60 " Has position: %s\n"
61 " Optimistic: %s",
62 YESNO(this->has_position_), YESNO(this->optimistic_));
63}
64
66 if (call.get_stop()) {
67 this->stop_prev_trigger_();
68 this->stop_trigger_.trigger();
70 this->publish_state();
71 }
72 if (call.get_toggle().has_value()) {
73 this->stop_prev_trigger_();
76 this->publish_state();
77 }
78 auto pos_val = call.get_position();
79 if (pos_val.has_value()) {
80 auto pos = *pos_val;
81 this->stop_prev_trigger_();
82
83 if (pos == VALVE_OPEN) {
84 this->open_trigger_.trigger();
86 } else if (pos == VALVE_CLOSED) {
87 this->close_trigger_.trigger();
89 } else {
91 }
92
93 if (this->optimistic_) {
94 this->position = pos;
95 }
96 }
97
98 this->publish_state();
99}
100
102 auto traits = ValveTraits();
103 traits.set_is_assumed_state(this->assumed_state_);
104 traits.set_supports_stop(this->has_stop_);
105 traits.set_supports_toggle(this->has_toggle_);
106 traits.set_supports_position(this->has_position_);
107 return traits;
108}
109
111
113 if (this->prev_command_trigger_ != nullptr) {
115 this->prev_command_trigger_ = nullptr;
116 }
117}
118
119} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
bool has_value() const
Check if a lambda is set.
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:564
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:556
float get_setup_priority() const override
TemplateLambda< float > state_f_
valve::ValveTraits get_traits() override
void control(const valve::ValveCall &call) override
TemplateValveRestoreMode restore_mode_
Trigger< float > * get_position_trigger()
const optional< bool > & get_toggle() const
Definition valve.cpp:89
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:155
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:125
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:113
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
const float VALVE_OPEN
Definition valve.cpp:13
const float VALVE_CLOSED
Definition valve.cpp:14
size_t size_t pos
Definition helpers.h:1255