ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
modbus_number.cpp
Go to the documentation of this file.
1#include <vector>
2#include "modbus_number.h"
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "modbus.number";
9
10// Maximum uint16_t registers to log in verbose hex output
11static constexpr size_t MODBUS_NUMBER_MAX_LOG_REGISTERS = 32;
12
13void ModbusNumber::parse_and_publish(const std::vector<uint8_t> &data) {
14 float result = payload_to_float(data, *this) / this->multiply_by_;
15
16 // Is there a lambda registered
17 // call it with the pre converted value and the raw data array
18 if (this->transform_func_.has_value()) {
19 // the lambda can parse the response itself
20 auto val = (*this->transform_func_)(this, result, data);
21 if (val.has_value()) {
22 ESP_LOGV(TAG, "Value overwritten by lambda");
23 result = val.value();
24 }
25 }
26 ESP_LOGD(TAG, "Number new state : %.02f", result);
27 // this->sensor_->raw_state = result;
28 this->publish_state(result);
29}
30
31void ModbusNumber::control(float value) {
32 ModbusCommandItem write_cmd;
33 std::vector<uint16_t> data;
34 float write_value = value;
35 // Is there are lambda configured?
36 if (this->write_transform_func_.has_value()) {
37 // data is passed by reference
38 // the lambda can fill the empty vector directly
39 // in that case the return value is ignored
40 auto val = (*this->write_transform_func_)(this, value, data);
41 if (val.has_value()) {
42 ESP_LOGV(TAG, "Value overwritten by lambda");
43 write_value = val.value();
44 } else {
45 ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
46 return;
47 }
48 } else {
49 write_value = this->multiply_by_ * write_value;
50 }
51
52 if (!data.empty()) {
53#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
54 char hex_buf[format_hex_pretty_uint16_size(MODBUS_NUMBER_MAX_LOG_REGISTERS)];
55#endif
56 ESP_LOGV(TAG, "Modbus Number write raw: %s",
57 format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
59 this->parent_, data,
60 [this, write_cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
61 this->parent_->on_write_register_response(write_cmd.register_type, this->start_address, data);
62 });
63 } else {
65
66 ESP_LOGD(TAG,
67 "Updating register: connected Sensor=%s start address=0x%X register count=%d new value=%.02f (val=%.02f)",
68 this->get_name().c_str(), this->start_address, this->register_count, value, write_value);
69
70 // Create and send the write command
71 if (this->register_count == 1 && !this->use_write_multiple_) {
72 // since offset is in bytes and a register is 16 bits we get the start by adding offset/2
74 data[0]);
75 } else {
77 this->parent_, this->start_address + this->offset / 2, this->register_count, data);
78 }
79 // publish new value
80 write_cmd.on_data_func = [this, write_cmd, value](ModbusRegisterType register_type, uint16_t start_address,
81 const std::vector<uint8_t> &data) {
82 // gets called when the write command is ack'd from the device
84 this->publish_state(value);
85 };
86 }
87 this->parent_->queue_command(write_cmd);
88 this->publish_state(value);
89}
90void ModbusNumber::dump_config() { LOG_NUMBER(TAG, "Modbus Number", this); }
91
92} // namespace esphome::modbus_controller
const StringRef & get_name() const
Definition entity_base.h:71
static ModbusCommandItem create_custom_command(ModbusController *modbusdevice, const std::vector< uint8_t > &values, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler=nullptr)
Create custom modbus command.
static ModbusCommandItem create_write_single_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t value)
Create modbus write multiple registers command Function 16 (10hex) Write Multiple Registers.
static ModbusCommandItem create_write_multiple_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t register_count, const std::vector< uint16_t > &values)
Create modbus read command Function code 02-04.
std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> on_data_func
void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)
default delegate called by process_modbus_data when a response for a write response has retrieved fro...
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
optional< write_transform_func_t > write_transform_func_
optional< transform_func_t > transform_func_
void parse_and_publish(const std::vector< uint8_t > &data) override
void publish_state(float state)
Definition number.cpp:22
mopeka_std_values val[3]
std::vector< uint16_t > float_to_payload(float value, SensorValueType value_type)
float payload_to_float(const std::vector< uint8_t > &data, const SensorItem &item)
Convert vector<uint8_t> response payload to float.
const std::vector< uint8_t > & data
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:340
constexpr size_t format_hex_pretty_uint16_size(size_t count)
Calculate buffer size needed for format_hex_pretty_to with uint16_t data: "XXXX:XXXX:....
Definition helpers.h:1421