ESPHome  2022.12.8
feedback_cover.cpp
Go to the documentation of this file.
1 #include "feedback_cover.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace feedback {
7 
8 static const char *const TAG = "feedback.cover";
9 
10 using namespace esphome::cover;
11 
13  auto restore = this->restore_state_();
14 
15  if (restore.has_value()) {
16  restore->apply(this);
17  } else {
18  // if no other information, assume half open
19  this->position = 0.5f;
20  }
21  this->current_operation = COVER_OPERATION_IDLE;
22 
23 #ifdef USE_BINARY_SENSOR
24  // if available, get position from endstop sensors
25  if (this->open_endstop_ != nullptr && this->open_endstop_->state) {
26  this->position = COVER_OPEN;
27  } else if (this->close_endstop_ != nullptr && this->close_endstop_->state) {
28  this->position = COVER_CLOSED;
29  }
30 
31  // if available, get moving state from sensors
32  if (this->open_feedback_ != nullptr && this->open_feedback_->state) {
33  this->current_operation = COVER_OPERATION_OPENING;
34  } else if (this->close_feedback_ != nullptr && this->close_feedback_->state) {
35  this->current_operation = COVER_OPERATION_CLOSING;
36  }
37 #endif
38 
39  this->last_recompute_time_ = this->start_dir_time_ = millis();
40 }
41 
43  auto traits = CoverTraits();
44  traits.set_supports_position(true);
45  traits.set_supports_toggle(true);
46  traits.set_is_assumed_state(this->assumed_state_);
47  return traits;
48 }
49 
51  LOG_COVER("", "Endstop Cover", this);
52  ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
53 #ifdef USE_BINARY_SENSOR
54  LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
55  LOG_BINARY_SENSOR(" ", "Open Feedback", this->open_feedback_);
56  LOG_BINARY_SENSOR(" ", "Open Obstacle", this->open_obstacle_);
57 #endif
58  ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
59 #ifdef USE_BINARY_SENSOR
60  LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
61  LOG_BINARY_SENSOR(" ", "Close Feedback", this->close_feedback_);
62  LOG_BINARY_SENSOR(" ", "Close Obstacle", this->close_obstacle_);
63 #endif
64  if (this->has_built_in_endstop_) {
65  ESP_LOGCONFIG(TAG, " Has builtin endstop: YES");
66  }
67  if (this->infer_endstop_) {
68  ESP_LOGCONFIG(TAG, " Infer endstop from movement: YES");
69  }
70  if (this->max_duration_ < UINT32_MAX) {
71  ESP_LOGCONFIG(TAG, " Max Duration: %.1fs", this->max_duration_ / 1e3f);
72  }
73  if (this->direction_change_waittime_.has_value()) {
74  ESP_LOGCONFIG(TAG, " Direction change wait time: %.1fs", *this->direction_change_waittime_ / 1e3f);
75  }
76  if (this->acceleration_wait_time_) {
77  ESP_LOGCONFIG(TAG, " Acceleration wait time: %.1fs", this->acceleration_wait_time_ / 1e3f);
78  }
79 #ifdef USE_BINARY_SENSOR
80  if (this->obstacle_rollback_ && (this->open_obstacle_ != nullptr || this->close_obstacle_ != nullptr)) {
81  ESP_LOGCONFIG(TAG, " Obstacle rollback: %.1f%%", this->obstacle_rollback_ * 100);
82  }
83 #endif
84 }
85 
86 #ifdef USE_BINARY_SENSOR
87 
89  this->open_feedback_ = open_feedback;
90 
91  // setup callbacks to react to sensor changes
92  open_feedback->add_on_state_callback([this](bool state) {
93  ESP_LOGD(TAG, "'%s' - Open feedback '%s'.", this->name_.c_str(), state ? "STARTED" : "ENDED");
94  this->recompute_position_();
95  if (!state && this->infer_endstop_ && this->current_trigger_operation_ == COVER_OPERATION_OPENING) {
96  this->endstop_reached_(true);
97  }
98  this->set_current_operation_(state ? COVER_OPERATION_OPENING : COVER_OPERATION_IDLE, false);
99  });
100 }
101 
103  this->close_feedback_ = close_feedback;
104 
105  close_feedback->add_on_state_callback([this](bool state) {
106  ESP_LOGD(TAG, "'%s' - Close feedback '%s'.", this->name_.c_str(), state ? "STARTED" : "ENDED");
107  this->recompute_position_();
108  if (!state && this->infer_endstop_ && this->current_trigger_operation_ == COVER_OPERATION_CLOSING) {
109  this->endstop_reached_(false);
110  }
111 
112  this->set_current_operation_(state ? COVER_OPERATION_CLOSING : COVER_OPERATION_IDLE, false);
113  });
114 }
115 
117  this->open_endstop_ = open_endstop;
118  open_endstop->add_on_state_callback([this](bool state) {
119  if (state) {
120  this->endstop_reached_(true);
121  }
122  });
123 }
124 
126  this->close_endstop_ = close_endstop;
127  close_endstop->add_on_state_callback([this](bool state) {
128  if (state) {
129  this->endstop_reached_(false);
130  }
131  });
132 }
133 #endif
134 
135 void FeedbackCover::endstop_reached_(bool open_endstop) {
136  const uint32_t now = millis();
137 
138  this->position = open_endstop ? COVER_OPEN : COVER_CLOSED;
139 
140  // only act if endstop activated while moving in the right direction, in case we are coming back
141  // from a position slightly past the endpoint
142  if (this->current_trigger_operation_ == (open_endstop ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING)) {
143  float dur = (now - this->start_dir_time_) / 1e3f;
144  ESP_LOGD(TAG, "'%s' - %s endstop reached. Took %.1fs.", this->name_.c_str(), open_endstop ? "Open" : "Close", dur);
145 
146  // if there is no external mechanism, stop the cover
147  if (!this->has_built_in_endstop_) {
148  this->start_direction_(COVER_OPERATION_IDLE);
149  } else {
150  this->set_current_operation_(COVER_OPERATION_IDLE, true);
151  }
152  }
153 
154  // always sync position and publish
155  this->publish_state();
156  this->last_publish_time_ = now;
157 }
158 
160  if (is_triggered) {
161  this->current_trigger_operation_ = operation;
162  }
163 
164  // if it is setting the actual operation (not triggered one) or
165  // if we don't have moving sensor, we operate in optimistic mode, assuming actions take place immediately
166  // thus, triggered operation always sets current operation.
167  // otherwise, current operation comes from sensor, and may differ from requested operation
168  // this might be from delays or complex actions, or because the movement was not trigger by the component
169  // but initiated externally
170 
171 #ifdef USE_BINARY_SENSOR
172  if (!is_triggered || (this->open_feedback_ == nullptr || this->close_feedback_ == nullptr))
173 #endif
174  {
175  auto now = millis();
176  this->current_operation = operation;
177  this->start_dir_time_ = this->last_recompute_time_ = now;
178  this->publish_state();
179  this->last_publish_time_ = now;
180  }
181 }
182 
183 #ifdef USE_BINARY_SENSOR
185  this->close_obstacle_ = close_obstacle;
186 
187  close_obstacle->add_on_state_callback([this](bool state) {
188  if (state && (this->current_operation == COVER_OPERATION_CLOSING ||
189  this->current_trigger_operation_ == COVER_OPERATION_CLOSING)) {
190  ESP_LOGD(TAG, "'%s' - Close obstacle detected.", this->name_.c_str());
191  this->start_direction_(COVER_OPERATION_IDLE);
192 
193  if (this->obstacle_rollback_) {
194  this->target_position_ = clamp(this->position + this->obstacle_rollback_, COVER_CLOSED, COVER_OPEN);
195  this->start_direction_(COVER_OPERATION_OPENING);
196  }
197  }
198  });
199 }
200 
202  this->open_obstacle_ = open_obstacle;
203 
204  open_obstacle->add_on_state_callback([this](bool state) {
205  if (state && (this->current_operation == COVER_OPERATION_OPENING ||
206  this->current_trigger_operation_ == COVER_OPERATION_OPENING)) {
207  ESP_LOGD(TAG, "'%s' - Open obstacle detected.", this->name_.c_str());
208  this->start_direction_(COVER_OPERATION_IDLE);
209 
210  if (this->obstacle_rollback_) {
211  this->target_position_ = clamp(this->position - this->obstacle_rollback_, COVER_CLOSED, COVER_OPEN);
212  this->start_direction_(COVER_OPERATION_CLOSING);
213  }
214  }
215  });
216 }
217 #endif
218 
220  if (this->current_operation == COVER_OPERATION_IDLE)
221  return;
222  const uint32_t now = millis();
223 
224  // Recompute position every loop cycle
225  this->recompute_position_();
226 
227  // if we initiated the move, check if we reached position or max time
228  // (stoping from endstop sensor is handled in callback)
229  if (this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
230  if (this->is_at_target_()) {
231  if (this->has_built_in_endstop_ &&
232  (this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED)) {
233  // Don't trigger stop, let the cover stop by itself.
234  this->set_current_operation_(COVER_OPERATION_IDLE, true);
235  } else {
236  this->start_direction_(COVER_OPERATION_IDLE);
237  }
238  } else if (now - this->start_dir_time_ > this->max_duration_) {
239  ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
240  this->start_direction_(COVER_OPERATION_IDLE);
241  }
242  }
243 
244  // update current position at requested interval, regardless of who started the movement
245  // so that we also update UI if there was an external movement
246  // don´t save intermediate positions
247  if (now - this->last_publish_time_ > this->update_interval_) {
248  this->publish_state(false);
249  this->last_publish_time_ = now;
250  }
251 }
252 
254  // stop action logic
255  if (call.get_stop()) {
256  this->start_direction_(COVER_OPERATION_IDLE);
257  } else if (call.get_toggle().has_value()) {
258  // toggle action logic: OPEN - STOP - CLOSE
259  if (this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
260  this->start_direction_(COVER_OPERATION_IDLE);
261  } else {
262  if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) {
263  this->target_position_ = COVER_OPEN;
264  this->start_direction_(COVER_OPERATION_OPENING);
265  } else {
266  this->target_position_ = COVER_CLOSED;
267  this->start_direction_(COVER_OPERATION_CLOSING);
268  }
269  }
270  } else if (call.get_position().has_value()) {
271  // go to position action
272  auto pos = *call.get_position();
273  if (pos == this->position) {
274  // already at target,
275 
276  // for covers with built in end stop, if we don´t have sensors we should send the command again
277  // to make sure the assumed state is not wrong
278  if (this->has_built_in_endstop_ && ((pos == COVER_OPEN
279 #ifdef USE_BINARY_SENSOR
280  && this->open_endstop_ == nullptr
281 #endif
282  && !this->infer_endstop_) ||
283  (pos == COVER_CLOSED
284 #ifdef USE_BINARY_SENSOR
285  && this->close_endstop_ == nullptr
286 #endif
287  && !this->infer_endstop_))) {
288  this->target_position_ = pos;
289  this->start_direction_(pos == COVER_CLOSED ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING);
290  } else if (this->current_operation != COVER_OPERATION_IDLE ||
291  this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
292  // if we are moving, stop
293  this->start_direction_(COVER_OPERATION_IDLE);
294  }
295  } else {
296  this->target_position_ = pos;
297  this->start_direction_(pos < this->position ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING);
298  }
299  }
300 }
301 
303  if (this->direction_change_waittime_.has_value()) {
304  this->cancel_timeout("direction_change");
305  }
306  if (this->prev_command_trigger_ != nullptr) {
307  this->prev_command_trigger_->stop_action();
308  this->prev_command_trigger_ = nullptr;
309  }
310 }
311 
313  // if initiated externally, current operation might be different from
314  // operation that was triggered, thus evaluate position against what was asked
315 
316  switch (this->current_trigger_operation_) {
318  return this->position >= this->target_position_;
320  return this->position <= this->target_position_;
322  return this->current_operation == COVER_OPERATION_IDLE;
323  default:
324  return true;
325  }
326 }
328  Trigger<> *trig;
329 
330 #ifdef USE_BINARY_SENSOR
331  binary_sensor::BinarySensor *obstacle{nullptr};
332 #endif
333 
334  switch (dir) {
336  trig = this->stop_trigger_;
337  break;
339  this->last_operation_ = dir;
340  trig = this->open_trigger_;
341 #ifdef USE_BINARY_SENSOR
342  obstacle = this->open_obstacle_;
343 #endif
344  break;
346  this->last_operation_ = dir;
347  trig = this->close_trigger_;
348 #ifdef USE_BINARY_SENSOR
349  obstacle = this->close_obstacle_;
350 #endif
351  break;
352  default:
353  return;
354  }
355 
356  this->stop_prev_trigger_();
357 
358 #ifdef USE_BINARY_SENSOR
359  // check if there is an obstacle to start the new operation -> abort without any change
360  // the case when an obstacle appears while moving is handled in the callback
361  if (obstacle != nullptr && obstacle->state) {
362  ESP_LOGD(TAG, "'%s' - %s obstacle detected. Action not started.", this->name_.c_str(),
363  dir == COVER_OPERATION_OPENING ? "Open" : "Close");
364  return;
365  }
366 #endif
367 
368  // if we are moving and need to move in the opposite direction
369  // check if we have a wait time
370  if (this->direction_change_waittime_.has_value() && dir != COVER_OPERATION_IDLE &&
371  this->current_operation != COVER_OPERATION_IDLE && dir != this->current_operation) {
372  ESP_LOGD(TAG, "'%s' - Reversing direction.", this->name_.c_str());
373  this->start_direction_(COVER_OPERATION_IDLE);
374 
375  this->set_timeout("direction_change", *this->direction_change_waittime_,
376  [this, dir]() { this->start_direction_(dir); });
377 
378  } else {
379  this->set_current_operation_(dir, true);
380  this->prev_command_trigger_ = trig;
381  ESP_LOGD(TAG, "'%s' - Firing '%s' trigger.", this->name_.c_str(),
382  dir == COVER_OPERATION_OPENING ? "OPEN"
383  : dir == COVER_OPERATION_CLOSING ? "CLOSE"
384  : "STOP");
385  trig->trigger();
386  }
387 }
388 
390  if (this->current_operation == COVER_OPERATION_IDLE)
391  return;
392 
393  const uint32_t now = millis();
394  float dir;
395  float action_dur;
396  float min_pos;
397  float max_pos;
398 
399  // endstop sensors update position from their callbacks, and sets the fully open/close value
400  // If we have endstop, estimation never reaches the fully open/closed state.
401  // but if movement continues past corresponding endstop (inertia), keep the fully open/close state
402 
403  switch (this->current_operation) {
405  dir = 1.0f;
406  action_dur = this->open_duration_;
407  min_pos = COVER_CLOSED;
408  max_pos = (
409 #ifdef USE_BINARY_SENSOR
410  this->open_endstop_ != nullptr ||
411 #endif
412  this->infer_endstop_) &&
413  this->position < COVER_OPEN
414  ? 0.99f
415  : COVER_OPEN;
416  break;
418  dir = -1.0f;
419  action_dur = this->close_duration_;
420  min_pos = (
421 #ifdef USE_BINARY_SENSOR
422  this->close_endstop_ != nullptr ||
423 #endif
424  this->infer_endstop_) &&
425  this->position > COVER_CLOSED
426  ? 0.01f
427  : COVER_CLOSED;
428  max_pos = COVER_OPEN;
429  break;
430  default:
431  return;
432  }
433 
434  // check if we have an acceleration_wait_time, and remove from position computation
435  if (now > (this->start_dir_time_ + this->acceleration_wait_time_)) {
436  this->position +=
437  dir * (now - std::max(this->start_dir_time_ + this->acceleration_wait_time_, this->last_recompute_time_)) /
438  (action_dur - this->acceleration_wait_time_);
439  this->position = clamp(this->position, min_pos, max_pos);
440  }
441  this->last_recompute_time_ = now;
442 }
443 
444 } // namespace feedback
445 } // namespace esphome
void set_close_endstop(binary_sensor::BinarySensor *close_endstop)
void set_current_operation_(cover::CoverOperation operation, bool is_triggered)
void endstop_reached_(bool open_endstop)
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:84
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void set_open_sensor(binary_sensor::BinarySensor *open_feedback)
void control(const cover::CoverCall &call) override
void start_direction_(cover::CoverOperation dir)
const optional< bool > & get_toggle() const
Definition: cover.cpp:99
void set_close_obstacle_sensor(binary_sensor::BinarySensor *close_obstacle)
const float COVER_OPEN
Definition: cover.cpp:9
void set_close_sensor(binary_sensor::BinarySensor *close_feedback)
void set_open_endstop(binary_sensor::BinarySensor *open_endstop)
cover::CoverTraits get_traits() override
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
void set_open_obstacle_sensor(binary_sensor::BinarySensor *open_obstacle)
float position
Definition: cover.h:14
The cover is currently opening.
Definition: cover.h:84
const optional< float > & get_position() const
Definition: cover.cpp:97
bool state
Definition: fan.h:34
bool get_stop() const
Definition: cover.cpp:147