ESPHome  2024.11.0
endstop_cover.cpp
Go to the documentation of this file.
1 #include "endstop_cover.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace endstop {
7 
8 static const char *const TAG = "endstop.cover";
9 
10 using namespace esphome::cover;
11 
13  auto traits = CoverTraits();
14  traits.set_supports_stop(true);
15  traits.set_supports_position(true);
16  traits.set_supports_toggle(true);
17  traits.set_is_assumed_state(false);
18  return traits;
19 }
20 void EndstopCover::control(const CoverCall &call) {
21  if (call.get_stop()) {
22  this->start_direction_(COVER_OPERATION_IDLE);
23  this->publish_state();
24  }
25  if (call.get_toggle().has_value()) {
26  if (this->current_operation != COVER_OPERATION_IDLE) {
27  this->start_direction_(COVER_OPERATION_IDLE);
28  this->publish_state();
29  } else {
30  if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) {
31  this->target_position_ = COVER_OPEN;
32  this->start_direction_(COVER_OPERATION_OPENING);
33  } else {
34  this->target_position_ = COVER_CLOSED;
35  this->start_direction_(COVER_OPERATION_CLOSING);
36  }
37  }
38  }
39  if (call.get_position().has_value()) {
40  auto pos = *call.get_position();
41  if (pos == this->position) {
42  // already at target
43  } else {
45  this->target_position_ = pos;
46  this->start_direction_(op);
47  }
48  }
49 }
51  auto restore = this->restore_state_();
52  if (restore.has_value()) {
53  restore->apply(this);
54  }
55 
56  if (this->is_open_()) {
57  this->position = COVER_OPEN;
58  } else if (this->is_closed_()) {
59  this->position = COVER_CLOSED;
60  } else if (!restore.has_value()) {
61  this->position = 0.5f;
62  }
63 }
65  if (this->current_operation == COVER_OPERATION_IDLE)
66  return;
67 
68  const uint32_t now = millis();
69 
70  if (this->current_operation == COVER_OPERATION_OPENING && this->is_open_()) {
71  float dur = (now - this->start_dir_time_) / 1e3f;
72  ESP_LOGD(TAG, "'%s' - Open endstop reached. Took %.1fs.", this->name_.c_str(), dur);
73 
74  this->start_direction_(COVER_OPERATION_IDLE);
75  this->position = COVER_OPEN;
76  this->publish_state();
77  } else if (this->current_operation == COVER_OPERATION_CLOSING && this->is_closed_()) {
78  float dur = (now - this->start_dir_time_) / 1e3f;
79  ESP_LOGD(TAG, "'%s' - Close endstop reached. Took %.1fs.", this->name_.c_str(), dur);
80 
81  this->start_direction_(COVER_OPERATION_IDLE);
82  this->position = COVER_CLOSED;
83  this->publish_state();
84  } else if (now - this->start_dir_time_ > this->max_duration_) {
85  ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
86  this->start_direction_(COVER_OPERATION_IDLE);
87  this->publish_state();
88  }
89 
90  // Recompute position every loop cycle
91  this->recompute_position_();
92 
93  if (this->current_operation != COVER_OPERATION_IDLE && this->is_at_target_()) {
94  this->start_direction_(COVER_OPERATION_IDLE);
95  this->publish_state();
96  }
97 
98  // Send current position every second
99  if (this->current_operation != COVER_OPERATION_IDLE && now - this->last_publish_time_ > 1000) {
100  this->publish_state(false);
101  this->last_publish_time_ = now;
102  }
103 }
105  LOG_COVER("", "Endstop Cover", this);
106  LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
107  ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
108  LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
109  ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
110 }
113  if (this->prev_command_trigger_ != nullptr) {
114  this->prev_command_trigger_->stop_action();
115  this->prev_command_trigger_ = nullptr;
116  }
117 }
119  switch (this->current_operation) {
121  if (this->target_position_ == COVER_OPEN)
122  return this->is_open_();
123  return this->position >= this->target_position_;
125  if (this->target_position_ == COVER_CLOSED)
126  return this->is_closed_();
127  return this->position <= this->target_position_;
129  default:
130  return true;
131  }
132 }
134  if (dir == this->current_operation)
135  return;
136 
137  this->recompute_position_();
138  Trigger<> *trig;
139  switch (dir) {
141  trig = this->stop_trigger_;
142  break;
144  this->last_operation_ = dir;
145  trig = this->open_trigger_;
146  break;
148  this->last_operation_ = dir;
149  trig = this->close_trigger_;
150  break;
151  default:
152  return;
153  }
154 
155  this->current_operation = dir;
156 
157  this->stop_prev_trigger_();
158  trig->trigger();
159  this->prev_command_trigger_ = trig;
160 
161  const uint32_t now = millis();
162  this->start_dir_time_ = now;
163  this->last_recompute_time_ = now;
164 }
166  if (this->current_operation == COVER_OPERATION_IDLE)
167  return;
168 
169  float dir;
170  float action_dur;
171  switch (this->current_operation) {
173  dir = 1.0f;
174  action_dur = this->open_duration_;
175  break;
177  dir = -1.0f;
178  action_dur = this->close_duration_;
179  break;
180  default:
181  return;
182  }
183 
184  const uint32_t now = millis();
185  this->position += dir * (now - this->last_recompute_time_) / action_dur;
186  this->position = clamp(this->position, 0.0f, 1.0f);
187 
188  this->last_recompute_time_ = now;
189 }
190 
191 } // namespace endstop
192 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
cover::CoverTraits get_traits() override
CoverOperation
Enum encoding the current operation of a cover.
Definition: cover.h:80
The cover is currently closing.
Definition: cover.h:86
const float COVER_CLOSED
Definition: cover.cpp:10
bool has_value() const
Definition: optional.h:87
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:93
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
const optional< bool > & get_toggle() const
Definition: cover.cpp:99
const float COVER_OPEN
Definition: cover.cpp:9
void start_direction_(cover::CoverOperation dir)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void control(const cover::CoverCall &call) override
float position
Definition: cover.h:14
The cover is currently opening.
Definition: cover.h:84
float get_setup_priority() const override
const optional< float > & get_position() const
Definition: cover.cpp:97
bool get_stop() const
Definition: cover.cpp:147