ESPHome 2025.5.0
Loading...
Searching...
No Matches
demo_cover.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome {
7namespace demo {
8
9enum class DemoCoverType {
10 TYPE_1,
11 TYPE_2,
12 TYPE_3,
13 TYPE_4,
14};
15
16class DemoCover : public cover::Cover, public Component {
17 public:
19 void setup() override {
20 switch (type_) {
23 break;
25 this->position = 0.7;
26 break;
28 this->position = 0.1;
29 this->tilt = 0.8;
30 break;
33 this->tilt = 1.0;
34 break;
35 }
36 this->publish_state();
37 }
38
39 protected:
40 void control(const cover::CoverCall &call) override {
41 if (call.get_position().has_value()) {
42 float target = *call.get_position();
43 this->current_operation =
45
46 this->set_timeout("move", 2000, [this, target]() {
48 this->position = target;
49 this->publish_state();
50 });
51 }
52 if (call.get_tilt().has_value()) {
53 this->tilt = *call.get_tilt();
54 }
55 if (call.get_stop()) {
56 this->cancel_timeout("move");
57 }
58
59 this->publish_state();
60 }
62 cover::CoverTraits traits{};
63 switch (type_) {
65 traits.set_is_assumed_state(true);
66 break;
68 traits.set_supports_position(true);
69 break;
71 traits.set_supports_position(true);
72 traits.set_supports_tilt(true);
73 break;
75 traits.set_supports_stop(true);
76 traits.set_is_assumed_state(true);
77 traits.set_supports_tilt(true);
78 break;
79 }
80 return traits;
81 }
82
84};
85
86} // namespace demo
87} // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition component.cpp:76
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:72
const optional< float > & get_tilt() const
Definition cover.cpp:98
Base class for all cover devices.
Definition cover.h:111
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:116
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:166
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:124
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:122
cover::CoverTraits get_traits() override
Definition demo_cover.h:61
void control(const cover::CoverCall &call) override
Definition demo_cover.h:40
void setup() override
Definition demo_cover.h:19
void set_type(DemoCoverType type)
Definition demo_cover.h:18
bool has_value() const
Definition optional.h:87
uint8_t type
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:84
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:86
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:82
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7