ESPHome  2024.11.1
modbus_switch.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 class ModbusSwitch : public Component, public switch_::Switch, public SensorItem {
13  public:
15  uint16_t skip_updates, bool force_new_range) {
16  this->register_type = register_type;
17  this->start_address = start_address;
18  this->offset = offset;
19  this->bitmask = bitmask;
21  this->skip_updates = skip_updates;
22  this->register_count = 1;
23  if (register_type == ModbusRegisterType::HOLDING || register_type == ModbusRegisterType::COIL) {
24  this->start_address += offset;
25  this->offset = 0;
26  }
27  this->force_new_range = force_new_range;
28  };
29  void setup() override;
30  void write_state(bool state) override;
31  void dump_config() override;
32  void set_state(bool state) { this->state = state; }
33  void parse_and_publish(const std::vector<uint8_t> &data) override;
34  void set_parent(ModbusController *parent) { this->parent_ = parent; }
35 
36  using transform_func_t = std::function<optional<bool>(ModbusSwitch *, bool, const std::vector<uint8_t> &)>;
37  using write_transform_func_t = std::function<optional<bool>(ModbusSwitch *, bool, std::vector<uint8_t> &)>;
40  void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
41 
42  protected:
47 };
48 
49 } // namespace modbus_controller
50 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
void set_template(transform_func_t &&f)
Definition: modbus_switch.h:38
optional< write_transform_func_t > write_transform_func_
Definition: modbus_switch.h:46
optional< transform_func_t > publish_transform_func_
Definition: modbus_switch.h:45
void set_parent(ModbusController *parent)
Definition: modbus_switch.h:34
std::function< optional< bool >(ModbusSwitch *, bool, const std::vector< uint8_t > &)> transform_func_t
Definition: modbus_switch.h:36
const nullopt_t nullopt((nullopt_t::init()))
ModbusSwitch(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, uint16_t skip_updates, bool force_new_range)
Definition: modbus_switch.h:14
void parse_and_publish(const std::vector< uint8_t > &data) override
std::function< optional< bool >(ModbusSwitch *, bool, std::vector< uint8_t > &)> write_transform_func_t
Definition: modbus_switch.h:37
void set_write_template(write_transform_func_t &&f)
Definition: modbus_switch.h:39
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_use_write_mutiple(bool use_write_multiple)
Definition: modbus_switch.h:40
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
void write_state(bool state) override