ESPHome 2025.9.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 {
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 switch (this->restore_mode_) {
21 break;
22 case VALVE_RESTORE: {
23 auto restore = this->restore_state_();
24 if (restore.has_value())
25 restore->apply(this);
26 break;
27 }
29 auto restore = this->restore_state_();
30 if (restore.has_value()) {
31 restore->to_call(this).perform();
32 }
33 break;
34 }
35 }
36}
37
39 bool changed = false;
40
41 if (this->state_f_.has_value()) {
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
52 if (changed)
53 this->publish_state();
54}
55
56void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
57void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
58void TemplateValve::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
60
65
67 LOG_VALVE("", "Template Valve", this);
68 ESP_LOGCONFIG(TAG,
69 " Has position: %s\n"
70 " Optimistic: %s",
71 YESNO(this->has_position_), YESNO(this->optimistic_));
72}
73
75 if (call.get_stop()) {
76 this->stop_prev_trigger_();
77 this->stop_trigger_->trigger();
79 this->publish_state();
80 }
81 if (call.get_toggle().has_value()) {
82 this->stop_prev_trigger_();
83 this->toggle_trigger_->trigger();
85 this->publish_state();
86 }
87 if (call.get_position().has_value()) {
88 auto pos = *call.get_position();
89 this->stop_prev_trigger_();
90
91 if (pos == VALVE_OPEN) {
92 this->open_trigger_->trigger();
94 } else if (pos == VALVE_CLOSED) {
95 this->close_trigger_->trigger();
97 } else {
98 this->position_trigger_->trigger(pos);
99 }
100
101 if (this->optimistic_) {
102 this->position = pos;
103 }
104 }
105
106 this->publish_state();
107}
108
110 auto traits = ValveTraits();
111 traits.set_is_assumed_state(this->assumed_state_);
112 traits.set_supports_stop(this->has_stop_);
113 traits.set_supports_toggle(this->has_toggle_);
114 traits.set_supports_position(this->has_position_);
115 return traits;
116}
117
119
120void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
121void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
122void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
123
125 if (this->prev_command_trigger_ != nullptr) {
127 this->prev_command_trigger_ = nullptr;
128 }
129}
130
131} // namespace template_
132} // namespace esphome
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:153
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
bool has_value() const
Definition optional.h:92
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:49
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