ESPHome 2025.12.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 {
5namespace template_ {
6
7using namespace esphome::cover;
8
9static const char *const TAG = "template.cover";
10
12 : open_trigger_(new Trigger<>()),
13 close_trigger_(new Trigger<>),
14 stop_trigger_(new Trigger<>()),
15 toggle_trigger_(new Trigger<>()),
16 position_trigger_(new Trigger<float>()),
17 tilt_trigger_(new Trigger<float>()) {}
19 switch (this->restore_mode_) {
21 break;
22 case COVER_RESTORE: {
23 auto restore = this->restore_state_();
24 if (restore.has_value())
25 restore->apply(this);
26 break;
27 }
29 auto restore = this->restore_state_();
30 if (restore.has_value()) {
31 restore->to_call(this).perform();
32 }
33 break;
34 }
35 }
36 if (!this->state_f_.has_value() && !this->tilt_f_.has_value())
37 this->disable_loop();
38}
40 bool changed = false;
41
42 auto s = this->state_f_();
43 if (s.has_value()) {
44 auto pos = clamp(*s, 0.0f, 1.0f);
45 if (pos != this->position) {
46 this->position = pos;
47 changed = true;
48 }
49 }
50
51 auto tilt = this->tilt_f_();
52 if (tilt.has_value()) {
53 auto tilt_val = clamp(*tilt, 0.0f, 1.0f);
54 if (tilt_val != this->tilt) {
55 this->tilt = tilt_val;
56 changed = true;
57 }
58 }
59
60 if (changed)
61 this->publish_state();
62}
63void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
64void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
70void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
72 if (call.get_stop()) {
73 this->stop_prev_trigger_();
74 this->stop_trigger_->trigger();
76 this->publish_state();
77 }
78 if (call.get_toggle().has_value()) {
79 this->stop_prev_trigger_();
80 this->toggle_trigger_->trigger();
82 this->publish_state();
83 }
84 if (call.get_position().has_value()) {
85 auto pos = *call.get_position();
86 this->stop_prev_trigger_();
87
88 if (pos == COVER_OPEN) {
89 this->open_trigger_->trigger();
91 } else if (pos == COVER_CLOSED) {
92 this->close_trigger_->trigger();
94 } else {
95 this->position_trigger_->trigger(pos);
96 }
97
98 if (this->optimistic_) {
99 this->position = pos;
100 }
101 }
102
103 if (call.get_tilt().has_value()) {
104 auto tilt = *call.get_tilt();
105 this->tilt_trigger_->trigger(tilt);
106
107 if (this->optimistic_) {
108 this->tilt = tilt;
109 }
110 }
111
112 this->publish_state();
113}
115 auto traits = CoverTraits();
116 traits.set_is_assumed_state(this->assumed_state_);
117 traits.set_supports_stop(this->has_stop_);
118 traits.set_supports_toggle(this->has_toggle_);
119 traits.set_supports_position(this->has_position_);
120 traits.set_supports_tilt(this->has_tilt_);
121 return traits;
122}
125void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
126void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
127void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
128void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
130 if (this->prev_command_trigger_ != nullptr) {
132 this->prev_command_trigger_ = nullptr;
133 }
134}
135
136} // namespace template_
137} // namespace esphome
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:169
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:177
const optional< bool > & get_toggle() const
Definition cover.cpp:104
const optional< float > & get_position() const
Definition cover.cpp:102
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:190
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:153
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:15
const float COVER_OPEN
Definition cover.cpp:14
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:58
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7