ESPHome 2025.5.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 {
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 LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
108 ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
109 LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
110 ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
111}
114 if (this->prev_command_trigger_ != nullptr) {
116 this->prev_command_trigger_ = nullptr;
117 }
118}
120 switch (this->current_operation) {
122 if (this->target_position_ == COVER_OPEN)
123 return this->is_open_();
124 return this->position >= this->target_position_;
126 if (this->target_position_ == COVER_CLOSED)
127 return this->is_closed_();
128 return this->position <= this->target_position_;
130 default:
131 return true;
132 }
133}
135 if (dir == this->current_operation)
136 return;
137
138 this->recompute_position_();
139 Trigger<> *trig;
140 switch (dir) {
142 trig = this->stop_trigger_;
143 break;
145 this->last_operation_ = dir;
146 trig = this->open_trigger_;
147 break;
149 this->last_operation_ = dir;
150 trig = this->close_trigger_;
151 break;
152 default:
153 return;
154 }
155
156 this->current_operation = dir;
157
158 this->stop_prev_trigger_();
159 trig->trigger();
160 this->prev_command_trigger_ = trig;
161
162 const uint32_t now = millis();
163 this->start_dir_time_ = now;
164 this->last_recompute_time_ = now;
165}
168 return;
169
170 float dir;
171 float action_dur;
172 switch (this->current_operation) {
174 dir = 1.0f;
175 action_dur = this->open_duration_;
176 break;
178 dir = -1.0f;
179 action_dur = this->close_duration_;
180 break;
181 default:
182 return;
183 }
184
185 const uint32_t now = millis();
186 this->position += dir * (now - this->last_recompute_time_) / action_dur;
187 this->position = clamp(this->position, 0.0f, 1.0f);
188
189 this->last_recompute_time_ = now;
190}
191
192} // namespace endstop
193} // 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:68
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:104
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
const optional< bool > & get_toggle() const
Definition cover.cpp:99
const optional< float > & get_position() const
Definition cover.cpp:97
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:116
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:200
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:166
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:122
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:87
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:80
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:84
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:86
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:82
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
Application App
Global storage of Application pointer - only one Application can exist.
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101