ESPHome 2026.1.4
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 {
34 } else {
37 }
38 }
39 }
40 if (call.get_position().has_value()) {
41 auto pos = *call.get_position();
42 if (pos == this->position) {
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 }
56
57 if (this->is_open_()) {
58 this->position = COVER_OPEN;
59 } else if (this->is_closed_()) {
60 this->position = COVER_CLOSED;
61 } else if (!restore.has_value()) {
62 this->position = 0.5f;
63 }
64}
67 return;
68
69 const uint32_t now = App.get_loop_component_start_time();
70
71 if (this->current_operation == COVER_OPERATION_OPENING && this->is_open_()) {
72 float dur = (now - this->start_dir_time_) / 1e3f;
73 ESP_LOGD(TAG, "'%s' - Open endstop reached. Took %.1fs.", this->name_.c_str(), dur);
74
76 this->position = COVER_OPEN;
77 this->publish_state();
78 } else if (this->current_operation == COVER_OPERATION_CLOSING && this->is_closed_()) {
79 float dur = (now - this->start_dir_time_) / 1e3f;
80 ESP_LOGD(TAG, "'%s' - Close endstop reached. Took %.1fs.", this->name_.c_str(), dur);
81
83 this->position = COVER_CLOSED;
84 this->publish_state();
85 } else if (now - this->start_dir_time_ > this->max_duration_) {
86 ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
88 this->publish_state();
89 }
90
91 // Recompute position every loop cycle
92 this->recompute_position_();
93
96 this->publish_state();
97 }
98
99 // Send current position every second
100 if (this->current_operation != COVER_OPERATION_IDLE && now - this->last_publish_time_ > 1000) {
101 this->publish_state(false);
102 this->last_publish_time_ = now;
103 }
104}
106 LOG_COVER("", "Endstop Cover", this);
107 ESP_LOGCONFIG(TAG,
108 " Open Duration: %.1fs\n"
109 " Close Duration: %.1fs",
110 this->open_duration_ / 1e3f, this->close_duration_ / 1e3f);
111 LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
112 LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
113}
116 if (this->prev_command_trigger_ != nullptr) {
118 this->prev_command_trigger_ = nullptr;
119 }
120}
122 switch (this->current_operation) {
124 if (this->target_position_ == COVER_OPEN)
125 return this->is_open_();
126 return this->position >= this->target_position_;
128 if (this->target_position_ == COVER_CLOSED)
129 return this->is_closed_();
130 return this->position <= this->target_position_;
132 default:
133 return true;
134 }
135}
137 if (dir == this->current_operation)
138 return;
139
140 this->recompute_position_();
141 Trigger<> *trig;
142 switch (dir) {
144 trig = this->stop_trigger_;
145 break;
147 this->last_operation_ = dir;
148 trig = this->open_trigger_;
149 break;
151 this->last_operation_ = dir;
152 trig = this->close_trigger_;
153 break;
154 default:
155 return;
156 }
157
158 this->current_operation = dir;
159
160 this->stop_prev_trigger_();
161 trig->trigger();
162 this->prev_command_trigger_ = trig;
163
164 const uint32_t now = millis();
165 this->start_dir_time_ = now;
166 this->last_recompute_time_ = now;
167}
170 return;
171
172 float dir;
173 float action_dur;
174 switch (this->current_operation) {
176 dir = 1.0f;
177 action_dur = this->open_duration_;
178 break;
180 dir = -1.0f;
181 action_dur = this->close_duration_;
182 break;
183 default:
184 return;
185 }
186
187 const uint32_t now = millis();
188 this->position += dir * (now - this->last_recompute_time_) / action_dur;
189 this->position = clamp(this->position, 0.0f, 1.0f);
190
191 this->last_recompute_time_ = now;
192}
193
194} // namespace endstop
195} // 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:238
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:246
const optional< bool > & get_toggle() const
Definition cover.cpp:103
const optional< float > & get_position() const
Definition cover.cpp:101
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:117
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:189
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:152
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:123
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
float get_setup_priority() const override
cover::CoverOperation last_operation_
bool has_value() const
Definition optional.h:92
const float COVER_CLOSED
Definition cover.cpp:14
const float COVER_OPEN
Definition cover.cpp:13
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:81
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:85
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:87
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:83
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.