ESPHome 2025.12.0
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
8static const char *const TAG = "template.valve";
9
11 : open_trigger_(new Trigger<>()),
12 close_trigger_(new Trigger<>),
13 stop_trigger_(new Trigger<>()),
14 toggle_trigger_(new Trigger<>()),
15 position_trigger_(new Trigger<float>()) {}
16
18 switch (this->restore_mode_) {
20 break;
21 case VALVE_RESTORE: {
22 auto restore = this->restore_state_();
23 if (restore.has_value())
24 restore->apply(this);
25 break;
26 }
28 auto restore = this->restore_state_();
29 if (restore.has_value()) {
30 restore->to_call(this).perform();
31 }
32 break;
33 }
34 }
35 if (!this->state_f_.has_value())
36 this->disable_loop();
37}
38
40 bool changed = false;
41
42 auto s = this->state_f_();
43 if (s.has_value()) {
44 auto pos = clamp(*s, 0.0f, 1.0f);
45 if (pos != this->position) {
46 this->position = pos;
47 changed = true;
48 }
49 }
50
51 if (changed)
52 this->publish_state();
53}
54
55void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
56void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
58
63
65 LOG_VALVE("", "Template Valve", this);
66 ESP_LOGCONFIG(TAG,
67 " Has position: %s\n"
68 " Optimistic: %s",
69 YESNO(this->has_position_), YESNO(this->optimistic_));
70}
71
73 if (call.get_stop()) {
74 this->stop_prev_trigger_();
75 this->stop_trigger_->trigger();
77 this->publish_state();
78 }
79 if (call.get_toggle().has_value()) {
80 this->stop_prev_trigger_();
81 this->toggle_trigger_->trigger();
83 this->publish_state();
84 }
85 if (call.get_position().has_value()) {
86 auto pos = *call.get_position();
87 this->stop_prev_trigger_();
88
89 if (pos == VALVE_OPEN) {
90 this->open_trigger_->trigger();
92 } else if (pos == VALVE_CLOSED) {
93 this->close_trigger_->trigger();
95 } else {
96 this->position_trigger_->trigger(pos);
97 }
98
99 if (this->optimistic_) {
100 this->position = pos;
101 }
102 }
103
104 this->publish_state();
105}
106
108 auto traits = ValveTraits();
109 traits.set_is_assumed_state(this->assumed_state_);
110 traits.set_supports_stop(this->has_stop_);
111 traits.set_supports_toggle(this->has_toggle_);
112 traits.set_supports_position(this->has_position_);
113 return traits;
114}
115
117
118void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
119void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
120void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
121
123 if (this->prev_command_trigger_ != nullptr) {
125 this->prev_command_trigger_ = nullptr;
126 }
127}
128
129} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
bool has_value() const
Check if a lambda is set.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:212
bool has_value() const
Definition optional.h:92
void set_optimistic(bool optimistic)
float get_setup_priority() const override
void set_has_position(bool has_position)
void set_assumed_state(bool assumed_state)
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
const optional< bool > & get_toggle() const
Definition valve.cpp:94
const optional< float > & get_position() const
Definition valve.cpp:93
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:163
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:133
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:117
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
const char *const TAG
Definition spi.cpp:8
const float VALVE_OPEN
Definition valve.cpp:12
const float VALVE_CLOSED
Definition valve.cpp:13