Fix published state for modbus number (#2894)

This commit is contained in:
Jesse Hills 2021-12-10 09:32:34 +13:00 committed by GitHub
parent 6df1d5222d
commit 24ec5a6e9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,6 +27,7 @@ void ModbusNumber::parse_and_publish(const std::vector<uint8_t> &data) {
void ModbusNumber::control(float value) { void ModbusNumber::control(float value) {
std::vector<uint16_t> data; std::vector<uint16_t> data;
float write_value = value;
// Is there are lambda configured? // Is there are lambda configured?
if (this->write_transform_func_.has_value()) { if (this->write_transform_func_.has_value()) {
// data is passed by reference // data is passed by reference
@ -35,23 +36,23 @@ void ModbusNumber::control(float value) {
auto val = (*this->write_transform_func_)(this, value, data); auto val = (*this->write_transform_func_)(this, value, data);
if (val.has_value()) { if (val.has_value()) {
ESP_LOGV(TAG, "Value overwritten by lambda"); ESP_LOGV(TAG, "Value overwritten by lambda");
value = val.value(); write_value = val.value();
} else { } else {
ESP_LOGV(TAG, "Communication handled by lambda - exiting control"); ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
return; return;
} }
} else { } else {
value = multiply_by_ * value; write_value = multiply_by_ * write_value;
} }
// lambda didn't set payload // lambda didn't set payload
if (data.empty()) { if (data.empty()) {
data = float_to_payload(value, this->sensor_value_type); data = float_to_payload(write_value, this->sensor_value_type);
} }
ESP_LOGD(TAG, ESP_LOGD(TAG,
"Updating register: connected Sensor=%s start address=0x%X register count=%d new value=%.02f (val=%.02f)", "Updating register: connected Sensor=%s start address=0x%X register count=%d new value=%.02f (val=%.02f)",
this->get_name().c_str(), this->start_address, this->register_count, value, value); this->get_name().c_str(), this->start_address, this->register_count, write_value, write_value);
// Create and send the write command // Create and send the write command
auto write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset, auto write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset,