ESPHome 2026.2.4
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 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
24 }
25
26class Valve;
27
28class ValveCall {
29 public:
30 ValveCall(Valve *parent);
31
33 ValveCall &set_command(const char *command);
45 ValveCall &set_stop(bool stop);
46
48 void perform();
49
50 const optional<float> &get_position() const;
51 bool get_stop() const;
52 const optional<bool> &get_toggle() const;
53
54 protected:
55 void validate_();
56
58 bool stop_{false};
61};
62
65 float position;
66
68 ValveCall to_call(Valve *valve);
70 void apply(Valve *valve);
71} __attribute__((packed));
72
82
83const LogString *valve_operation_to_str(ValveOperation op);
84
105 public:
106 explicit Valve();
107
115 float position;
116
119
120 void add_on_state_callback(std::function<void()> &&f);
121
129 void publish_state(bool save = true);
130
131 virtual ValveTraits get_traits() = 0;
132
134 bool is_fully_open() const;
136 bool is_fully_closed() const;
137
138 protected:
139 friend ValveCall;
140
141 virtual void control(const ValveCall &call) = 0;
142
144
146
148};
149
150} // namespace valve
151} // namespace esphome
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition valve.cpp:120
ValveCall & set_command_close()
Set the command to close the valve.
Definition valve.cpp:54
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition valve.cpp:66
const optional< bool > & get_toggle() const
Definition valve.cpp:90
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:62
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:58
optional< bool > toggle_
Definition valve.h:60
const optional< float > & get_position() const
Definition valve.cpp:89
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition valve.cpp:36
optional< float > position_
Definition valve.h:59
bool get_stop() const
Definition valve.cpp:124
ValveCall(Valve *parent)
Definition valve.cpp:35
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:50
void perform()
Perform the valve call.
Definition valve.cpp:70
Base class for all valve devices.
Definition valve.h:104
optional< ValveRestoreState > restore_state_()
Definition valve.cpp:159
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:129
LazyCallbackManager< void()> state_callback_
Definition valve.h:145
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0....
Definition valve.cpp:168
ESPPreferenceObject rtc_
Definition valve.h:147
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1....
Definition valve.cpp:167
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:115
virtual void control(const ValveCall &call)=0
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:126
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:109
void add_on_state_callback(std::function< void()> &&f)
Definition valve.cpp:128
virtual ValveTraits get_traits()=0
float position
Definition cover.h:0
const float VALVE_OPEN
Definition valve.cpp:14
const float VALVE_CLOSED
Definition valve.cpp:15
enum esphome::valve::ValveOperation __attribute__
ValveOperation
Enum encoding the current operation of a valve.
Definition valve.h:74
@ VALVE_OPERATION_OPENING
The valve is currently opening.
Definition valve.h:78
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:76
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
Definition valve.h:80
const LogString * valve_operation_to_str(ValveOperation op)
Definition valve.cpp:29
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:64
void apply(Valve *valve)
Apply these settings to the valve.
Definition valve.cpp:175
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
Definition valve.cpp:170