ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
modbus_select.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4#include <vector>
5
9
11
12class ModbusSelect final : public Component, public select::Select, public SensorItem, public WriterEntity {
13 public:
15 std::vector<int64_t> mapping) {
16 this->register_type = modbus::EntityType::HOLDING; // not configurable
17 this->sensor_value_type = sensor_value_type;
18 this->set_address(start_address);
19 this->set_offset_from_start_address(0); // not configurable
20 this->bitmask = 0xFFFFFFFF; // not configurable
21 this->response_bytes = 0; // not configurable
22 this->reuse_previous_range = reuse_previous_range;
23 this->mapping_ = std::move(mapping);
24 }
25
26 using transform_func_t = optional<std::string> (*)(ModbusSelect *const, int64_t, std::span<const uint8_t>);
27 using write_transform_func_t = optional<int64_t> (*)(ModbusSelect *const, const std::string &, int64_t,
29
30 void set_parent(ModbusController *const parent) { this->set_controller_(parent); }
31 void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
32 void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
35
36 void dump_config() override;
37 void parse_and_publish(std::span<const uint8_t> data) override;
38 void control(size_t index) override;
39
40 protected:
41 std::vector<int64_t> mapping_{};
43 bool optimistic_{false};
44 optional<transform_func_t> transform_func_{nullopt};
45 optional<write_transform_func_t> write_transform_func_{nullopt};
46};
47
48} // namespace esphome::modbus_controller
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:227
optional< write_transform_func_t > write_transform_func_
ModbusSelect(SensorValueType sensor_value_type, uint16_t start_address, RangeReuse reuse_previous_range, std::vector< int64_t > mapping)
void control(size_t index) override
void set_use_write_mutiple(bool use_write_multiple)
optional< int64_t >(*)(ModbusSelect *const, const std::string &, int64_t, modbus::RegisterValues &) write_transform_func_t
optional< std::string >(*)(ModbusSelect *const, int64_t, std::span< const uint8_t >) transform_func_t
void set_template(transform_func_t f)
void parse_and_publish(std::span< const uint8_t > data) override
optional< transform_func_t > transform_func_
void set_write_template(write_transform_func_t f)
void set_parent(ModbusController *const parent)
void set_address(uint16_t address)
Sets the configured address, and points the range base at it.
void set_offset_from_start_address(uint8_t offset)
Records the offset as configured, and seeds the resolved position with it.
Gives a writer entity the write API of the WriterDevice it owns.
void set_controller_(ModbusController *controller)
Base-class for all selects.
Definition select.h:29
const std::vector< uint8_t > & data
RangeReuse
How an item relates to the register range built just before it (same register type,...