Fix published state for modbus number (#2894)

This commit is contained in:
Jesse Hills 2021-12-10 09:32:34 +13:00
parent e5c9e87fad
commit 649366ff44
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

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,