ESPHome 2025.5.0
Loading...
Searching...
No Matches
valve.h
Go to the documentation of this file.
1#pragma once
2
7#include "valve_traits.h"
8
9namespace esphome {
10namespace 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 if (!(obj)->get_device_class().empty()) { \
23 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
24 } \
25 }
26
27class Valve;
28
29class ValveCall {
30 public:
31 ValveCall(Valve *parent);
32
34 ValveCall &set_command(const char *command);
46 ValveCall &set_stop(bool stop);
47
49 void perform();
50
51 const optional<float> &get_position() const;
52 bool get_stop() const;
53 const optional<bool> &get_toggle() const;
54
55 protected:
56 void validate_();
57
59 bool stop_{false};
62};
63
66 float position;
67
69 ValveCall to_call(Valve *valve);
71 void apply(Valve *valve);
72} __attribute__((packed));
73
83
85
106 public:
107 explicit Valve();
108
116 float position;
117
120
121 void add_on_state_callback(std::function<void()> &&f);
122
130 void publish_state(bool save = true);
131
132 virtual ValveTraits get_traits() = 0;
133
135 bool is_fully_open() const;
137 bool is_fully_closed() const;
138
139 protected:
140 friend ValveCall;
141
142 virtual void control(const ValveCall &call) = 0;
143
145
147
149};
150
151} // namespace valve
152} // namespace esphome
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:55
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition valve.cpp:67
const optional< bool > & get_toggle() const
Definition valve.cpp:91
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:63
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:59
optional< bool > toggle_
Definition valve.h:61
const optional< float > & get_position() const
Definition valve.cpp:90
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition valve.cpp:37
optional< float > position_
Definition valve.h:60
bool get_stop() const
Definition valve.cpp:125
ValveCall(Valve *parent)
Definition valve.cpp:36
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:51
void perform()
Perform the valve call.
Definition valve.cpp:71
Base class for all valve devices.
Definition valve.h:105
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:157
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:130
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
CallbackManager< void()> state_callback_
Definition valve.h:146
ESPPreferenceObject rtc_
Definition valve.h:148
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:116
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:127
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:110
void add_on_state_callback(std::function< void()> &&f)
Definition valve.cpp:129
virtual ValveTraits get_traits()=0
float position
Definition cover.h:0
const char * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:21
const float VALVE_OPEN
Definition valve.cpp:9
const float VALVE_CLOSED
Definition valve.cpp:10
enum esphome::valve::ValveOperation __attribute__
ValveOperation
Enum encoding the current operation of a valve.
Definition valve.h:75
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:79
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:77
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Struct used to store the restored state of a valve.
Definition valve.h:65
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