ESPHome 2026.5.1
Loading...
Searching...
No Matches
demo_valve.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace esphome::demo {
6
7enum class DemoValveType {
8 TYPE_1,
9 TYPE_2,
10};
11
12class DemoValve : public valve::Valve {
13 public:
15 valve::ValveTraits traits;
16 if (this->type_ == DemoValveType::TYPE_2) {
17 traits.set_supports_position(true);
18 traits.set_supports_toggle(true);
19 traits.set_supports_stop(true);
20 }
21 return traits;
22 }
23
25
26 protected:
27 void control(const valve::ValveCall &call) override {
28 auto pos = call.get_position();
29 if (pos.has_value()) {
30 this->position = *pos;
31 this->publish_state();
32 return;
33 }
34 auto toggle = call.get_toggle();
35 if (toggle.has_value()) {
36 if (*toggle) {
37 if (this->position == valve::VALVE_OPEN) {
39 this->publish_state();
40 } else {
42 this->publish_state();
43 }
44 }
45 return;
46 } else if (call.get_stop()) {
48 this->publish_state(); // Keep the current position
49 return;
50 }
51 }
53};
54
55} // namespace esphome::demo
void control(const valve::ValveCall &call) override
Definition demo_valve.h:27
valve::ValveTraits get_traits() override
Definition demo_valve.h:14
void set_type(DemoValveType type)
Definition demo_valve.h:24
const optional< bool > & get_toggle() const
Definition valve.cpp:89
const optional< float > & get_position() const
Definition valve.cpp:88
bool get_stop() const
Definition valve.cpp:123
Base class for all valve devices.
Definition valve.h:103
void publish_state(bool save=true)
Publish the current state of the valve.
Definition valve.cpp:127
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition valve.h:114
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition valve.h:108
void set_supports_position(bool supports_position)
void set_supports_stop(bool supports_stop)
void set_supports_toggle(bool supports_toggle)
uint16_t type
const float VALVE_OPEN
Definition valve.cpp:13
const float VALVE_CLOSED
Definition valve.cpp:14
@ VALVE_OPERATION_IDLE
The valve is currently idle (not moving)
Definition valve.h:75
size_t size_t pos
Definition helpers.h:1038