ESPHome  2024.12.2
modbus_select.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 
9 
10 namespace esphome {
11 namespace modbus_controller {
12 
13 class ModbusSelect : public Component, public select::Select, public SensorItem {
14  public:
16  bool force_new_range, std::vector<int64_t> mapping) {
17  this->register_type = ModbusRegisterType::HOLDING; // not configurable
18  this->sensor_value_type = sensor_value_type;
19  this->start_address = start_address;
20  this->offset = 0; // not configurable
21  this->bitmask = 0xFFFFFFFF; // not configurable
22  this->register_count = register_count;
23  this->response_bytes = 0; // not configurable
24  this->skip_updates = skip_updates;
25  this->force_new_range = force_new_range;
26  this->mapping_ = std::move(mapping);
27  }
28 
29  using transform_func_t =
30  std::function<optional<std::string>(ModbusSelect *const, int64_t, const std::vector<uint8_t> &)>;
32  std::function<optional<int64_t>(ModbusSelect *const, const std::string &, int64_t, std::vector<uint16_t> &)>;
33 
34  void set_parent(ModbusController *const parent) { this->parent_ = parent; }
35  void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
36  void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
38  void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
39 
40  void dump_config() override;
41  void parse_and_publish(const std::vector<uint8_t> &data) override;
42  void control(const std::string &value) override;
43 
44  protected:
45  std::vector<int64_t> mapping_{};
47  bool use_write_multiple_{false};
48  bool optimistic_{false};
51 };
52 
53 } // namespace modbus_controller
54 } // namespace esphome
void set_write_template(write_transform_func_t &&f)
Definition: modbus_select.h:38
optional< transform_func_t > transform_func_
Definition: modbus_select.h:49
const nullopt_t nullopt((nullopt_t::init()))
std::function< optional< std::string >(ModbusSelect *const, int64_t, const std::vector< uint8_t > &)> transform_func_t
Definition: modbus_select.h:30
void set_template(transform_func_t &&f)
Definition: modbus_select.h:37
void control(const std::string &value) override
void set_parent(ModbusController *const parent)
Definition: modbus_select.h:34
optional< write_transform_func_t > write_transform_func_
Definition: modbus_select.h:50
void parse_and_publish(const std::vector< uint8_t > &data) override
ModbusSelect(SensorValueType sensor_value_type, uint16_t start_address, uint8_t register_count, uint16_t skip_updates, bool force_new_range, std::vector< int64_t > mapping)
Definition: modbus_select.h:15
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::function< optional< int64_t >(ModbusSelect *const, const std::string &, int64_t, std::vector< uint16_t > &)> write_transform_func_t
Definition: modbus_select.h:32
void set_use_write_mutiple(bool use_write_multiple)
Definition: modbus_select.h:35