ESPHome 2026.3.3
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
12 switch (this->restore_mode_) {
14 break;
15 case COVER_RESTORE: {
16 auto restore = this->restore_state_();
17 if (restore.has_value())
18 restore->apply(this);
19 break;
20 }
22 auto restore = this->restore_state_();
23 if (restore.has_value()) {
24 restore->to_call(this).perform();
25 }
26 break;
27 }
28 }
29 if (!this->state_f_.has_value() && !this->tilt_f_.has_value())
30 this->disable_loop();
31}
33 bool changed = false;
34
35 auto s = this->state_f_();
36 if (s.has_value()) {
37 auto pos = clamp(*s, 0.0f, 1.0f);
38 if (pos != this->position) {
39 this->position = pos;
40 changed = true;
41 }
42 }
43
44 auto tilt = this->tilt_f_();
45 if (tilt.has_value()) {
46 auto tilt_val = clamp(*tilt, 0.0f, 1.0f);
47 if (tilt_val != this->tilt) {
48 this->tilt = tilt_val;
49 changed = true;
50 }
51 }
52
53 if (changed)
54 this->publish_state();
55}
56void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
57void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
63void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
65 if (call.get_stop()) {
66 this->stop_prev_trigger_();
67 this->stop_trigger_.trigger();
69 this->publish_state();
70 }
71 if (call.get_toggle().has_value()) {
72 this->stop_prev_trigger_();
75 this->publish_state();
76 }
77 auto pos_val = call.get_position();
78 if (pos_val.has_value()) {
79 auto pos = *pos_val;
80 this->stop_prev_trigger_();
81
82 if (pos == COVER_OPEN) {
83 this->open_trigger_.trigger();
85 } else if (pos == COVER_CLOSED) {
86 this->close_trigger_.trigger();
88 } else {
90 }
91
92 if (this->optimistic_) {
93 this->position = pos;
94 }
95 }
96
97 auto tilt_val = call.get_tilt();
98 if (tilt_val.has_value()) {
99 auto tilt = *tilt_val;
101
102 if (this->optimistic_) {
103 this->tilt = tilt;
104 }
105 }
106
107 this->publish_state();
108}
110 auto traits = CoverTraits();
111 traits.set_is_assumed_state(this->assumed_state_);
112 traits.set_supports_stop(this->has_stop_);
113 traits.set_supports_toggle(this->has_toggle_);
114 traits.set_supports_position(this->has_position_);
115 traits.set_supports_tilt(this->has_tilt_);
116 return traits;
117}
120void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
121void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
122void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
123void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
125 if (this->prev_command_trigger_ != nullptr) {
127 this->prev_command_trigger_ = nullptr;
128 }
129}
130
131} // 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:325
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:333
const optional< bool > & get_toggle() const
Definition cover.cpp:94
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:180
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:143
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:123
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
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)
Trigger< float > * get_position_trigger()
void set_optimistic(bool optimistic)
float get_setup_priority() const override
cover::CoverTraits get_traits() override
TemplateCoverRestoreMode restore_mode_
TemplateLambda< float > tilt_f_
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:29
const char *const TAG
Definition spi.cpp:7
size_t size_t pos
Definition helpers.h:929