ESPHome 2026.3.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
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 if (call.get_position().has_value()) {
81 auto pos = *call.get_position();
82 this->stop_prev_trigger_();
83
84 if (pos == VALVE_OPEN) {
85 this->open_trigger_.trigger();
87 } else if (pos == VALVE_CLOSED) {
88 this->close_trigger_.trigger();
90 } else {
92 }
93
94 if (this->optimistic_) {
95 this->position = pos;
96 }
97 }
98
99 this->publish_state();
100}
101
103 auto traits = ValveTraits();
104 traits.set_is_assumed_state(this->assumed_state_);
105 traits.set_supports_stop(this->has_stop_);
106 traits.set_supports_toggle(this->has_toggle_);
107 traits.set_supports_position(this->has_position_);
108 return traits;
109}
110
112
113void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
114void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
115void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
116
118 if (this->prev_command_trigger_ != nullptr) {
120 this->prev_command_trigger_ = nullptr;
121 }
122}
123
124} // 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
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 optional< bool > & get_toggle() const
Definition valve.cpp:90
const optional< float > & get_position() const
Definition valve.cpp:89
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:854