ESPHome  2022.6.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  position_trigger_(new Trigger<float>()),
16  tilt_trigger_(new Trigger<float>()) {}
18  ESP_LOGCONFIG(TAG, "Setting up template cover '%s'...", this->name_.c_str());
19  switch (this->restore_mode_) {
20  case COVER_NO_RESTORE:
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 }
64 void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
65 void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
66 void TemplateCover::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
71 void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
73  if (call.get_stop()) {
74  this->stop_prev_trigger_();
75  this->stop_trigger_->trigger();
77  this->publish_state();
78  }
79  if (call.get_position().has_value()) {
80  auto pos = *call.get_position();
81  this->stop_prev_trigger_();
82 
83  if (pos == COVER_OPEN) {
84  this->open_trigger_->trigger();
86  } else if (pos == COVER_CLOSED) {
87  this->close_trigger_->trigger();
89  } else {
90  this->position_trigger_->trigger(pos);
91  }
92 
93  if (this->optimistic_) {
94  this->position = pos;
95  }
96  }
97 
98  if (call.get_tilt().has_value()) {
99  auto tilt = *call.get_tilt();
100  this->tilt_trigger_->trigger(tilt);
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_position(this->has_position_);
113  traits.set_supports_tilt(this->has_tilt_);
114  return traits;
115 }
118 void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
119 void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
120 void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
122  if (this->prev_command_trigger_ != nullptr) {
124  this->prev_command_trigger_ = nullptr;
125  }
126 }
127 
128 } // namespace template_
129 } // 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_position(bool has_position)
std::string name_
Definition: entity_base.h:54
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:84
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition: cover.h:125
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 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)
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:123
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
optional< std::function< optional< float >)> > tilt_f_
Trigger< float > * get_tilt_trigger() const
Definition: a4988.cpp:4
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