ESPHome  2023.3.1
time_based_cover.cpp
Go to the documentation of this file.
1 #include "time_based_cover.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace time_based {
7 
8 static const char *const TAG = "time_based.cover";
9 
10 using namespace esphome::cover;
11 
13  LOG_COVER("", "Time Based Cover", this);
14  ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
15  ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
16 }
18  auto restore = this->restore_state_();
19  if (restore.has_value()) {
20  restore->apply(this);
21  } else {
22  this->position = 0.5f;
23  }
24 }
26  if (this->current_operation == COVER_OPERATION_IDLE)
27  return;
28 
29  const uint32_t now = millis();
30 
31  // Recompute position every loop cycle
32  this->recompute_position_();
33 
34  if (this->is_at_target_()) {
35  if (this->has_built_in_endstop_ &&
36  (this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED)) {
37  // Don't trigger stop, let the cover stop by itself.
38  this->current_operation = COVER_OPERATION_IDLE;
39  } else {
40  this->start_direction_(COVER_OPERATION_IDLE);
41  }
42  this->publish_state();
43  }
44 
45  // Send current position every second
46  if (now - this->last_publish_time_ > 1000) {
47  this->publish_state(false);
48  this->last_publish_time_ = now;
49  }
50 }
53  auto traits = CoverTraits();
54  traits.set_supports_position(true);
55  traits.set_supports_toggle(true);
56  traits.set_is_assumed_state(this->assumed_state_);
57  return traits;
58 }
60  if (call.get_stop()) {
61  this->start_direction_(COVER_OPERATION_IDLE);
62  this->publish_state();
63  }
64  if (call.get_toggle().has_value()) {
65  if (this->current_operation != COVER_OPERATION_IDLE) {
66  this->start_direction_(COVER_OPERATION_IDLE);
67  this->publish_state();
68  } else {
69  if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) {
70  this->target_position_ = COVER_OPEN;
71  this->start_direction_(COVER_OPERATION_OPENING);
72  } else {
73  this->target_position_ = COVER_CLOSED;
74  this->start_direction_(COVER_OPERATION_CLOSING);
75  }
76  }
77  }
78  if (call.get_position().has_value()) {
79  auto pos = *call.get_position();
80  if (pos == this->position) {
81  // already at target
82  // for covers with built in end stop, we should send the command again
83  if (this->has_built_in_endstop_ && (pos == COVER_OPEN || pos == COVER_CLOSED)) {
85  this->target_position_ = pos;
86  this->start_direction_(op);
87  }
88  } else {
90  this->target_position_ = pos;
91  this->start_direction_(op);
92  }
93  }
94 }
96  if (this->prev_command_trigger_ != nullptr) {
97  this->prev_command_trigger_->stop_action();
98  this->prev_command_trigger_ = nullptr;
99  }
100 }
102  switch (this->current_operation) {
104  return this->position >= this->target_position_;
106  return this->position <= this->target_position_;
108  default:
109  return true;
110  }
111 }
113  if (dir == this->current_operation && dir != COVER_OPERATION_IDLE)
114  return;
115 
116  this->recompute_position_();
117  Trigger<> *trig;
118  switch (dir) {
120  trig = this->stop_trigger_;
121  break;
123  this->last_operation_ = dir;
124  trig = this->open_trigger_;
125  break;
127  this->last_operation_ = dir;
128  trig = this->close_trigger_;
129  break;
130  default:
131  return;
132  }
133 
134  this->current_operation = dir;
135 
136  const uint32_t now = millis();
137  this->start_dir_time_ = now;
138  this->last_recompute_time_ = now;
139 
140  this->stop_prev_trigger_();
141  trig->trigger();
142  this->prev_command_trigger_ = trig;
143 }
145  if (this->current_operation == COVER_OPERATION_IDLE)
146  return;
147 
148  float dir;
149  float action_dur;
150  switch (this->current_operation) {
152  dir = 1.0f;
153  action_dur = this->open_duration_;
154  break;
156  dir = -1.0f;
157  action_dur = this->close_duration_;
158  break;
159  default:
160  return;
161  }
162 
163  const uint32_t now = millis();
164  this->position += dir * (now - this->last_recompute_time_) / action_dur;
165  this->position = clamp(this->position, 0.0f, 1.0f);
166 
167  this->last_recompute_time_ = now;
168 }
169 
170 } // namespace time_based
171 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
void control(const cover::CoverCall &call) 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
cover::CoverTraits get_traits() override
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:89
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
float get_setup_priority() const override
void start_direction_(cover::CoverOperation dir)
const optional< bool > & get_toggle() const
Definition: cover.cpp:99
const float COVER_OPEN
Definition: cover.cpp:9
Definition: a4988.cpp:4
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 get_stop() const
Definition: cover.cpp:147