ESPHome 2026.8.1
Loading...
Searching...
No Matches
modbus_select.cpp
Go to the documentation of this file.
1#include "modbus_select.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "modbus_controller.select";
7
8void ModbusSelect::dump_config() { LOG_SELECT(TAG, "Modbus Controller Select", this); }
9
10void ModbusSelect::parse_and_publish(std::span<const uint8_t> data) {
11 int64_t value =
13
14 ESP_LOGD(TAG, "New select value %lld from payload", value);
15
16 optional<std::string> new_state;
17
18 if (this->transform_func_.has_value()) {
19 auto val = (*this->transform_func_)(this, value, data);
20 if (val.has_value()) {
21 new_state = *val;
22 ESP_LOGV(TAG, "lambda returned option %s", new_state->c_str());
23 }
24 }
25
26 if (!new_state.has_value()) {
27 auto map_it = std::find(this->mapping_.cbegin(), this->mapping_.cend(), value);
28
29 if (map_it != this->mapping_.cend()) {
30 size_t idx = std::distance(this->mapping_.cbegin(), map_it);
31 ESP_LOGV(TAG, "Found option %s for value %lld", this->option_at(idx), value);
32 this->publish_state(idx);
33 return;
34 } else {
35 ESP_LOGE(TAG, "No option found for mapping %lld", value);
36 }
37 }
38
39 if (new_state.has_value()) {
40 this->publish_state(new_state.value());
41 }
42}
43
44void ModbusSelect::control(size_t index) {
45 optional<int64_t> mapval = this->mapping_[index];
46 const char *option = this->option_at(index);
47 ESP_LOGD(TAG, "Found value %lld for option '%s'", *mapval, option);
48
49 std::vector<uint16_t> data;
50
51 if (this->write_transform_func_.has_value()) {
52 // Transform func requires string parameter for backward compatibility
53 auto val = (*this->write_transform_func_)(this, std::string(option), *mapval, data);
54 if (val.has_value()) {
55 mapval = val;
56 ESP_LOGV(TAG, "write_lambda returned mapping value %lld", *mapval);
57 } else {
58 ESP_LOGD(TAG, "Communication handled by write_lambda - exiting control");
59 return;
60 }
61 }
62
63 if (data.empty()) {
65 } else {
66 ESP_LOGV(TAG, "Using payload from write lambda");
67 }
68
69 if (data.empty()) {
70 ESP_LOGW(TAG, "No payload was created for updating select");
71 return;
72 }
73
74 // The command declares register_count registers, so the payload must be exactly that many words:
75 // a value type narrower than the declared width is zero-padded (the config deliberately allows
76 // register_count larger than the value type). Anything else would put a byte count on the wire
77 // that disagrees with the quantity field, which conformant devices reject.
78 // register_count declares the READ range width - it may pull neighboring registers into one poll -
79 // so a write covers exactly the registers the value occupies: the quantity comes from the payload,
80 // never from register_count (padding to it would zero registers the user only declared for reading).
81 // A payload wider than the declared range means the config and the lambda disagree - drop it.
82 if (data.size() > this->register_count) {
83 ESP_LOGE(TAG, "Payload has %zu registers but register_count is %u; dropping write", data.size(),
84 this->register_count);
85 return;
86 }
87
88 const uint16_t write_address = this->write_address();
89 optional<ModbusCommandItem> write_cmd;
90 if ((this->register_count == 1) && (!this->use_write_multiple_)) {
91 write_cmd.emplace(ModbusCommandItem::create_write_single_command(this->parent_, write_address, data[0]));
92 } else {
93 write_cmd.emplace(
95 }
96
97 this->parent_->queue_command(std::move(*write_cmd));
98
99 if (this->optimistic_)
100 this->publish_state(index);
101}
102
103} // namespace esphome::modbus_controller
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.
void queue_command(ModbusCommandItem command)
Queues a one-shot modbus command (writes, custom commands); taken by value, so std::move to avoid a c...
optional< write_transform_func_t > write_transform_func_
void control(size_t index) override
void parse_and_publish(std::span< const uint8_t > data) override
optional< transform_func_t > transform_func_
uint8_t offset
Position of this sensor's data within its range's response - a byte offset for registers,...
uint16_t write_address() const
Address a write entity (switch/number/select) targets, derived from its resolved position within the ...
const char * option_at(size_t index) const
Return the option value at the provided index offset (as const char* from flash).
Definition select.cpp:77
void publish_state(const std::string &state)
Definition select.cpp:11
mopeka_std_values val[3]
void number_to_payload(Container &data, int64_t value, SensorValueType value_type)
Append the Modbus register words for value to data.
std::optional< int64_t > payload_to_number(const uint8_t *data, size_t size, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
Convert a raw response payload to a number.
const std::vector< uint8_t > & data