ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
he60r.cpp
Go to the documentation of this file.
1#include "he60r.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6
7namespace esphome {
8namespace he60r {
9
10static const char *const TAG = "he60r.cover";
11static const uint8_t QUERY_BYTE = 0x38;
12static const uint8_t TOGGLE_BYTE = 0x30;
13
14using namespace esphome::cover;
15
17 auto restore = this->restore_state_();
18
19 if (restore.has_value()) {
20 restore->apply(this);
21 this->publish_state(false);
22 } else {
23 // if no other information, assume half open
24 this->position = 0.5f;
25 }
28 this->set_interval(300, [this]() { this->update_(); });
29}
30
32 auto traits = CoverTraits();
33 traits.set_supports_stop(true);
34 traits.set_supports_position(true);
35 traits.set_supports_toggle(true);
36 traits.set_is_assumed_state(false);
37 return traits;
38}
39
41 LOG_COVER("", "HE60R Cover", this);
43 ESP_LOGCONFIG(TAG,
44 " Open Duration: %.1fs\n"
45 " Close Duration: %.1fs",
46 this->open_duration_ / 1e3f, this->close_duration_ / 1e3f);
47 auto restore = this->restore_state_();
48 if (restore.has_value())
49 ESP_LOGCONFIG(TAG, " Saved position %d%%", (int) (restore->position * 100.f));
50}
51
53 const uint32_t now = millis();
54
56 auto new_position = operation == COVER_OPERATION_OPENING ? COVER_OPEN : COVER_CLOSED;
57 if (new_position != this->position || this->current_operation != COVER_OPERATION_IDLE) {
58 this->position = new_position;
60 if (this->last_command_ == operation) {
61 float dur = (float) (now - this->start_dir_time_) / 1e3f;
62 ESP_LOGD(TAG, "'%s' - %s endstop reached. Took %.1fs.", this->name_.c_str(),
63 operation == COVER_OPERATION_OPENING ? "Open" : "Close", dur);
64 }
65 this->publish_state();
66 }
67}
68
70 if (this->current_operation != operation) {
71 this->current_operation = operation;
72 if (operation != COVER_OPERATION_IDLE)
74 }
75}
76
77void HE60rCover::process_rx_(uint8_t data) {
78 ESP_LOGV(TAG, "Process RX data %X", data);
79 if (!this->query_seen_) {
80 this->query_seen_ = data == QUERY_BYTE;
81 if (!this->query_seen_)
82 ESP_LOGD(TAG, "RX Byte %02X", data);
83 return;
84 }
85 switch (data) {
86 case 0xB5: // at closed endstop, jammed?
87 case 0xF5: // at closed endstop, jammed?
88 case 0x55: // at closed endstop
91 break;
92
93 case 0x52: // at opened endstop
96 break;
97
98 case 0x51: // travelling up after encountering obstacle
99 case 0x01: // travelling up
100 case 0x11: // travelling up, triggered by remote
103 break;
104
105 case 0x44: // travelling down
106 case 0x14: // travelling down, triggered by remote
109 break;
110
111 case 0x86: // Stopped, jammed?
112 case 0x16: // stopped midway while opening, by remote
113 case 0x06: // stopped midway while opening
116 break;
117
118 case 0x10: // stopped midway while closing, by remote
119 case 0x00: // stopped midway while closing
122 break;
123
124 default:
125 break;
126 }
127}
128
130 if (this->toggles_needed_ != 0) {
131 if ((this->counter_++ & 0x3) == 0) {
132 this->toggles_needed_--;
133 ESP_LOGD(TAG, "Writing byte 0x30, still needed=%u", this->toggles_needed_);
134 this->write_byte(TOGGLE_BYTE);
135 } else {
136 this->write_byte(QUERY_BYTE);
137 }
138 } else {
139 this->write_byte(QUERY_BYTE);
140 this->counter_ = 0;
141 }
143 this->recompute_position_();
144
145 // if we initiated the move, check if we reached the target position
146 if (this->last_command_ != COVER_OPERATION_IDLE) {
147 if (this->is_at_target_()) {
149 }
150 }
151 }
152}
153
155 uint8_t data;
156
157 while (this->available() > 0) {
158 if (this->read_byte(&data)) {
159 this->process_rx_(data);
160 }
161 }
162}
163
165 if (call.get_stop()) {
167 } else if (call.get_toggle().has_value()) {
168 // toggle action logic: OPEN - STOP - CLOSE
169 if (this->last_command_ != COVER_OPERATION_IDLE) {
171 } else {
172 this->toggles_needed_++;
173 }
174 } else {
175 auto pos_opt = call.get_position();
176 if (!pos_opt.has_value())
177 return;
178 // go to position action
179 auto pos = *pos_opt;
180 // are we at the target?
181 if (pos == this->position) {
183 } else {
184 this->target_position_ = pos;
186 }
187 }
188}
189
196 // equality of floats is fraught with peril - this is reliable since the values are 0.0 or 1.0 which are
197 // exactly representable.
198 if (this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED)
199 return false;
200 // aiming for an intermediate position - exact comparison here will not work and we need to allow for overshoot
201 switch (this->last_command_) {
203 return this->position >= this->target_position_;
205 return this->position <= this->target_position_;
208 default:
209 return true;
210 }
211}
213 this->last_command_ = dir;
214 if (this->current_operation == dir)
215 return;
216 ESP_LOGD(TAG, "'%s' - Direction '%s' requested.", this->name_.c_str(),
217 dir == COVER_OPERATION_OPENING ? "OPEN"
218 : dir == COVER_OPERATION_CLOSING ? "CLOSE"
219 : "STOP");
220
221 if (dir == this->next_direction_) {
222 // either moving and needs to stop, or stopped and will move correctly on one trigger
223 this->toggles_needed_ = 1;
224 } else {
226 // if stopped, but will go the wrong way, need 3 triggers.
227 this->toggles_needed_ = 3;
228 } else {
229 // just stop and reverse
230 this->toggles_needed_ = 2;
231 }
232 ESP_LOGD(TAG, "'%s' - Reversing direction.", this->name_.c_str());
233 }
234 this->start_dir_time_ = millis();
235}
236
239 return;
240
241 const uint32_t now = millis();
242 if (now != this->last_recompute_time_) {
243 auto diff = (unsigned) (now - last_recompute_time_);
244 float delta;
245 switch (this->current_operation) {
247 delta = (float) diff / (float) this->open_duration_;
248 break;
250 delta = -(float) diff / (float) this->close_duration_;
251 break;
252 default:
253 return;
254 }
255
256 // make sure our guesstimate never reaches full open or close.
257 auto new_position = clamp(delta + this->position, COVER_CLOSED + 0.01f, COVER_OPEN - 0.01f);
258 ESP_LOGD(TAG, "Recompute %ums, dir=%u, delta=%f, pos=%f", diff, this->current_operation, delta, new_position);
259 this->last_recompute_time_ = now;
260 if (this->position != new_position) {
261 this->position = new_position;
262 this->publish_state();
263 }
264 }
265}
266
267} // namespace he60r
268} // namespace esphome
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:417
constexpr const char * c_str() const
Definition string_ref.h:73
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:179
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:142
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
bool is_at_target_() const
Check if the cover has reached or passed the target position.
Definition he60r.cpp:195
void control(const cover::CoverCall &call) override
Definition he60r.cpp:164
cover::CoverOperation last_command_
Definition he60r.h:36
cover::CoverTraits get_traits() override
Definition he60r.cpp:31
void setup() override
Definition he60r.cpp:16
void loop() override
Definition he60r.cpp:154
void endstop_reached_(cover::CoverOperation operation)
Definition he60r.cpp:52
uint32_t last_recompute_time_
Definition he60r.h:37
void set_current_operation_(cover::CoverOperation operation)
Definition he60r.cpp:69
void process_rx_(uint8_t data)
Definition he60r.cpp:77
cover::CoverOperation next_direction_
Definition he60r.h:35
void dump_config() override
Definition he60r.cpp:40
void start_direction_(cover::CoverOperation dir)
Definition he60r.cpp:212
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_byte(uint8_t data)
Definition uart.h:18
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:1082
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t