13 static const char *
const TAG =
"tuya.cover";
16 this->value_range_ = this->max_value_ - this->min_value_;
18 this->parent_->add_on_initialized_callback([
this]() {
20 this->set_direction_(this->invert_position_);
23 switch (this->restore_mode_) {
27 auto restore = this->restore_state_();
28 if (restore.has_value())
33 auto restore = this->restore_state_();
34 if (restore.has_value()) {
35 restore->to_call(
this).perform();
43 if (this->position_report_id_.has_value()) {
45 report_id = *this->position_report_id_;
48 this->parent_->register_listener(report_id, [
this](
const TuyaDatapoint &datapoint) {
50 ESP_LOGD(TAG,
"Ignoring MCU position report - not calibrated");
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();
61 if (this->control_id_.has_value()) {
62 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_STOP);
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_;
69 parent_->force_set_integer_datapoint_value(*this->position_id_, position_int);
76 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_OPEN);
78 this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_CLOSE);
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_;
85 parent_->force_set_integer_datapoint_value(*this->position_id_, position_int);
89 this->publish_state();
93 if (!this->direction_id_.has_value()) {
98 ESP_LOGD(TAG,
"Setting direction: inverted");
100 ESP_LOGD(TAG,
"Setting direction: normal");
103 this->parent_->set_boolean_datapoint_value(*this->direction_id_, inverted);
107 ESP_LOGCONFIG(TAG,
"Tuya Cover:");
108 if (this->invert_position_) {
109 if (this->direction_id_.has_value()) {
110 ESP_LOGCONFIG(TAG,
" Inverted");
112 ESP_LOGCONFIG(TAG,
" Configured as Inverted, but direction_datapoint isn't configured");
115 if (this->invert_position_report_) {
116 ESP_LOGCONFIG(TAG,
" Position Reporting Inverted");
118 if (this->control_id_.has_value()) {
119 ESP_LOGCONFIG(TAG,
" Control has datapoint ID %u", *this->control_id_);
121 if (this->direction_id_.has_value()) {
122 ESP_LOGCONFIG(TAG,
" Direction has datapoint ID %u", *this->direction_id_);
124 if (this->position_id_.has_value()) {
125 ESP_LOGCONFIG(TAG,
" Position has datapoint ID %u", *this->position_id_);
127 if (this->position_report_id_.has_value()) {
128 ESP_LOGCONFIG(TAG,
" Position Report has datapoint ID %u", *this->position_report_id_);
134 traits.set_supports_stop(
true);
135 traits.set_supports_position(
true);
const uint8_t COMMAND_OPEN
const uint8_t COMMAND_STOP
cover::CoverTraits get_traits() override
void set_direction_(bool inverted)
const uint8_t COMMAND_CLOSE
void control(const cover::CoverCall &call) override
Implementation of SPI Controller mode.
const optional< float > & get_position() const
void dump_config() override