ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
current_based_cover.cpp
Go to the documentation of this file.
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
5#include <cfloat>
6
7namespace esphome {
8namespace current_based {
9
10static const char *const TAG = "current_based.cover";
11
12using namespace esphome::cover;
13
15 auto traits = CoverTraits();
16 traits.set_supports_stop(true);
17 traits.set_supports_position(true);
18 traits.set_supports_toggle(true);
19 traits.set_is_assumed_state(false);
20 return traits;
21}
23 if (call.get_stop()) {
24 this->direction_idle_();
25 }
26 if (call.get_toggle().has_value()) {
29 this->publish_state();
30 } else {
31 if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) {
32 this->target_position_ = COVER_OPEN;
34 } else {
35 this->target_position_ = COVER_CLOSED;
37 }
38 }
39 }
40 if (call.get_position().has_value()) {
41 auto pos = *call.get_position();
42 if (fabsf(this->position - pos) < 0.01) {
43 // already at target
44 } else {
46 this->target_position_ = pos;
47 this->start_direction_(op);
48 }
49 }
50}
52 auto restore = this->restore_state_();
53 if (restore.has_value()) {
54 restore->apply(this);
55 } else {
56 this->position = 0.5f;
57 }
58}
59
62 return;
63
64 const uint32_t now = App.get_loop_component_start_time();
65
67 if (this->malfunction_detection_ && this->is_closing_()) { // Malfunction
68 this->direction_idle_();
70 ESP_LOGI(TAG, "'%s' - Malfunction detected during opening. Current flow detected in close circuit",
71 this->name_.c_str());
72 } else if (this->is_opening_blocked_()) { // Blocked
73 ESP_LOGD(TAG, "'%s' - Obstacle detected during opening.", this->name_.c_str());
74 this->direction_idle_();
75 if (this->obstacle_rollback_ != 0) {
76 this->set_timeout("rollback", 300, [this]() {
77 ESP_LOGD(TAG, "'%s' - Rollback.", this->name_.c_str());
78 this->target_position_ = clamp(this->position - this->obstacle_rollback_, 0.0F, 1.0F);
80 });
81 }
82 } else if (this->is_initial_delay_finished_() && !this->is_opening_()) { // End reached
83 auto dur = (now - this->start_dir_time_) / 1e3f;
84 ESP_LOGD(TAG, "'%s' - Open position reached. Took %.1fs.", this->name_.c_str(), dur);
85 this->direction_idle_(COVER_OPEN);
86 }
87 } else if (this->current_operation == COVER_OPERATION_CLOSING) {
88 if (this->malfunction_detection_ && this->is_opening_()) { // Malfunction
89 this->direction_idle_();
91 ESP_LOGI(TAG, "'%s' - Malfunction detected during closing. Current flow detected in open circuit",
92 this->name_.c_str());
93 } else if (this->is_closing_blocked_()) { // Blocked
94 ESP_LOGD(TAG, "'%s' - Obstacle detected during closing.", this->name_.c_str());
95 this->direction_idle_();
96 if (this->obstacle_rollback_ != 0) {
97 this->set_timeout("rollback", 300, [this]() {
98 ESP_LOGD(TAG, "'%s' - Rollback.", this->name_.c_str());
99 this->target_position_ = clamp(this->position + this->obstacle_rollback_, 0.0F, 1.0F);
101 });
102 }
103 } else if (this->is_initial_delay_finished_() && !this->is_closing_()) { // End reached
104 auto dur = (now - this->start_dir_time_) / 1e3f;
105 ESP_LOGD(TAG, "'%s' - Close position reached. Took %.1fs.", this->name_.c_str(), dur);
106 this->direction_idle_(COVER_CLOSED);
107 }
108 }
109 if (now - this->start_dir_time_ > this->max_duration_) {
110 ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
111 this->direction_idle_();
112 }
113
114 // Recompute position every loop cycle
115 this->recompute_position_();
116
117 if (this->current_operation != COVER_OPERATION_IDLE && this->is_at_target_()) {
118 this->direction_idle_();
119 }
120
121 // Send current position every second
122 if (this->current_operation != COVER_OPERATION_IDLE && now - this->last_publish_time_ > 1000) {
123 this->publish_state(false);
124 this->last_publish_time_ = now;
125 }
126}
127
128void CurrentBasedCover::direction_idle_(float new_position) {
130 if (new_position != FLT_MAX) {
131 this->position = new_position;
132 }
133 this->publish_state();
134}
135
137 LOG_COVER("", "Endstop Cover", this);
138 LOG_SENSOR(" ", "Open Sensor", this->open_sensor_);
139 ESP_LOGCONFIG(TAG, " Open moving current threshold: %.11fA", this->open_moving_current_threshold_);
140 if (this->open_obstacle_current_threshold_ != FLT_MAX) {
141 ESP_LOGCONFIG(TAG, " Open obstacle current threshold: %.11fA", this->open_obstacle_current_threshold_);
142 }
143 ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
144 LOG_SENSOR(" ", "Close Sensor", this->close_sensor_);
145 ESP_LOGCONFIG(TAG, " Close moving current threshold: %.11fA", this->close_moving_current_threshold_);
146 if (this->close_obstacle_current_threshold_ != FLT_MAX) {
147 ESP_LOGCONFIG(TAG, " Close obstacle current threshold: %.11fA", this->close_obstacle_current_threshold_);
148 }
149 ESP_LOGCONFIG(TAG,
150 " Close Duration: %.1fs\n"
151 " Obstacle Rollback: %.1f%%",
152 this->close_duration_ / 1e3f, this->obstacle_rollback_ * 100);
153 if (this->max_duration_ != UINT32_MAX) {
154 ESP_LOGCONFIG(TAG, " Maximum duration: %.1fs", this->max_duration_ / 1e3f);
155 }
156 ESP_LOGCONFIG(TAG,
157 " Start sensing delay: %.1fs\n"
158 " Malfunction detection: %s",
159 this->start_sensing_delay_ / 1e3f, YESNO(this->malfunction_detection_));
160}
161
163 if (this->prev_command_trigger_ != nullptr) {
165 this->prev_command_trigger_ = nullptr;
166 }
167}
168
172
174 if (this->open_obstacle_current_threshold_ == FLT_MAX) {
175 return false;
176 }
178}
179
183
185 if (this->close_obstacle_current_threshold_ == FLT_MAX) {
186 return false;
187 }
189}
193
195 switch (this->current_operation) {
197 if (this->target_position_ == COVER_OPEN) {
198 if (!this->is_initial_delay_finished_()) // During initial delay, state is assumed
199 return false;
200 return !this->is_opening_();
201 }
202 return this->position >= this->target_position_;
204 if (this->target_position_ == COVER_CLOSED) {
205 if (!this->is_initial_delay_finished_()) // During initial delay, state is assumed
206 return false;
207 return !this->is_closing_();
208 }
209 return this->position <= this->target_position_;
211 default:
212 return true;
213 }
214}
216 if (dir == this->current_operation)
217 return;
218
219 this->recompute_position_();
220 Trigger<> *trig;
221 switch (dir) {
223 trig = &this->stop_trigger_;
224 break;
226 this->last_operation_ = dir;
227 trig = &this->open_trigger_;
228 break;
230 this->last_operation_ = dir;
231 trig = &this->close_trigger_;
232 break;
233 default:
234 return;
235 }
236
237 this->current_operation = dir;
238
239 this->stop_prev_trigger_();
240 trig->trigger();
241 this->prev_command_trigger_ = trig;
242
243 const auto now = millis();
244 this->start_dir_time_ = now;
245 this->last_recompute_time_ = now;
246}
249 return;
250
251 float dir;
252 float action_dur;
253 switch (this->current_operation) {
255 dir = 1.0F;
256 action_dur = this->open_duration_;
257 break;
259 dir = -1.0F;
260 action_dur = this->close_duration_;
261 break;
262 default:
263 return;
264 }
265
266 const auto now = millis();
267 this->position += dir * (now - this->last_recompute_time_) / action_dur;
268 this->position = clamp(this->position, 0.0F, 1.0F);
269
270 this->last_recompute_time_ = now;
271}
272
273} // namespace current_based
274} // namespace esphome
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:443
constexpr const char * c_str() const
Definition string_ref.h:73
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:325
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:333
const optional< bool > & get_toggle() const
Definition cover.cpp:94
const optional< float > & get_position() const
Definition cover.cpp:92
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:115
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:180
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:143
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
void start_direction_(cover::CoverOperation dir)
void control(const cover::CoverCall &call) override
void direction_idle_(float new_position=FLT_MAX)
bool has_value() const
Definition optional.h:92
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:118
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:79
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:83
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:85
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
size_t size_t pos
Definition helpers.h:854
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.