ESPHome 2026.3.0
Loading...
Searching...
No Matches
endstop_cover.cpp
Go to the documentation of this file.
1#include "endstop_cover.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
5
6namespace esphome {
7namespace endstop {
8
9static const char *const TAG = "endstop.cover";
10
11using namespace esphome::cover;
12
14 auto traits = CoverTraits();
15 traits.set_supports_stop(true);
16 traits.set_supports_position(true);
17 traits.set_supports_toggle(true);
18 traits.set_is_assumed_state(false);
19 return traits;
20}
22 if (call.get_stop()) {
24 this->publish_state();
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 auto opt_pos = call.get_position();
41 if (opt_pos.has_value()) {
42 auto pos = *opt_pos;
43 if (pos == this->position) {
44 // already at target
45 } else {
47 this->target_position_ = pos;
48 this->start_direction_(op);
49 }
50 }
51}
53 auto restore = this->restore_state_();
54 if (restore.has_value()) {
55 restore->apply(this);
56 }
57
58 if (this->is_open_()) {
59 this->position = COVER_OPEN;
60 } else if (this->is_closed_()) {
61 this->position = COVER_CLOSED;
62 } else if (!restore.has_value()) {
63 this->position = 0.5f;
64 }
65}
68 return;
69
71
72 if (this->current_operation == COVER_OPERATION_OPENING && this->is_open_()) {
73 float dur = (now - this->start_dir_time_) / 1e3f;
74 ESP_LOGD(TAG, "'%s' - Open endstop reached. Took %.1fs.", this->name_.c_str(), dur);
75
77 this->position = COVER_OPEN;
78 this->publish_state();
79 } else if (this->current_operation == COVER_OPERATION_CLOSING && this->is_closed_()) {
80 float dur = (now - this->start_dir_time_) / 1e3f;
81 ESP_LOGD(TAG, "'%s' - Close endstop reached. Took %.1fs.", this->name_.c_str(), dur);
82
84 this->position = COVER_CLOSED;
85 this->publish_state();
86 } else if (now - this->start_dir_time_ > this->max_duration_) {
87 ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
89 this->publish_state();
90 }
91
92 // Recompute position every loop cycle
93 this->recompute_position_();
94
97 this->publish_state();
98 }
99
100 // Send current position every second
101 if (this->current_operation != COVER_OPERATION_IDLE && now - this->last_publish_time_ > 1000) {
102 this->publish_state(false);
103 this->last_publish_time_ = now;
104 }
105}
107 LOG_COVER("", "Endstop Cover", this);
108 ESP_LOGCONFIG(TAG,
109 " Open Duration: %.1fs\n"
110 " Close Duration: %.1fs",
111 this->open_duration_ / 1e3f, this->close_duration_ / 1e3f);
112 LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
113 LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
114}
115
117 if (this->prev_command_trigger_ != nullptr) {
119 this->prev_command_trigger_ = nullptr;
120 }
121}
123 switch (this->current_operation) {
125 if (this->target_position_ == COVER_OPEN)
126 return this->is_open_();
127 return this->position >= this->target_position_;
129 if (this->target_position_ == COVER_CLOSED)
130 return this->is_closed_();
131 return this->position <= this->target_position_;
133 default:
134 return true;
135 }
136}
138 if (dir == this->current_operation)
139 return;
140
141 this->recompute_position_();
142 Trigger<> *trig;
143 switch (dir) {
145 trig = &this->stop_trigger_;
146 break;
148 this->last_operation_ = dir;
149 trig = &this->open_trigger_;
150 break;
152 this->last_operation_ = dir;
153 trig = &this->close_trigger_;
154 break;
155 default:
156 return;
157 }
158
159 this->current_operation = dir;
160
161 this->stop_prev_trigger_();
162 trig->trigger();
163 this->prev_command_trigger_ = trig;
164
165 const uint32_t now = millis();
166 this->start_dir_time_ = now;
167 this->last_recompute_time_ = now;
168}
171 return;
172
173 float dir;
174 float action_dur;
175 switch (this->current_operation) {
177 dir = 1.0f;
178 action_dur = this->open_duration_;
179 break;
181 dir = -1.0f;
182 action_dur = this->close_duration_;
183 break;
184 default:
185 return;
186 }
187
188 const uint32_t now = millis();
189 this->position += dir * (now - this->last_recompute_time_) / action_dur;
190 this->position = clamp(this->position, 0.0f, 1.0f);
191
192 this->last_recompute_time_ = now;
193}
194
195} // namespace endstop
196} // 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.
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
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
binary_sensor::BinarySensor * open_endstop_
cover::CoverTraits get_traits() override
void start_direction_(cover::CoverOperation dir)
binary_sensor::BinarySensor * close_endstop_
void control(const cover::CoverCall &call) override
cover::CoverOperation last_operation_
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:929
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t