ESPHome  2024.10.2
modbus_number.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <vector>
8 
9 namespace esphome {
10 namespace modbus_controller {
11 
12 using value_to_data_t = std::function<float>(float);
13 
14 class ModbusNumber : public number::Number, public Component, public SensorItem {
15  public:
17  SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range) {
18  this->register_type = register_type;
19  this->start_address = start_address;
20  this->offset = offset;
21  this->bitmask = bitmask;
22  this->sensor_value_type = value_type;
23  this->register_count = register_count;
24  this->skip_updates = skip_updates;
25  this->force_new_range = force_new_range;
26  };
27 
28  void dump_config() override;
29  void parse_and_publish(const std::vector<uint8_t> &data) override;
30  float get_setup_priority() const override { return setup_priority::HARDWARE; }
31  void set_parent(ModbusController *parent) { this->parent_ = parent; }
32  void set_write_multiply(float factor) { multiply_by_ = factor; }
33 
34  using transform_func_t = std::function<optional<float>(ModbusNumber *, float, const std::vector<uint8_t> &)>;
35  using write_transform_func_t = std::function<optional<float>(ModbusNumber *, float, std::vector<uint16_t> &)>;
38  void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
39 
40  protected:
41  void control(float value) override;
45  float multiply_by_{1.0};
46  bool use_write_multiple_{false};
47 };
48 
49 } // namespace modbus_controller
50 } // namespace esphome
void set_parent(ModbusController *parent)
Definition: modbus_number.h:31
void set_write_template(write_transform_func_t &&f)
Definition: modbus_number.h:37
optional< write_transform_func_t > write_transform_func_
Definition: modbus_number.h:43
void set_template(transform_func_t &&f)
Definition: modbus_number.h:36
std::function< float >(float) value_to_data_t
Definition: modbus_number.h:12
std::function< optional< float >(ModbusNumber *, float, const std::vector< uint8_t > &)> transform_func_t
Definition: modbus_number.h:34
void parse_and_publish(const std::vector< uint8_t > &data) override
Base-class for all numbers.
Definition: number.h:39
ModbusNumber(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range)
Definition: modbus_number.h:16
void set_use_write_mutiple(bool use_write_multiple)
Definition: modbus_number.h:38
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
float get_setup_priority() const override
Definition: modbus_number.h:30
optional< transform_func_t > transform_func_
Definition: modbus_number.h:42
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::function< optional< float >(ModbusNumber *, float, std::vector< uint16_t > &)> write_transform_func_t
Definition: modbus_number.h:35
void control(float value) override