ESPHome 2026.3.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
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
50void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
51void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
53
58
60 LOG_VALVE("", "Template Valve", this);
61 ESP_LOGCONFIG(TAG,
62 " Has position: %s\n"
63 " Optimistic: %s",
64 YESNO(this->has_position_), YESNO(this->optimistic_));
65}
66
68 if (call.get_stop()) {
69 this->stop_prev_trigger_();
70 this->stop_trigger_.trigger();
72 this->publish_state();
73 }
74 if (call.get_toggle().has_value()) {
75 this->stop_prev_trigger_();
78 this->publish_state();
79 }
80 auto pos_val = call.get_position();
81 if (pos_val.has_value()) {
82 auto pos = *pos_val;
83 this->stop_prev_trigger_();
84
85 if (pos == VALVE_OPEN) {
86 this->open_trigger_.trigger();
88 } else if (pos == VALVE_CLOSED) {
89 this->close_trigger_.trigger();
91 } else {
93 }
94
95 if (this->optimistic_) {
96 this->position = pos;
97 }
98 }
99
100 this->publish_state();
101}
102
104 auto traits = ValveTraits();
105 traits.set_is_assumed_state(this->assumed_state_);
106 traits.set_supports_stop(this->has_stop_);
107 traits.set_supports_toggle(this->has_toggle_);
108 traits.set_supports_position(this->has_position_);
109 return traits;
110}
111
113
114void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
115void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
116void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
117
119 if (this->prev_command_trigger_ != nullptr) {
121 this->prev_command_trigger_ = nullptr;
122 }
123}
124
125} // 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:325
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:333
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 optional< bool > & get_toggle() const
Definition valve.cpp:90
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:159
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:129
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:115
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:29
const char *const TAG
Definition spi.cpp:7
const float VALVE_OPEN
Definition valve.cpp:14
const float VALVE_CLOSED
Definition valve.cpp:15
size_t size_t pos
Definition helpers.h:929