ESPHome  2024.10.2
template_cover.cpp
Go to the documentation of this file.
1 #include "template_cover.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace template_ {
6 
7 using namespace esphome::cover;
8 
9 static 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  ESP_LOGCONFIG(TAG, "Setting up template cover '%s'...", this->name_.c_str());
20  switch (this->restore_mode_) {
21  case COVER_NO_RESTORE:
22  break;
23  case COVER_RESTORE: {
24  auto restore = this->restore_state_();
25  if (restore.has_value())
26  restore->apply(this);
27  break;
28  }
30  auto restore = this->restore_state_();
31  if (restore.has_value()) {
32  restore->to_call(this).perform();
33  }
34  break;
35  }
36  }
37 }
39  bool changed = false;
40 
41  if (this->state_f_.has_value()) {
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  if (this->tilt_f_.has_value()) {
52  auto s = (*this->tilt_f_)();
53  if (s.has_value()) {
54  auto tilt = clamp(*s, 0.0f, 1.0f);
55  if (tilt != this->tilt) {
56  this->tilt = tilt;
57  changed = true;
58  }
59  }
60  }
61 
62  if (changed)
63  this->publish_state();
64 }
65 void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
66 void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
67 void TemplateCover::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
73 void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
75  if (call.get_stop()) {
76  this->stop_prev_trigger_();
77  this->stop_trigger_->trigger();
79  this->publish_state();
80  }
81  if (call.get_toggle().has_value()) {
82  this->stop_prev_trigger_();
83  this->toggle_trigger_->trigger();
85  this->publish_state();
86  }
87  if (call.get_position().has_value()) {
88  auto pos = *call.get_position();
89  this->stop_prev_trigger_();
90 
91  if (pos == COVER_OPEN) {
92  this->open_trigger_->trigger();
94  } else if (pos == COVER_CLOSED) {
95  this->close_trigger_->trigger();
97  } else {
98  this->position_trigger_->trigger(pos);
99  }
100 
101  if (this->optimistic_) {
102  this->position = pos;
103  }
104  }
105 
106  if (call.get_tilt().has_value()) {
107  auto tilt = *call.get_tilt();
108  this->tilt_trigger_->trigger(tilt);
109 
110  if (this->optimistic_) {
111  this->tilt = tilt;
112  }
113  }
114 
115  this->publish_state();
116 }
118  auto traits = CoverTraits();
119  traits.set_is_assumed_state(this->assumed_state_);
120  traits.set_supports_stop(this->has_stop_);
121  traits.set_supports_toggle(this->has_toggle_);
122  traits.set_supports_position(this->has_position_);
123  traits.set_supports_tilt(this->has_tilt_);
124  return traits;
125 }
128 void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
129 void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
130 void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
131 void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
132 void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
134  if (this->prev_command_trigger_ != nullptr) {
136  this->prev_command_trigger_ = nullptr;
137  }
138 }
139 
140 } // namespace template_
141 } // namespace esphome
float get_setup_priority() const override
void publish_state(bool save=true)
Publish the current state of the cover.
Definition: cover.cpp:166
void set_has_toggle(bool has_toggle)
void set_has_position(bool has_position)
Trigger< float > * position_trigger_
cover::CoverTraits get_traits() override
const float COVER_CLOSED
Definition: cover.cpp:10
bool has_value() const
Definition: optional.h:87
optional< CoverRestoreState > restore_state_()
Definition: cover.cpp:200
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition: cover.h:124
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void set_tilt_lambda(std::function< optional< float >()> &&tilt_f)
void set_optimistic(bool optimistic)
void control(const cover::CoverCall &call) override
const char *const TAG
Definition: spi.cpp:8
const optional< bool > & get_toggle() const
Definition: cover.cpp:99
const float COVER_OPEN
Definition: cover.cpp:9
void set_state_lambda(std::function< optional< float >()> &&f)
TemplateCoverRestoreMode restore_mode_
Trigger< float > * get_position_trigger() const
void set_assumed_state(bool assumed_state)
constexpr const char * c_str() const
Definition: string_ref.h:68
optional< std::function< optional< float >)> > state_f_
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:122
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
optional< std::function< optional< float >)> > tilt_f_
Trigger< float > * get_tilt_trigger() const
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const optional< float > & get_tilt() const
Definition: cover.cpp:98
void stop_action()
Stop any action connected to this trigger.
Definition: automation.h:103
const optional< float > & get_position() const
Definition: cover.cpp:97
bool get_stop() const
Definition: cover.cpp:147