7 static const char *
const TAG =
"valve";
13 if (pos == VALVE_OPEN) {
15 }
else if (pos == VALVE_CLOSED) {
38 if (strcasecmp(command,
"OPEN") == 0) {
40 }
else if (strcasecmp(command,
"CLOSE") == 0) {
42 }
else if (strcasecmp(command,
"STOP") == 0) {
44 }
else if (strcasecmp(command,
"TOGGLE") == 0) {
76 ESP_LOGD(TAG,
" Command: STOP");
79 if (traits.get_supports_position()) {
80 ESP_LOGD(TAG,
" Position: %.0f%%", *this->
position_ * 100.0f);
86 ESP_LOGD(TAG,
" Command: TOGGLE");
96 if (!traits.get_supports_position() && pos != VALVE_OPEN && pos !=
VALVE_CLOSED) {
97 ESP_LOGW(TAG,
"'%s' - This valve device does not support setting position!", this->
parent_->
get_name().
c_str());
99 }
else if (pos < 0.0f || pos > 1.0f) {
100 ESP_LOGW(TAG,
"'%s' - Position %.2f is out of range [0.0 - 1.0]", this->
parent_->
get_name().
c_str(), pos);
105 if (!traits.get_supports_toggle()) {
106 ESP_LOGW(TAG,
"'%s' - This valve device does not support toggle!", this->
parent_->
get_name().
c_str());
112 ESP_LOGW(TAG,
"Cannot set position when stopping a valve!");
116 ESP_LOGW(TAG,
"Cannot set toggle when stopping a valve!");
133 ESP_LOGD(TAG,
"'%s' - Publishing:", this->name_.c_str());
134 auto traits = this->get_traits();
135 if (traits.get_supports_position()) {
136 ESP_LOGD(TAG,
" Position: %.0f%%", this->
position * 100.0f);
139 ESP_LOGD(TAG,
" State: OPEN");
140 }
else if (this->
position == VALVE_CLOSED) {
141 ESP_LOGD(TAG,
" State: CLOSED");
143 ESP_LOGD(TAG,
" State: UNKNOWN");
148 this->state_callback_.call();
152 memset(&restore, 0,
sizeof(restore));
154 this->rtc_.save(&restore);
160 if (!this->rtc_.load(&recovered))
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
ValveCall & set_command_open()
Set the command to open the valve.
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
ValveOperation
Enum encoding the current operation of a valve.
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0...
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
void add_on_state_callback(std::function< void()> &&f)
virtual void control(const ValveCall &call)=0
const optional< bool > & get_toggle() const
Struct used to store the restored state of a valve.
void apply(Valve *valve)
Apply these settings to the valve.
ValveCall & set_command_stop()
Set the command to stop the valve.
virtual ValveTraits get_traits()=0
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
The valve is currently opening.
ValveCall & set_command_toggle()
Set the command to toggle the valve.
ValveCall make_call()
Construct a new valve call used to control the valve.
ValveCall & set_command_close()
Set the command to close the valve.
The valve is currently idle (not moving)
The valve is currently closing.
void perform()
Perform the valve call.
ESPPreferences * global_preferences
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1...
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
optional< ValveRestoreState > restore_state_()
constexpr const char * c_str() const
const char * valve_operation_to_str(ValveOperation op)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
const optional< float > & get_position() const
Implementation of SPI Controller mode.
Base class for all valve devices.
void publish_state(bool save=true)
Publish the current state of the valve.
ValveCall & set_position(float position)
Set the call to a certain target position.
optional< float > position_
const StringRef & get_name() const
const char * valve_command_to_str(float pos)