ESPHome 2025.9.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}
38 bool changed = false;
39
40 if (this->state_f_.has_value()) {
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 if (this->tilt_f_.has_value()) {
51 auto s = (*this->tilt_f_)();
52 if (s.has_value()) {
53 auto tilt = clamp(*s, 0.0f, 1.0f);
54 if (tilt != this->tilt) {
55 this->tilt = tilt;
56 changed = true;
57 }
58 }
59 }
60
61 if (changed)
62 this->publish_state();
63}
64void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
65void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
66void TemplateCover::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
72void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
74 if (call.get_stop()) {
75 this->stop_prev_trigger_();
76 this->stop_trigger_->trigger();
78 this->publish_state();
79 }
80 if (call.get_toggle().has_value()) {
81 this->stop_prev_trigger_();
82 this->toggle_trigger_->trigger();
84 this->publish_state();
85 }
86 if (call.get_position().has_value()) {
87 auto pos = *call.get_position();
88 this->stop_prev_trigger_();
89
90 if (pos == COVER_OPEN) {
91 this->open_trigger_->trigger();
93 } else if (pos == COVER_CLOSED) {
94 this->close_trigger_->trigger();
96 } else {
97 this->position_trigger_->trigger(pos);
98 }
99
100 if (this->optimistic_) {
101 this->position = pos;
102 }
103 }
104
105 if (call.get_tilt().has_value()) {
106 auto tilt = *call.get_tilt();
107 this->tilt_trigger_->trigger(tilt);
108
109 if (this->optimistic_) {
110 this->tilt = tilt;
111 }
112 }
113
114 this->publish_state();
115}
117 auto traits = CoverTraits();
118 traits.set_is_assumed_state(this->assumed_state_);
119 traits.set_supports_stop(this->has_stop_);
120 traits.set_supports_toggle(this->has_toggle_);
121 traits.set_supports_position(this->has_position_);
122 traits.set_supports_tilt(this->has_tilt_);
123 return traits;
124}
127void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
128void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
129void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
130void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
131void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
133 if (this->prev_command_trigger_ != nullptr) {
135 this->prev_command_trigger_ = nullptr;
136 }
137}
138
139} // namespace template_
140} // namespace esphome
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:153
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
const optional< bool > & get_toggle() const
Definition cover.cpp:99
const optional< float > & get_position() const
Definition cover.cpp:97
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:196
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:162
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:124
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:122
bool has_value() const
Definition optional.h:92
Trigger< float > * get_position_trigger() const
void set_tilt_lambda(std::function< optional< float >()> &&tilt_f)
Trigger< float > * get_tilt_trigger() const
optional< std::function< optional< float >()> > tilt_f_
void control(const cover::CoverCall &call) override
void set_has_position(bool has_position)
void set_assumed_state(bool assumed_state)
void set_state_lambda(std::function< optional< float >()> &&f)
void set_optimistic(bool optimistic)
optional< std::function< optional< float >()> > state_f_
float get_setup_priority() const override
cover::CoverTraits get_traits() override
TemplateCoverRestoreMode restore_mode_
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7