ESPHome  2022.1.2
demo_cover.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace esphome {
7 namespace demo {
8 
9 enum class DemoCoverType {
10  TYPE_1,
11  TYPE_2,
12  TYPE_3,
13  TYPE_4,
14 };
15 
16 class DemoCover : public cover::Cover, public Component {
17  public:
18  void set_type(DemoCoverType type) { type_ = type; }
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]() {
47  this->current_operation = cover::COVER_OPERATION_IDLE;
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_is_assumed_state(true);
76  traits.set_supports_tilt(true);
77  break;
78  }
79  return traits;
80  }
81 
83 };
84 
85 } // namespace demo
86 } // namespace esphome
Base class for all cover devices.
Definition: cover.h:111
The cover is currently closing.
Definition: cover.h:86
const float COVER_CLOSED
Definition: cover.cpp:10
float tilt
Definition: cover.h:15
bool has_value() const
Definition: optional.h:87
void set_type(DemoCoverType type)
Definition: demo_cover.h:18
void setup() override
Definition: demo_cover.h:19
const float COVER_OPEN
Definition: cover.cpp:9
The cover is currently idle (not moving)
Definition: cover.h:82
uint8_t type
void control(const cover::CoverCall &call) override
Definition: demo_cover.h:40
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
float position
Definition: cover.h:14
DemoCoverType type_
Definition: demo_cover.h:82
cover::CoverTraits get_traits() override
Definition: demo_cover.h:61
The cover is currently opening.
Definition: cover.h:84
const optional< float > & get_tilt() const
Definition: cover.cpp:100
const optional< float > & get_position() const
Definition: cover.cpp:99
bool get_stop() const
Definition: cover.cpp:149