Tuya Number: split "multiply" to a separate option (#5458)

This commit is contained in:
Faidon Liambotis 2023-10-03 03:23:18 +03:00 committed by GitHub
parent e95ba57a61
commit f38849828d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

View file

@ -6,6 +6,7 @@ from esphome.const import (
CONF_NUMBER_DATAPOINT,
CONF_MAX_VALUE,
CONF_MIN_VALUE,
CONF_MULTIPLY,
CONF_STEP,
)
from .. import tuya_ns, CONF_TUYA_ID, Tuya
@ -31,6 +32,7 @@ CONFIG_SCHEMA = cv.All(
cv.Required(CONF_MAX_VALUE): cv.float_,
cv.Required(CONF_MIN_VALUE): cv.float_,
cv.Required(CONF_STEP): cv.positive_float,
cv.Optional(CONF_MULTIPLY, default=1.0): cv.float_,
}
)
.extend(cv.COMPONENT_SCHEMA),
@ -49,7 +51,8 @@ async def to_code(config):
step=config[CONF_STEP],
)
paren = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren))
cg.add(var.set_write_multiply(config[CONF_MULTIPLY]))
parent = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(parent))
cg.add(var.set_number_id(config[CONF_NUMBER_DATAPOINT]))

View file

@ -10,7 +10,7 @@ void TuyaNumber::setup() {
this->parent_->register_listener(this->number_id_, [this](const TuyaDatapoint &datapoint) {
if (datapoint.type == TuyaDatapointType::INTEGER) {
ESP_LOGV(TAG, "MCU reported number %u is: %d", datapoint.id, datapoint.value_int);
this->publish_state(datapoint.value_int * this->traits.get_step());
this->publish_state(datapoint.value_int / multiply_by_);
} else if (datapoint.type == TuyaDatapointType::ENUM) {
ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum);
this->publish_state(datapoint.value_enum);
@ -22,7 +22,7 @@ void TuyaNumber::setup() {
void TuyaNumber::control(float value) {
ESP_LOGV(TAG, "Setting number %u: %f", this->number_id_, value);
if (this->type_ == TuyaDatapointType::INTEGER) {
int integer_value = lround(value / this->traits.get_step());
int integer_value = lround(value * multiply_by_);
this->parent_->set_integer_datapoint_value(this->number_id_, integer_value);
} else if (this->type_ == TuyaDatapointType::ENUM) {
this->parent_->set_enum_datapoint_value(this->number_id_, value);

View file

@ -12,6 +12,7 @@ class TuyaNumber : public number::Number, public Component {
void setup() override;
void dump_config() override;
void set_number_id(uint8_t number_id) { this->number_id_ = number_id; }
void set_write_multiply(float factor) { multiply_by_ = factor; }
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
@ -20,6 +21,7 @@ class TuyaNumber : public number::Number, public Component {
Tuya *parent_;
uint8_t number_id_{0};
float multiply_by_{1.0};
TuyaDatapointType type_{};
};