ESPHome 2026.1.5
Loading...
Searching...
No Matches
modbus_switch.cpp
Go to the documentation of this file.
1
2#include "modbus_switch.h"
4#include "esphome/core/log.h"
5namespace esphome {
6namespace modbus_controller {
7
8static const char *const TAG = "modbus_controller.switch";
9
10// Maximum bytes to log in verbose hex output
11static constexpr size_t MODBUS_SWITCH_MAX_LOG_BYTES = 64;
12
14 optional<bool> initial_state = Switch::get_initial_state_with_restore_mode();
15 if (initial_state.has_value()) {
16 // if it has a value, restore_mode is not "DISABLED", therefore act on the switch:
17 if (initial_state.value()) {
18 this->turn_on();
19 } else {
20 this->turn_off();
21 }
22 }
23}
24void ModbusSwitch::dump_config() { LOG_SWITCH(TAG, "Modbus Controller Switch", this); }
25
27
29
30void ModbusSwitch::parse_and_publish(const std::vector<uint8_t> &data) {
31 bool value = false;
32 switch (this->register_type) {
35 // offset for coil is the actual number of the coil not the byte offset
36 value = coil_from_vector(this->offset, data);
37 break;
38 default:
39 value = get_data<uint16_t>(data, this->offset) & this->bitmask;
40 break;
41 }
42
43 // Is there a lambda registered
44 // call it with the pre converted value and the raw data array
45 if (this->publish_transform_func_) {
46 // the lambda can parse the response itself
47 auto val = (*this->publish_transform_func_)(this, value, data);
48 if (val.has_value()) {
49 ESP_LOGV(TAG, "Value overwritten by lambda");
50 value = val.value();
51 }
52 }
53
54 ESP_LOGV(TAG, "Publish '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
55 ONOFF(value), (int) this->register_type, this->start_address, this->offset);
56 this->publish_state(value);
57}
58
60 // This will be called every time the user requests a state change.
62 std::vector<uint8_t> data;
63 // Is there are lambda configured?
64 if (this->write_transform_func_.has_value()) {
65 // data is passed by reference
66 // the lambda can fill the empty vector directly
67 // in that case the return value is ignored
68 auto val = (*this->write_transform_func_)(this, state, data);
69 if (val.has_value()) {
70 ESP_LOGV(TAG, "Value overwritten by lambda");
71 state = val.value();
72 } else {
73 ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
74 return;
75 }
76 }
77 if (!data.empty()) {
78#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
79 char hex_buf[format_hex_pretty_size(MODBUS_SWITCH_MAX_LOG_BYTES)];
80#endif
81 ESP_LOGV(TAG, "Modbus Switch write raw: %s",
82 format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
84 this->parent_, data,
85 [this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
86 this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
87 });
88 } else {
89 ESP_LOGV(TAG, "write_state '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
90 ONOFF(state), (int) this->register_type, this->start_address, this->offset);
91 if (this->register_type == ModbusRegisterType::COIL) {
92 // offset for coil and discrete inputs is the coil/register number not bytes
93 if (this->use_write_multiple_) {
94 std::vector<bool> states{state};
96 } else {
98 }
99 } else {
100 // since offset is in bytes and a register is 16 bits we get the start by adding offset/2
101 if (this->use_write_multiple_) {
102 std::vector<uint16_t> bool_states(1, state ? (0xFFFF & this->bitmask) : 0);
104 bool_states);
105 } else {
107 state ? 0xFFFF & this->bitmask : 0u);
108 }
109 }
110 }
111 this->parent_->queue_command(cmd);
112 this->publish_state(state);
113}
114// ModbusSwitch end
115} // namespace modbus_controller
116} // namespace esphome
const StringRef & get_name() const
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.
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_single_coil(ModbusController *modbusdevice, uint16_t address, bool value)
Create modbus write single registers command Function 05 (05hex) Write Single Coil.
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.
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.
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...
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
optional< write_transform_func_t > write_transform_func_
void set_assumed_state(bool assumed_state)
void parse_and_publish(const std::vector< uint8_t > &data) override
optional< transform_func_t > publish_transform_func_
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
void turn_on()
Turn this switch on.
Definition switch.cpp:21
void turn_off()
Turn this switch off.
Definition switch.cpp:25
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:57
bool state
Definition fan.h:0
mopeka_std_values val[4]
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 .
T get_data(const std::vector< uint8_t > &data, size_t buffer_offset)
Extract data from modbus response buffer.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:334
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:830