ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
valve.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/log.h"
8#include "valve_traits.h"
9
10namespace esphome::valve {
11
12const extern float VALVE_OPEN;
13const extern float VALVE_CLOSED;
14
15#define LOG_VALVE(prefix, type, obj) \
16 if (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
17 auto traits_ = (obj)->get_traits(); \
18 if (traits_.get_is_assumed_state()) { \
19 ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
20 } \
21 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
22 }
23
24class Valve;
25
26class ValveCall {
27 public:
28 ValveCall(Valve *parent);
29
31 ValveCall &set_command(const char *command);
43 ValveCall &set_stop(bool stop);
44
46 void perform();
47
48 const optional<float> &get_position() const;
49 bool get_stop() const { return this->stop_; }
50 const optional<bool> &get_toggle() const;
51
52 protected:
53 void validate_();
54
56 bool stop_{false};
57 optional<float> position_{};
58 optional<bool> toggle_{};
59};
60
63 float position;
64
66 ValveCall to_call(Valve *valve);
68 void apply(Valve *valve);
69} __attribute__((packed));
70
80
81const LogString *valve_operation_to_str(ValveOperation op);
82
102class Valve : public EntityBase {
103 public:
104 explicit Valve();
105
113 float position;
114
116 ValveCall make_call() { return {this}; }
117
118 template<typename F> void add_on_state_callback(F &&f) { this->state_callback_.add(std::forward<F>(f)); }
119
127 void publish_state(bool save = true);
128
129 virtual ValveTraits get_traits() = 0;
130
132 bool is_fully_open() const { return this->position == VALVE_OPEN; }
134 bool is_fully_closed() const { return this->position == VALVE_CLOSED; }
135
136 protected:
137 friend ValveCall;
138
139 virtual void control(const ValveCall &call) = 0;
140
141 optional<ValveRestoreState> restore_state_();
142
144
146};
147
148} // namespace esphome::valve
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition valve.cpp:121
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:58
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:57
bool get_stop() const
Definition valve.h:49
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:102
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:155
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:125
LazyCallbackManager< void()> state_callback_
Definition valve.h:143
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
Definition valve.h:134
void add_on_state_callback(F &&f)
Definition valve.h:118
ESPPreferenceObject rtc_
Definition valve.h:145
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1....
Definition valve.h:132
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:113
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.h:116
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:107
virtual ValveTraits get_traits()=0
float position
Definition cover.h:0
const float VALVE_OPEN
Definition valve.cpp:13
const float VALVE_CLOSED
Definition valve.cpp:14
enum esphome::valve::ValveOperation __attribute__
ValveOperation
Enum encoding the current operation of a valve.
Definition valve.h:72
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:76
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:74
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:78
const LogString * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:28
Struct used to store the restored state of a valve.
Definition valve.h:62
void apply(Valve *valve)
Apply these settings to the valve.
Definition valve.cpp:168
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
Definition valve.cpp:163