ESPHome 2026.11.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::he60r {
8
9ESPHOME_LOG_TAG(TAG, "he60r.cover");
10static const uint8_t QUERY_BYTE = 0x38;
11static const uint8_t TOGGLE_BYTE = 0x30;
12
13using namespace esphome::cover;
14
16 auto restore = this->restore_state_();
17
18 if (restore.has_value()) {
19 restore->apply(this);
20 this->publish_state(false);
21 } else {
22 // if no other information, assume half open
23 this->position = 0.5f;
24 }
27 this->set_interval(300, [this]() { this->update_(); });
28}
29
31 auto traits = CoverTraits();
32 traits.set_supports_stop(true);
33 traits.set_supports_position(true);
34 traits.set_supports_toggle(true);
35 traits.set_is_assumed_state(false);
36 return traits;
37}
38
40 LOG_COVER("", "HE60R Cover", this);
41 ESP_LOGCONFIG(TAG,
42 " Open Duration: %.1fs\n"
43 " Close Duration: %.1fs",
44 this->open_duration_ / 1e3f, this->close_duration_ / 1e3f);
45 auto restore = this->restore_state_();
46 if (restore.has_value()) {
47 ESP_LOGCONFIG(TAG, " Saved position %d%%", (int) (restore->position * 100.f));
48 }
49}
50
52 const uint32_t now = millis();
53
55 auto new_position = operation == COVER_OPERATION_OPENING ? COVER_OPEN : COVER_CLOSED;
56 if (new_position != this->position || this->current_operation != COVER_OPERATION_IDLE) {
57 this->position = new_position;
59 if (this->last_command_ == operation) {
60 float dur = (float) (now - this->start_dir_time_) / 1e3f;
61 ESP_LOGD(TAG, "'%s' - %s endstop reached. Took %.1fs.", LOG_STR_ARG(this->get_log_name()),
62 operation == COVER_OPERATION_OPENING ? LOG_STR_LITERAL("Open") : LOG_STR_LITERAL("Close"), dur);
63 }
64 this->publish_state();
65 }
66}
67
69 if (this->current_operation != operation) {
70 this->current_operation = operation;
71 if (operation != COVER_OPERATION_IDLE)
73 }
74}
75
76void HE60rCover::process_rx_(uint8_t data) {
77 ESP_LOGV(TAG, "Process RX data %X", data);
78 if (!this->query_seen_) {
79 this->query_seen_ = data == QUERY_BYTE;
80 if (!this->query_seen_) {
81 ESP_LOGD(TAG, "RX Byte %02X", data);
82 }
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 this->read_available([this](uint8_t data) { this->process_rx_(data); });
156}
157
159 if (call.get_stop()) {
161 } else if (call.get_toggle().has_value()) {
162 // toggle action logic: OPEN - STOP - CLOSE
163 if (this->last_command_ != COVER_OPERATION_IDLE) {
165 } else {
166 this->toggles_needed_++;
167 }
168 } else {
169 auto pos_opt = call.get_position();
170 if (!pos_opt.has_value())
171 return;
172 // go to position action
173 auto pos = *pos_opt;
174 // are we at the target?
175 if (pos == this->position) {
177 } else {
178 this->target_position_ = pos;
180 }
181 }
182}
183
190 // equality of floats is fraught with peril - this is reliable since the values are 0.0 or 1.0 which are
191 // exactly representable.
192 if (this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED)
193 return false;
194 // aiming for an intermediate position - exact comparison here will not work and we need to allow for overshoot
195 switch (this->last_command_) {
197 return this->position >= this->target_position_;
199 return this->position <= this->target_position_;
202 default:
203 return true;
204 }
205}
207 this->last_command_ = dir;
208 if (this->current_operation == dir)
209 return;
210 ESP_LOGD(TAG, "'%s' - Direction '%s' requested.", LOG_STR_ARG(this->get_log_name()),
211 dir == COVER_OPERATION_OPENING ? LOG_STR_LITERAL("OPEN")
212 : dir == COVER_OPERATION_CLOSING ? LOG_STR_LITERAL("CLOSE")
213 : LOG_STR_LITERAL("STOP"));
214
215 if (dir == this->next_direction_) {
216 // either moving and needs to stop, or stopped and will move correctly on one trigger
217 this->toggles_needed_ = 1;
218 } else {
220 // if stopped, but will go the wrong way, need 3 triggers.
221 this->toggles_needed_ = 3;
222 } else {
223 // just stop and reverse
224 this->toggles_needed_ = 2;
225 }
226 ESP_LOGD(TAG, "'%s' - Reversing direction.", LOG_STR_ARG(this->get_log_name()));
227 }
228 this->start_dir_time_ = millis();
229}
230
233 return;
234
235 const uint32_t now = millis();
236 if (now != this->last_recompute_time_) {
237 auto diff = (unsigned) (now - last_recompute_time_);
238 float delta;
239 switch (this->current_operation) {
241 delta = (float) diff / (float) this->open_duration_;
242 break;
244 delta = -(float) diff / (float) this->close_duration_;
245 break;
246 default:
247 return;
248 }
249
250 // make sure our guesstimate never reaches full open or close.
251 auto new_position = clamp(delta + this->position, COVER_CLOSED + 0.01f, COVER_OPEN - 0.01f);
252 ESP_LOGD(TAG, "Recompute %ums, dir=%u, delta=%f, pos=%f", diff, this->current_operation, delta, new_position);
253 this->last_recompute_time_ = now;
254 if (this->position != new_position) {
255 this->position = new_position;
256 this->publish_state();
257 }
258 }
259}
260
261} // namespace esphome::he60r
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
const optional< bool > & get_toggle() const
Definition cover.cpp:101
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:120
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:195
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:158
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:126
bool is_at_target_() const
Check if the cover has reached or passed the target position.
Definition he60r.cpp:189
void control(const cover::CoverCall &call) override
Definition he60r.cpp:158
cover::CoverOperation last_command_
Definition he60r.h:35
cover::CoverTraits get_traits() override
Definition he60r.cpp:30
void setup() override
Definition he60r.cpp:15
void loop() override
Definition he60r.cpp:154
void endstop_reached_(cover::CoverOperation operation)
Definition he60r.cpp:51
uint32_t last_recompute_time_
Definition he60r.h:36
void set_current_operation_(cover::CoverOperation operation)
Definition he60r.cpp:68
void process_rx_(uint8_t data)
Definition he60r.cpp:76
cover::CoverOperation next_direction_
Definition he60r.h:34
void dump_config() override
Definition he60r.cpp:39
void start_direction_(cover::CoverOperation dir)
Definition he60r.cpp:206
void write_byte(uint8_t data)
Definition uart.h:22
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "cover")
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:84
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:88
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:90
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:86
size_t size_t pos
Definition helpers.h:1255
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t