ESPHome 2025.12.1
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 {
11namespace valve {
12
13const extern float VALVE_OPEN;
14const extern float VALVE_CLOSED;
15
16#define LOG_VALVE(prefix, type, obj) \
17 if ((obj) != nullptr) { \
18 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
19 auto traits_ = (obj)->get_traits(); \
20 if (traits_.get_is_assumed_state()) { \
21 ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
22 } \
23 if (!(obj)->get_device_class_ref().empty()) { \
24 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
25 } \
26 }
27
28class Valve;
29
30class ValveCall {
31 public:
32 ValveCall(Valve *parent);
33
35 ValveCall &set_command(const char *command);
47 ValveCall &set_stop(bool stop);
48
50 void perform();
51
52 const optional<float> &get_position() const;
53 bool get_stop() const;
54 const optional<bool> &get_toggle() const;
55
56 protected:
57 void validate_();
58
60 bool stop_{false};
63};
64
67 float position;
68
70 ValveCall to_call(Valve *valve);
72 void apply(Valve *valve);
73} __attribute__((packed));
74
84
85const LogString *valve_operation_to_str(ValveOperation op);
86
107 public:
108 explicit Valve();
109
117 float position;
118
121
122 void add_on_state_callback(std::function<void()> &&f);
123
131 void publish_state(bool save = true);
132
133 virtual ValveTraits get_traits() = 0;
134
136 bool is_fully_open() const;
138 bool is_fully_closed() const;
139
140 protected:
141 friend ValveCall;
142
143 virtual void control(const ValveCall &call) = 0;
144
146
148
150};
151
152} // namespace valve
153} // namespace esphome
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition valve.cpp:124
ValveCall & set_command_close()
Set the command to close the valve.
Definition valve.cpp:58
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition valve.cpp:70
const optional< bool > & get_toggle() const
Definition valve.cpp:94
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:66
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:62
optional< bool > toggle_
Definition valve.h:62
const optional< float > & get_position() const
Definition valve.cpp:93
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition valve.cpp:40
optional< float > position_
Definition valve.h:61
bool get_stop() const
Definition valve.cpp:128
ValveCall(Valve *parent)
Definition valve.cpp:39
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:54
void perform()
Perform the valve call.
Definition valve.cpp:74
Base class for all valve devices.
Definition valve.h:106
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:163
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:133
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
Definition valve.cpp:172
CallbackManager< void()> state_callback_
Definition valve.h:147
ESPPreferenceObject rtc_
Definition valve.h:149
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1....
Definition valve.cpp:171
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:117
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:130
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:111
void add_on_state_callback(std::function< void()> &&f)
Definition valve.cpp:132
virtual ValveTraits get_traits()=0
float position
Definition cover.h:0
const float VALVE_OPEN
Definition valve.cpp:12
const float VALVE_CLOSED
Definition valve.cpp:13
enum esphome::valve::ValveOperation __attribute__
ValveOperation
Enum encoding the current operation of a valve.
Definition valve.h:76
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:80
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:78
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:82
const LogString * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:24
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:66
void apply(Valve *valve)
Apply these settings to the valve.
Definition valve.cpp:179
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
Definition valve.cpp:174