8 static const char *
const TAG =
"he60r.cover";
9 static const uint8_t QUERY_BYTE = 0x38;
10 static const uint8_t TOGGLE_BYTE = 0x30;
15 auto restore = this->restore_state_();
17 if (restore.has_value()) {
19 this->publish_state(
false);
25 this->last_recompute_time_ = this->start_dir_time_ =
millis();
26 this->set_interval(300, [
this]() { this->update_(); });
31 traits.set_supports_stop(
true);
32 traits.set_supports_position(
true);
33 traits.set_supports_toggle(
true);
34 traits.set_is_assumed_state(
false);
39 LOG_COVER(
"",
"HE60R Cover",
this);
41 ESP_LOGCONFIG(TAG,
" Open Duration: %.1fs", this->open_duration_ / 1e3f);
42 ESP_LOGCONFIG(TAG,
" Close Duration: %.1fs", this->close_duration_ / 1e3f);
43 auto restore = this->restore_state_();
44 if (restore.has_value())
45 ESP_LOGCONFIG(TAG,
" Saved position %d%%", (
int) (restore->position * 100.f));
49 const uint32_t now =
millis();
56 if (this->last_command_ == operation) {
57 float dur = (now - this->start_dir_time_) / 1e3f;
58 ESP_LOGD(TAG,
"'%s' - %s endstop reached. Took %.1fs.", this->name_.c_str(),
61 this->publish_state();
66 if (this->current_operation != operation) {
67 this->current_operation = operation;
69 this->last_recompute_time_ =
millis();
70 this->publish_state();
75 ESP_LOGV(TAG,
"Process RX data %X", data);
76 if (!this->query_seen_) {
77 this->query_seen_ = data == QUERY_BYTE;
78 if (!this->query_seen_)
79 ESP_LOGD(TAG,
"RX Byte %02X", data);
127 if (toggles_needed_ != 0) {
128 if ((this->counter_++ & 0x3) == 0) {
130 ESP_LOGD(TAG,
"Writing byte 0x30, still needed=%d", toggles_needed_);
131 this->write_byte(TOGGLE_BYTE);
133 this->write_byte(QUERY_BYTE);
136 this->write_byte(QUERY_BYTE);
140 this->recompute_position_();
144 if (this->is_at_target_()) {
154 while (this->available() > 0) {
155 if (this->read_byte(&data)) {
156 this->process_rx_(data);
169 this->toggles_needed_++;
178 this->target_position_ = pos;
195 switch (this->last_command_) {
197 return this->
position >= this->target_position_;
199 return this->
position <= this->target_position_;
207 this->last_command_ = dir;
208 if (this->current_operation == dir)
210 ESP_LOGD(TAG,
"'%s' - Direction '%s' requested.", this->name_.c_str(),
215 if (dir == this->next_direction_) {
217 this->toggles_needed_ = 1;
221 this->toggles_needed_ = 3;
224 this->toggles_needed_ = 2;
226 ESP_LOGD(TAG,
"'%s' - Reversing direction.", this->name_.c_str());
228 this->start_dir_time_ =
millis();
235 const uint32_t now =
millis();
239 switch (this->current_operation) {
242 action_dur = this->open_duration_;
246 action_dur = this->close_duration_;
252 if (now > this->last_recompute_time_) {
253 auto diff = now - last_recompute_time_;
254 auto delta = dir * diff / action_dur;
257 ESP_LOGD(TAG,
"Recompute %dms, dir=%f, action_dur=%f, delta=%f, pos=%f", (
int) diff, dir, action_dur, delta,
259 this->last_recompute_time_ = now;
260 this->publish_state();
CoverOperation
Enum encoding the current operation of a cover.
void set_current_operation_(cover::CoverOperation operation)
The cover is currently closing.
void dump_config() override
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
uint32_t IRAM_ATTR HOT millis()
bool is_at_target_() const
Check if the cover has reached or passed the target position.
const optional< bool > & get_toggle() const
void process_rx_(uint8_t data)
void endstop_reached_(cover::CoverOperation operation)
cover::CoverTraits get_traits() override
void start_direction_(cover::CoverOperation dir)
void control(const cover::CoverCall &call) override
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
The cover is currently opening.
const optional< float > & get_position() const
void recompute_position_()