ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
valve.cpp
Go to the documentation of this file.
1#include "valve.h"
2#include "esphome/core/log.h"
3#include <strings.h>
4
5namespace esphome {
6namespace valve {
7
8static const char *const TAG = "valve";
9
10const float VALVE_OPEN = 1.0f;
11const float VALVE_CLOSED = 0.0f;
12
13const char *valve_command_to_str(float pos) {
14 if (pos == VALVE_OPEN) {
15 return "OPEN";
16 } else if (pos == VALVE_CLOSED) {
17 return "CLOSE";
18 } else {
19 return "UNKNOWN";
20 }
21}
23 switch (op) {
25 return "IDLE";
27 return "OPENING";
29 return "CLOSING";
30 default:
31 return "UNKNOWN";
32 }
33}
34
36
37ValveCall::ValveCall(Valve *parent) : parent_(parent) {}
38ValveCall &ValveCall::set_command(const char *command) {
39 if (strcasecmp(command, "OPEN") == 0) {
40 this->set_command_open();
41 } else if (strcasecmp(command, "CLOSE") == 0) {
42 this->set_command_close();
43 } else if (strcasecmp(command, "STOP") == 0) {
44 this->set_command_stop();
45 } else if (strcasecmp(command, "TOGGLE") == 0) {
46 this->set_command_toggle();
47 } else {
48 ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command);
49 }
50 return *this;
51}
53 this->position_ = VALVE_OPEN;
54 return *this;
55}
57 this->position_ = VALVE_CLOSED;
58 return *this;
59}
61 this->stop_ = true;
62 return *this;
63}
65 this->toggle_ = true;
66 return *this;
67}
69 this->position_ = position;
70 return *this;
71}
73 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
74 auto traits = this->parent_->get_traits();
75 this->validate_();
76 if (this->stop_) {
77 ESP_LOGD(TAG, " Command: STOP");
78 }
79 if (this->position_.has_value()) {
80 if (traits.get_supports_position()) {
81 ESP_LOGD(TAG, " Position: %.0f%%", *this->position_ * 100.0f);
82 } else {
83 ESP_LOGD(TAG, " Command: %s", valve_command_to_str(*this->position_));
84 }
85 }
86 if (this->toggle_.has_value()) {
87 ESP_LOGD(TAG, " Command: TOGGLE");
88 }
89 this->parent_->control(*this);
90}
91const optional<float> &ValveCall::get_position() const { return this->position_; }
92const optional<bool> &ValveCall::get_toggle() const { return this->toggle_; }
94 auto traits = this->parent_->get_traits();
95 if (this->position_.has_value()) {
96 auto pos = *this->position_;
97 if (!traits.get_supports_position() && pos != VALVE_OPEN && pos != VALVE_CLOSED) {
98 ESP_LOGW(TAG, "'%s' - This valve device does not support setting position!", this->parent_->get_name().c_str());
99 this->position_.reset();
100 } else if (pos < 0.0f || pos > 1.0f) {
101 ESP_LOGW(TAG, "'%s' - Position %.2f is out of range [0.0 - 1.0]", this->parent_->get_name().c_str(), pos);
102 this->position_ = clamp(pos, 0.0f, 1.0f);
103 }
104 }
105 if (this->toggle_.has_value()) {
106 if (!traits.get_supports_toggle()) {
107 ESP_LOGW(TAG, "'%s' - This valve device does not support toggle!", this->parent_->get_name().c_str());
108 this->toggle_.reset();
109 }
110 }
111 if (this->stop_) {
112 if (this->position_.has_value()) {
113 ESP_LOGW(TAG, "Cannot set position when stopping a valve!");
114 this->position_.reset();
115 }
116 if (this->toggle_.has_value()) {
117 ESP_LOGW(TAG, "Cannot set toggle when stopping a valve!");
118 this->toggle_.reset();
119 }
120 }
121}
123 this->stop_ = stop;
124 return *this;
125}
126bool ValveCall::get_stop() const { return this->stop_; }
127
128ValveCall Valve::make_call() { return {this}; }
129
130void Valve::add_on_state_callback(std::function<void()> &&f) { this->state_callback_.add(std::move(f)); }
131void Valve::publish_state(bool save) {
132 this->position = clamp(this->position, 0.0f, 1.0f);
133
134 ESP_LOGD(TAG, "'%s' - Publishing:", this->name_.c_str());
135 auto traits = this->get_traits();
136 if (traits.get_supports_position()) {
137 ESP_LOGD(TAG, " Position: %.0f%%", this->position * 100.0f);
138 } else {
139 if (this->position == VALVE_OPEN) {
140 ESP_LOGD(TAG, " State: OPEN");
141 } else if (this->position == VALVE_CLOSED) {
142 ESP_LOGD(TAG, " State: CLOSED");
143 } else {
144 ESP_LOGD(TAG, " State: UNKNOWN");
145 }
146 }
147 ESP_LOGD(TAG, " Current Operation: %s", valve_operation_to_str(this->current_operation));
148
149 this->state_callback_.call();
150
151 if (save) {
152 ValveRestoreState restore{};
153 memset(&restore, 0, sizeof(restore));
154 restore.position = this->position;
155 this->rtc_.save(&restore);
156 }
157}
160 ValveRestoreState recovered{};
161 if (!this->rtc_.load(&recovered))
162 return {};
163 return recovered;
164}
165
166bool Valve::is_fully_open() const { return this->position == VALVE_OPEN; }
167bool Valve::is_fully_closed() const { return this->position == VALVE_CLOSED; }
168
170 auto call = valve->make_call();
171 call.set_position(this->position);
172 return call;
173}
175 valve->position = this->position;
176 valve->publish_state();
177}
178
179} // namespace valve
180} // namespace esphome
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
const StringRef & get_name() const
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
constexpr const char * c_str() const
Definition string_ref.h:69
bool has_value() const
Definition optional.h:92
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition valve.cpp:122
ValveCall & set_command_close()
Set the command to close the valve.
Definition valve.cpp:56
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition valve.cpp:68
const optional< bool > & get_toggle() const
Definition valve.cpp:92
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:64
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:60
optional< bool > toggle_
Definition valve.h:61
const optional< float > & get_position() const
Definition valve.cpp:91
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition valve.cpp:38
optional< float > position_
Definition valve.h:60
bool get_stop() const
Definition valve.cpp:126
ValveCall(Valve *parent)
Definition valve.cpp:37
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:52
void perform()
Perform the valve call.
Definition valve.cpp:72
Base class for all valve devices.
Definition valve.h:105
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:158
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:131
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
Definition valve.cpp:167
CallbackManager< void()> state_callback_
Definition valve.h:146
ESPPreferenceObject rtc_
Definition valve.h:148
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1....
Definition valve.cpp:166
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:116
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:128
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:110
void add_on_state_callback(std::function< void()> &&f)
Definition valve.cpp:130
virtual ValveTraits get_traits()=0
float position
Definition cover.h:0
const char *const TAG
Definition spi.cpp:8
const char * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:22
const float VALVE_OPEN
Definition valve.cpp:10
const float VALVE_CLOSED
Definition valve.cpp:11
const char * valve_command_to_str(float pos)
Definition valve.cpp:13
ValveOperation
Enum encoding the current operation of a valve.
Definition valve.h:75
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:79
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:77
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences
Struct used to store the restored state of a valve.
Definition valve.h:65
void apply(Valve *valve)
Apply these settings to the valve.
Definition valve.cpp:174
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
Definition valve.cpp:169