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