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