ESPHome  2024.11.0
valve.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 #include "valve_traits.h"
8 
9 namespace esphome {
10 namespace valve {
11 
12 const extern float VALVE_OPEN;
13 const 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 
27 class Valve;
28 
29 class 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 
75 enum ValveOperation : uint8_t {
82 };
83 
85 
105 class Valve : public EntityBase, public EntityBase_DeviceClass {
106  public:
107  explicit Valve();
108 
116  float position;
117 
119  ValveCall make_call();
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 
144  optional<ValveRestoreState> restore_state_();
145 
146  CallbackManager<void()> state_callback_{};
147 
149 };
150 
151 } // namespace valve
152 } // namespace esphome
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition: valve.cpp:37
ValveCall & set_command_open()
Set the command to open the valve.
Definition: valve.cpp:51
ValveOperation
Enum encoding the current operation of a valve.
Definition: valve.h:75
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition: valve.h:116
float position
Definition: valve.h:14
const optional< bool > & get_toggle() const
Definition: valve.cpp:91
Struct used to store the restored state of a valve.
Definition: valve.h:65
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition: valve.cpp:59
The valve is currently opening.
Definition: valve.h:79
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition: valve.cpp:63
ValveCall & set_command_close()
Set the command to close the valve.
Definition: valve.cpp:55
ValveCall to_call(Valve *valve)
Convert this struct to a valve call that can be performed.
The valve is currently idle (not moving)
Definition: valve.h:77
The valve is currently closing.
Definition: valve.h:81
void perform()
Perform the valve call.
Definition: valve.cpp:71
void apply(Valve *valve)
Apply these settings to the valve.
bool get_stop() const
Definition: valve.cpp:125
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition: valve.cpp:121
optional< bool > toggle_
Definition: valve.h:61
const float VALVE_OPEN
Definition: valve.cpp:9
ESPPreferenceObject rtc_
Definition: valve.h:148
const char * valve_operation_to_str(ValveOperation op)
Definition: valve.cpp:21
enum esphome::valve::ValveOperation __attribute__
const optional< float > & get_position() const
Definition: valve.cpp:90
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition: valve.cpp:67
optional< float > position_
Definition: valve.h:60
const float VALVE_CLOSED
Definition: valve.cpp:10
ValveCall(Valve *parent)
Definition: valve.cpp:36