ESPHome 2026.2.1
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 if (call.get_position().has_value()) {
78 auto pos = *call.get_position();
79 this->stop_prev_trigger_();
80
81 if (pos == COVER_OPEN) {
82 this->open_trigger_.trigger();
84 } else if (pos == COVER_CLOSED) {
85 this->close_trigger_.trigger();
87 } else {
89 }
90
91 if (this->optimistic_) {
92 this->position = pos;
93 }
94 }
95
96 if (call.get_tilt().has_value()) {
97 auto tilt = *call.get_tilt();
99
100 if (this->optimistic_) {
101 this->tilt = tilt;
102 }
103 }
104
105 this->publish_state();
106}
108 auto traits = CoverTraits();
109 traits.set_is_assumed_state(this->assumed_state_);
110 traits.set_supports_stop(this->has_stop_);
111 traits.set_supports_toggle(this->has_toggle_);
112 traits.set_supports_position(this->has_position_);
113 traits.set_supports_tilt(this->has_tilt_);
114 return traits;
115}
118void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
119void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
120void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
121void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
123 if (this->prev_command_trigger_ != nullptr) {
125 this->prev_command_trigger_ = nullptr;
126 }
127}
128
129} // 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:279
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:287
const optional< float > & get_tilt() const
Definition cover.cpp:93
const optional< bool > & get_toggle() const
Definition cover.cpp:94
const optional< float > & get_position() const
Definition cover.cpp:92
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
bool has_value() const
Definition optional.h:92
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_
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:83
const char *const TAG
Definition spi.cpp:7
size_t size_t pos
Definition helpers.h:729