ESPHome  2024.10.2
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_stop(true);
55  traits.set_supports_position(true);
56  traits.set_supports_toggle(true);
57  traits.set_is_assumed_state(this->assumed_state_);
58  return traits;
59 }
61  if (call.get_stop()) {
62  this->start_direction_(COVER_OPERATION_IDLE);
63  this->publish_state();
64  }
65  if (call.get_toggle().has_value()) {
66  if (this->current_operation != COVER_OPERATION_IDLE) {
67  this->start_direction_(COVER_OPERATION_IDLE);
68  this->publish_state();
69  } else {
70  if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) {
71  this->target_position_ = COVER_OPEN;
72  this->start_direction_(COVER_OPERATION_OPENING);
73  } else {
74  this->target_position_ = COVER_CLOSED;
75  this->start_direction_(COVER_OPERATION_CLOSING);
76  }
77  }
78  }
79  if (call.get_position().has_value()) {
80  auto pos = *call.get_position();
81  if (pos == this->position) {
82  // already at target
83  if (this->manual_control_ && (pos == COVER_OPEN || pos == COVER_CLOSED)) {
84  // for covers with manual control switch, we can't rely on the computed position, so if
85  // the command triggered again, we'll assume it's in the opposite direction anyway.
87  this->position = pos == COVER_CLOSED ? COVER_OPEN : COVER_CLOSED;
88  this->target_position_ = pos;
89  this->start_direction_(op);
90  }
91  // for covers with built in end stop, we should send the command again
92  if (this->has_built_in_endstop_ && (pos == COVER_OPEN || pos == COVER_CLOSED)) {
94  this->target_position_ = pos;
95  this->start_direction_(op);
96  }
97  } else {
99  if (this->manual_control_ && (pos == COVER_OPEN || pos == COVER_CLOSED)) {
100  this->position = pos == COVER_CLOSED ? COVER_OPEN : COVER_CLOSED;
101  }
102  this->target_position_ = pos;
103  this->start_direction_(op);
104  }
105  }
106 }
108  if (this->prev_command_trigger_ != nullptr) {
109  this->prev_command_trigger_->stop_action();
110  this->prev_command_trigger_ = nullptr;
111  }
112 }
114  switch (this->current_operation) {
116  return this->position >= this->target_position_;
118  return this->position <= this->target_position_;
120  default:
121  return true;
122  }
123 }
125  if (dir == this->current_operation && dir != COVER_OPERATION_IDLE)
126  return;
127 
128  this->recompute_position_();
129  Trigger<> *trig;
130  switch (dir) {
132  trig = this->stop_trigger_;
133  break;
135  this->last_operation_ = dir;
136  trig = this->open_trigger_;
137  break;
139  this->last_operation_ = dir;
140  trig = this->close_trigger_;
141  break;
142  default:
143  return;
144  }
145 
146  this->current_operation = dir;
147 
148  const uint32_t now = millis();
149  this->start_dir_time_ = now;
150  this->last_recompute_time_ = now;
151 
152  this->stop_prev_trigger_();
153  trig->trigger();
154  this->prev_command_trigger_ = trig;
155 }
157  if (this->current_operation == COVER_OPERATION_IDLE)
158  return;
159 
160  float dir;
161  float action_dur;
162  switch (this->current_operation) {
164  dir = 1.0f;
165  action_dur = this->open_duration_;
166  break;
168  dir = -1.0f;
169  action_dur = this->close_duration_;
170  break;
171  default:
172  return;
173  }
174 
175  const uint32_t now = millis();
176  this->position += dir * (now - this->last_recompute_time_) / action_dur;
177  this->position = clamp(this->position, 0.0f, 1.0f);
178 
179  this->last_recompute_time_ = now;
180 }
181 
182 } // namespace time_based
183 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
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:92
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
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
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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