ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
demo_cover.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome::demo {
7
8enum class DemoCoverType {
9 TYPE_1,
10 TYPE_2,
11 TYPE_3,
12 TYPE_4,
13};
14
15class DemoCover final : public cover::Cover, public Component {
16 public:
18 void setup() override {
19 switch (type_) {
21 this->position = cover::COVER_OPEN;
22 break;
24 this->position = 0.7;
25 break;
27 this->position = 0.1;
28 this->tilt = 0.8;
29 break;
31 this->position = cover::COVER_CLOSED;
32 this->tilt = 1.0;
33 break;
34 }
35 this->publish_state();
36 }
37
38 protected:
39 static constexpr uint32_t MOVE_TIMEOUT_ID = 0;
40
41 void control(const cover::CoverCall &call) override {
42 auto pos = call.get_position();
43 if (pos.has_value()) {
44 float target = *pos;
45 this->current_operation =
47
48 this->set_timeout(MOVE_TIMEOUT_ID, 2000, [this, target]() {
50 this->position = target;
51 this->publish_state();
52 });
53 }
54 auto tilt = call.get_tilt();
55 if (tilt.has_value()) {
56 this->tilt = *tilt;
57 }
58 if (call.get_stop()) {
59 this->cancel_timeout(MOVE_TIMEOUT_ID);
60 }
61
62 this->publish_state();
63 }
65 cover::CoverTraits traits{};
66 switch (type_) {
68 traits.set_is_assumed_state(true);
69 break;
71 traits.set_supports_position(true);
72 break;
74 traits.set_supports_position(true);
75 traits.set_supports_tilt(true);
76 break;
78 traits.set_supports_stop(true);
79 traits.set_is_assumed_state(true);
80 traits.set_supports_tilt(true);
81 break;
82 }
83 return traits;
84 }
85
87};
88
89} // namespace esphome::demo
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
const optional< float > & get_tilt() const
Definition cover.cpp:100
bool get_stop() const
Definition cover.h:56
Base class for all cover devices.
Definition cover.h:115
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:120
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:158
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:128
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:126
cover::CoverTraits get_traits() override
Definition demo_cover.h:64
void control(const cover::CoverCall &call) override
Definition demo_cover.h:41
static constexpr uint32_t MOVE_TIMEOUT_ID
Definition demo_cover.h:39
void setup() override
Definition demo_cover.h:18
void set_type(DemoCoverType type)
Definition demo_cover.h:17
uint8_t type
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:88
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:90
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:86
size_t size_t pos
Definition helpers.h:1255
static void uint32_t