ESPHome 2025.5.0
Loading...
Searching...
No Matches
tuya_cover.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tuya_cover.h"
3
4namespace esphome {
5namespace tuya {
6
7const uint8_t COMMAND_OPEN = 0x00;
8const uint8_t COMMAND_CLOSE = 0x02;
9const uint8_t COMMAND_STOP = 0x01;
10
11using namespace esphome::cover;
12
13static const char *const TAG = "tuya.cover";
14
16 this->value_range_ = this->max_value_ - this->min_value_;
17
18 this->parent_->add_on_initialized_callback([this]() {
19 // Set the direction (if configured/supported).
21
22 // Handle configured restore mode.
23 switch (this->restore_mode_) {
25 break;
26 case COVER_RESTORE: {
27 auto restore = this->restore_state_();
28 if (restore.has_value())
29 restore->apply(this);
30 break;
31 }
33 auto restore = this->restore_state_();
34 if (restore.has_value()) {
35 restore->to_call(this).perform();
36 }
37 break;
38 }
39 }
40 });
41
42 uint8_t report_id = *this->position_id_;
43 if (this->position_report_id_.has_value()) {
44 // A position report datapoint is configured; listen to that instead.
45 report_id = *this->position_report_id_;
46 }
47
48 this->parent_->register_listener(report_id, [this](const TuyaDatapoint &datapoint) {
49 if (datapoint.value_int == 123) {
50 ESP_LOGD(TAG, "Ignoring MCU position report - not calibrated");
51 return;
52 }
53 auto pos = float(datapoint.value_uint - this->min_value_) / this->value_range_;
54 this->position = this->invert_position_report_ ? pos : 1.0f - pos;
55 this->publish_state();
56 });
57}
58
60 if (call.get_stop()) {
61 if (this->control_id_.has_value()) {
62 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_STOP);
63 } else {
64 auto pos = this->position;
65 pos = this->invert_position_report_ ? pos : 1.0f - pos;
66 auto position_int = static_cast<uint32_t>(pos * this->value_range_);
67 position_int = position_int + this->min_value_;
68
70 }
71 }
72 if (call.get_position().has_value()) {
73 auto pos = *call.get_position();
74 if (this->control_id_.has_value() && (pos == COVER_OPEN || pos == COVER_CLOSED)) {
75 if (pos == COVER_OPEN) {
76 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_OPEN);
77 } else {
78 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_CLOSE);
79 }
80 } else {
81 pos = this->invert_position_report_ ? pos : 1.0f - pos;
82 auto position_int = static_cast<uint32_t>(pos * this->value_range_);
83 position_int = position_int + this->min_value_;
84
86 }
87 }
88
89 this->publish_state();
90}
91
92void TuyaCover::set_direction_(bool inverted) {
93 if (!this->direction_id_.has_value()) {
94 return;
95 }
96
97 if (inverted) {
98 ESP_LOGD(TAG, "Setting direction: inverted");
99 } else {
100 ESP_LOGD(TAG, "Setting direction: normal");
101 }
102
103 this->parent_->set_boolean_datapoint_value(*this->direction_id_, inverted);
104}
105
107 ESP_LOGCONFIG(TAG, "Tuya Cover:");
108 if (this->invert_position_) {
109 if (this->direction_id_.has_value()) {
110 ESP_LOGCONFIG(TAG, " Inverted");
111 } else {
112 ESP_LOGCONFIG(TAG, " Configured as Inverted, but direction_datapoint isn't configured");
113 }
114 }
115 if (this->invert_position_report_) {
116 ESP_LOGCONFIG(TAG, " Position Reporting Inverted");
117 }
118 if (this->control_id_.has_value()) {
119 ESP_LOGCONFIG(TAG, " Control has datapoint ID %u", *this->control_id_);
120 }
121 if (this->direction_id_.has_value()) {
122 ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *this->direction_id_);
123 }
124 if (this->position_id_.has_value()) {
125 ESP_LOGCONFIG(TAG, " Position has datapoint ID %u", *this->position_id_);
126 }
127 if (this->position_report_id_.has_value()) {
128 ESP_LOGCONFIG(TAG, " Position Report has datapoint ID %u", *this->position_report_id_);
129 }
130}
131
133 auto traits = cover::CoverTraits();
134 traits.set_supports_stop(true);
135 traits.set_supports_position(true);
136 return traits;
137}
138
139} // namespace tuya
140} // namespace esphome
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
bool has_value() const
Definition optional.h:87
void dump_config() override
optional< uint8_t > control_id_
Definition tuya_cover.h:38
cover::CoverTraits get_traits() override
optional< uint8_t > position_report_id_
Definition tuya_cover.h:41
void set_direction_(bool inverted)
optional< uint8_t > direction_id_
Definition tuya_cover.h:39
void setup() override
optional< uint8_t > position_id_
Definition tuya_cover.h:40
void control(const cover::CoverCall &call) override
TuyaCoverRestoreMode restore_mode_
Definition tuya_cover.h:37
void add_on_initialized_callback(std::function< void()> callback)
Definition tuya.h:115
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:569
void force_set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:597
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:697
void force_set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:605
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
const uint8_t COMMAND_OPEN
Definition tuya_cover.cpp:7
@ COVER_RESTORE_AND_CALL
Definition tuya_cover.h:13
const uint8_t COMMAND_CLOSE
Definition tuya_cover.cpp:8
const uint8_t COMMAND_STOP
Definition tuya_cover.cpp:9
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t report_id
Definition tt21100.cpp:1