ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
template_cover.cpp
Go to the documentation of this file.
1#include "template_cover.h"
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6using namespace esphome::cover;
7
8static const char *const TAG = "template.cover";
9
11 : open_trigger_(new Trigger<>()),
12 close_trigger_(new Trigger<>),
13 stop_trigger_(new Trigger<>()),
14 toggle_trigger_(new Trigger<>()),
15 position_trigger_(new Trigger<float>()),
16 tilt_trigger_(new Trigger<float>()) {}
18 switch (this->restore_mode_) {
20 break;
21 case COVER_RESTORE: {
22 auto restore = this->restore_state_();
23 if (restore.has_value())
24 restore->apply(this);
25 break;
26 }
28 auto restore = this->restore_state_();
29 if (restore.has_value()) {
30 restore->to_call(this).perform();
31 }
32 break;
33 }
34 }
35 if (!this->state_f_.has_value() && !this->tilt_f_.has_value())
36 this->disable_loop();
37}
39 bool changed = false;
40
41 auto s = this->state_f_();
42 if (s.has_value()) {
43 auto pos = clamp(*s, 0.0f, 1.0f);
44 if (pos != this->position) {
45 this->position = pos;
46 changed = true;
47 }
48 }
49
50 auto tilt = this->tilt_f_();
51 if (tilt.has_value()) {
52 auto tilt_val = clamp(*tilt, 0.0f, 1.0f);
53 if (tilt_val != this->tilt) {
54 this->tilt = tilt_val;
55 changed = true;
56 }
57 }
58
59 if (changed)
60 this->publish_state();
61}
62void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
63void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
69void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
71 if (call.get_stop()) {
72 this->stop_prev_trigger_();
73 this->stop_trigger_->trigger();
75 this->publish_state();
76 }
77 if (call.get_toggle().has_value()) {
78 this->stop_prev_trigger_();
79 this->toggle_trigger_->trigger();
81 this->publish_state();
82 }
83 if (call.get_position().has_value()) {
84 auto pos = *call.get_position();
85 this->stop_prev_trigger_();
86
87 if (pos == COVER_OPEN) {
88 this->open_trigger_->trigger();
90 } else if (pos == COVER_CLOSED) {
91 this->close_trigger_->trigger();
93 } else {
94 this->position_trigger_->trigger(pos);
95 }
96
97 if (this->optimistic_) {
98 this->position = pos;
99 }
100 }
101
102 if (call.get_tilt().has_value()) {
103 auto tilt = *call.get_tilt();
104 this->tilt_trigger_->trigger(tilt);
105
106 if (this->optimistic_) {
107 this->tilt = tilt;
108 }
109 }
110
111 this->publish_state();
112}
114 auto traits = CoverTraits();
115 traits.set_is_assumed_state(this->assumed_state_);
116 traits.set_supports_stop(this->has_stop_);
117 traits.set_supports_toggle(this->has_toggle_);
118 traits.set_supports_position(this->has_position_);
119 traits.set_supports_tilt(this->has_tilt_);
120 return traits;
121}
124void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
125void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
126void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
127void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
129 if (this->prev_command_trigger_ != nullptr) {
131 this->prev_command_trigger_ = nullptr;
132 }
133}
134
135} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
bool has_value() const
Check if a lambda is set.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:212
const optional< bool > & get_toggle() const
Definition cover.cpp:103
const optional< float > & get_position() const
Definition cover.cpp:101
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:189
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:152
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:125
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:123
bool has_value() const
Definition optional.h:92
Trigger< float > * get_position_trigger() const
Trigger< float > * get_tilt_trigger() const
TemplateLambda< float > state_f_
void control(const cover::CoverCall &call) override
void set_has_position(bool has_position)
void set_assumed_state(bool assumed_state)
void set_optimistic(bool optimistic)
float get_setup_priority() const override
cover::CoverTraits get_traits() override
TemplateCoverRestoreMode restore_mode_
TemplateLambda< float > tilt_f_
const float COVER_CLOSED
Definition cover.cpp:14
const float COVER_OPEN
Definition cover.cpp:13
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
const char *const TAG
Definition spi.cpp:7