ESPHome  2024.5.3
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  this->target_position_ = pos;
100  this->start_direction_(op);
101  }
102  }
103 }
105  if (this->prev_command_trigger_ != nullptr) {
106  this->prev_command_trigger_->stop_action();
107  this->prev_command_trigger_ = nullptr;
108  }
109 }
111  switch (this->current_operation) {
113  return this->position >= this->target_position_;
115  return this->position <= this->target_position_;
117  default:
118  return true;
119  }
120 }
122  if (dir == this->current_operation && dir != COVER_OPERATION_IDLE)
123  return;
124 
125  this->recompute_position_();
126  Trigger<> *trig;
127  switch (dir) {
129  trig = this->stop_trigger_;
130  break;
132  this->last_operation_ = dir;
133  trig = this->open_trigger_;
134  break;
136  this->last_operation_ = dir;
137  trig = this->close_trigger_;
138  break;
139  default:
140  return;
141  }
142 
143  this->current_operation = dir;
144 
145  const uint32_t now = millis();
146  this->start_dir_time_ = now;
147  this->last_recompute_time_ = now;
148 
149  this->stop_prev_trigger_();
150  trig->trigger();
151  this->prev_command_trigger_ = trig;
152 }
154  if (this->current_operation == COVER_OPERATION_IDLE)
155  return;
156 
157  float dir;
158  float action_dur;
159  switch (this->current_operation) {
161  dir = 1.0f;
162  action_dur = this->open_duration_;
163  break;
165  dir = -1.0f;
166  action_dur = this->close_duration_;
167  break;
168  default:
169  return;
170  }
171 
172  const uint32_t now = millis();
173  this->position += dir * (now - this->last_recompute_time_) / action_dur;
174  this->position = clamp(this->position, 0.0f, 1.0f);
175 
176  this->last_recompute_time_ = now;
177 }
178 
179 } // namespace time_based
180 } // 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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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