Add invert_position_report to tuya.cover (#6020)

This commit is contained in:
Will Rouesnel 2024-06-05 17:52:19 +10:00 committed by GitHub
parent f36a96c8e2
commit c52d5c0279
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 3 deletions

View file

@ -16,6 +16,7 @@ CONF_DIRECTION_DATAPOINT = "direction_datapoint"
CONF_POSITION_DATAPOINT = "position_datapoint"
CONF_POSITION_REPORT_DATAPOINT = "position_report_datapoint"
CONF_INVERT_POSITION = "invert_position"
CONF_INVERT_POSITION_REPORT = "invert_position_report"
TuyaCover = tuya_ns.class_("TuyaCover", cover.Cover, cg.Component)
@ -47,6 +48,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_MIN_VALUE, default=0): cv.int_,
cv.Optional(CONF_MAX_VALUE, default=100): cv.int_,
cv.Optional(CONF_INVERT_POSITION, default=False): cv.boolean,
cv.Optional(CONF_INVERT_POSITION_REPORT, default=False): cv.boolean,
cv.Optional(CONF_RESTORE_MODE, default="RESTORE"): cv.enum(
RESTORE_MODES, upper=True
),
@ -71,6 +73,7 @@ async def to_code(config):
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))
cg.add(var.set_max_value(config[CONF_MAX_VALUE]))
cg.add(var.set_invert_position(config[CONF_INVERT_POSITION]))
cg.add(var.set_invert_position_report(config[CONF_INVERT_POSITION_REPORT]))
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
paren = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren))

View file

@ -51,7 +51,7 @@ void TuyaCover::setup() {
return;
}
auto pos = float(datapoint.value_uint - this->min_value_) / this->value_range_;
this->position = 1.0f - pos;
this->position = this->invert_position_report_ ? pos : 1.0f - pos;
this->publish_state();
});
}
@ -62,7 +62,7 @@ void TuyaCover::control(const cover::CoverCall &call) {
this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_STOP);
} else {
auto pos = this->position;
pos = 1.0f - pos;
pos = this->invert_position_report_ ? pos : 1.0f - pos;
auto position_int = static_cast<uint32_t>(pos * this->value_range_);
position_int = position_int + this->min_value_;
@ -78,7 +78,7 @@ void TuyaCover::control(const cover::CoverCall &call) {
this->parent_->force_set_enum_datapoint_value(*this->control_id_, COMMAND_CLOSE);
}
} else {
pos = 1.0f - pos;
pos = this->invert_position_report_ ? pos : 1.0f - pos;
auto position_int = static_cast<uint32_t>(pos * this->value_range_);
position_int = position_int + this->min_value_;
@ -112,6 +112,9 @@ void TuyaCover::dump_config() {
ESP_LOGCONFIG(TAG, " Configured as Inverted, but direction_datapoint isn't configured");
}
}
if (this->invert_position_report_) {
ESP_LOGCONFIG(TAG, " Position Reporting Inverted");
}
if (this->control_id_.has_value()) {
ESP_LOGCONFIG(TAG, " Control has datapoint ID %u", *this->control_id_);
}

View file

@ -25,6 +25,7 @@ class TuyaCover : public cover::Cover, public Component {
void set_min_value(uint32_t min_value) { min_value_ = min_value; }
void set_max_value(uint32_t max_value) { max_value_ = max_value; }
void set_invert_position(bool invert_position) { invert_position_ = invert_position; }
void set_invert_position_report(bool invert_position_report) { invert_position_report_ = invert_position_report; }
void set_restore_mode(TuyaCoverRestoreMode restore_mode) { restore_mode_ = restore_mode; }
protected:
@ -42,6 +43,7 @@ class TuyaCover : public cover::Cover, public Component {
uint32_t max_value_;
uint32_t value_range_;
bool invert_position_;
bool invert_position_report_;
};
} // namespace tuya