ESPHome 2026.5.0
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 ((obj) != nullptr) { \
17 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18 auto traits_ = (obj)->get_traits(); \
19 if (traits_.get_is_assumed_state()) { \
20 ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
21 } \
22 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
23 }
24
25class Valve;
26
27class ValveCall {
28 public:
29 ValveCall(Valve *parent);
30
32 ValveCall &set_command(const char *command);
44 ValveCall &set_stop(bool stop);
45
47 void perform();
48
49 const optional<float> &get_position() const;
50 bool get_stop() const;
51 const optional<bool> &get_toggle() const;
52
53 protected:
54 void validate_();
55
57 bool stop_{false};
58 optional<float> position_{};
59 optional<bool> toggle_{};
60};
61
64 float position;
65
67 ValveCall to_call(Valve *valve);
69 void apply(Valve *valve);
70} __attribute__((packed));
71
81
82const LogString *valve_operation_to_str(ValveOperation op);
83
103class Valve : public EntityBase {
104 public:
105 explicit Valve();
106
114 float position;
115
118
119 template<typename F> void add_on_state_callback(F &&f) { this->state_callback_.add(std::forward<F>(f)); }
120
128 void publish_state(bool save = true);
129
130 virtual ValveTraits get_traits() = 0;
131
133 bool is_fully_open() const;
135 bool is_fully_closed() const;
136
137 protected:
138 friend ValveCall;
139
140 virtual void control(const ValveCall &call) = 0;
141
142 optional<ValveRestoreState> restore_state_();
143
145
147};
148
149} // namespace esphome::valve
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
void add_on_state_callback(F &&f)
Definition valve.h:119
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 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:73
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:77
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:75
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:79
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: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