8static const char *
const TAG =
"valve";
39 if (strcasecmp(command,
"OPEN") == 0) {
41 }
else if (strcasecmp(command,
"CLOSE") == 0) {
43 }
else if (strcasecmp(command,
"STOP") == 0) {
45 }
else if (strcasecmp(command,
"TOGGLE") == 0) {
77 ESP_LOGD(TAG,
" Command: STOP");
80 if (traits.get_supports_position()) {
81 ESP_LOGD(TAG,
" Position: %.0f%%", *this->
position_ * 100.0f);
87 ESP_LOGD(TAG,
" Command: TOGGLE");
98 ESP_LOGW(TAG,
"'%s' - This valve device does not support setting position!", this->
parent_->
get_name().
c_str());
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);
106 if (!traits.get_supports_toggle()) {
107 ESP_LOGW(TAG,
"'%s' - This valve device does not support toggle!", this->
parent_->
get_name().
c_str());
113 ESP_LOGW(TAG,
"Cannot set position when stopping a valve!");
117 ESP_LOGW(TAG,
"Cannot set toggle when stopping a valve!");
134 ESP_LOGD(TAG,
"'%s' - Publishing:", this->
name_.
c_str());
136 if (traits.get_supports_position()) {
137 ESP_LOGD(TAG,
" Position: %.0f%%", this->
position * 100.0f);
140 ESP_LOGD(TAG,
" State: OPEN");
141 }
else if (this->
position == VALVE_CLOSED) {
142 ESP_LOGD(TAG,
" State: CLOSED");
144 ESP_LOGD(TAG,
" State: UNKNOWN");
153 memset(&restore, 0,
sizeof(restore));
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
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
ValveCall & set_command_close()
Set the command to close the valve.
ValveCall & set_position(float position)
Set the call to a certain target position.
const optional< bool > & get_toggle() const
ValveCall & set_command_toggle()
Set the command to toggle the valve.
ValveCall & set_command_stop()
Set the command to stop the valve.
const optional< float > & get_position() const
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
optional< float > position_
ValveCall & set_command_open()
Set the command to open the valve.
void perform()
Perform the valve call.
Base class for all valve devices.
optional< ValveRestoreState > restore_state_()
void publish_state(bool save=true)
Publish the current state of the valve.
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
CallbackManager< void()> state_callback_
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1....
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
void add_on_state_callback(std::function< void()> &&f)
virtual ValveTraits get_traits()=0
const char * valve_operation_to_str(ValveOperation op)
const char * valve_command_to_str(float pos)
ValveOperation
Enum encoding the current operation of a valve.
@ VALVE_OPERATION_OPENING
The valve is currently opening.
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Providing packet encoding functions for exchanging data with a remote host.
ESPPreferences * global_preferences
Struct used to store the restored state of a valve.
void apply(Valve *valve)
Apply these settings to the valve.
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.