ESPHome  2024.11.0
modbus_switch.cpp
Go to the documentation of this file.
1 
2 #include "modbus_switch.h"
3 #include "esphome/core/log.h"
4 namespace esphome {
5 namespace modbus_controller {
6 
7 static const char *const TAG = "modbus_controller.switch";
8 
10  optional<bool> initial_state = Switch::get_initial_state_with_restore_mode();
11  if (initial_state.has_value()) {
12  // if it has a value, restore_mode is not "DISABLED", therefore act on the switch:
13  if (initial_state.value()) {
14  this->turn_on();
15  } else {
16  this->turn_off();
17  }
18  }
19 }
20 void ModbusSwitch::dump_config() { LOG_SWITCH(TAG, "Modbus Controller Switch", this); }
21 
22 void ModbusSwitch::parse_and_publish(const std::vector<uint8_t> &data) {
23  bool value = false;
24  switch (this->register_type) {
27  // offset for coil is the actual number of the coil not the byte offset
28  value = coil_from_vector(this->offset, data);
29  break;
30  default:
31  value = get_data<uint16_t>(data, this->offset) & this->bitmask;
32  break;
33  }
34 
35  // Is there a lambda registered
36  // call it with the pre converted value and the raw data array
37  if (this->publish_transform_func_) {
38  // the lambda can parse the response itself
39  auto val = (*this->publish_transform_func_)(this, value, data);
40  if (val.has_value()) {
41  ESP_LOGV(TAG, "Value overwritten by lambda");
42  value = val.value();
43  }
44  }
45 
46  ESP_LOGV(TAG, "Publish '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
47  ONOFF(value), (int) this->register_type, this->start_address, this->offset);
48  this->publish_state(value);
49 }
50 
52  // This will be called every time the user requests a state change.
54  std::vector<uint8_t> data;
55  // Is there are lambda configured?
56  if (this->write_transform_func_.has_value()) {
57  // data is passed by reference
58  // the lambda can fill the empty vector directly
59  // in that case the return value is ignored
60  auto val = (*this->write_transform_func_)(this, state, data);
61  if (val.has_value()) {
62  ESP_LOGV(TAG, "Value overwritten by lambda");
63  state = val.value();
64  } else {
65  ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
66  return;
67  }
68  }
69  if (!data.empty()) {
70  ESP_LOGV(TAG, "Modbus Switch write raw: %s", format_hex_pretty(data).c_str());
72  this->parent_, data,
73  [this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
74  this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
75  });
76  } else {
77  ESP_LOGV(TAG, "write_state '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
78  ONOFF(state), (int) this->register_type, this->start_address, this->offset);
79  if (this->register_type == ModbusRegisterType::COIL) {
80  // offset for coil and discrete inputs is the coil/register number not bytes
81  if (this->use_write_multiple_) {
82  std::vector<bool> states{state};
84  } else {
86  }
87  } else {
88  // since offset is in bytes and a register is 16 bits we get the start by adding offset/2
89  if (this->use_write_multiple_) {
90  std::vector<uint16_t> bool_states(1, state ? (0xFFFF & this->bitmask) : 0);
92  bool_states);
93  } else {
95  state ? 0xFFFF & this->bitmask : 0u);
96  }
97  }
98  }
99  this->parent_->queue_command(cmd);
100  publish_state(state);
101 }
102 // ModbusSwitch end
103 } // namespace modbus_controller
104 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:364
optional< write_transform_func_t > write_transform_func_
Definition: modbus_switch.h:46
void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)
default delegate called by process_modbus_data when a response for a write response has retrieved fro...
static ModbusCommandItem create_write_single_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t value)
Create modbus write multiple registers command Function 16 (10hex) Write Multiple Registers...
mopeka_std_values val[4]
optional< transform_func_t > publish_transform_func_
Definition: modbus_switch.h:45
bool has_value() const
Definition: optional.h:87
bool coil_from_vector(int coil, const std::vector< uint8_t > &data)
Extract coil data from modbus response buffer Responses for coil are packed into bytes ...
static ModbusCommandItem create_write_single_coil(ModbusController *modbusdevice, uint16_t address, bool value)
Create modbus write single registers command Function 05 (05hex) Write Single Coil.
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
void parse_and_publish(const std::vector< uint8_t > &data) override
static ModbusCommandItem create_write_multiple_coils(ModbusController *modbusdevice, uint16_t start_address, const std::vector< bool > &values)
Create modbus write multiple registers command Function 15 (0Fhex) Write Multiple Coils...
static ModbusCommandItem create_write_multiple_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t register_count, const std::vector< uint16_t > &values)
Create modbus read command Function code 02-04.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
static ModbusCommandItem create_custom_command(ModbusController *modbusdevice, const std::vector< uint8_t > &values, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler=nullptr)
Create custom modbus command.
const StringRef & get_name() const
Definition: entity_base.cpp:10
stm32_cmd_t * cmd
Definition: stm32flash.h:96
void write_state(bool state) override
void turn_off()
Turn this switch off.
Definition: switch.cpp:15