ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
modbus_binarysensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "modbus_controller.binary_sensor";
7
8void ModbusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Modbus Controller Binary Sensor", this); }
9
10void ModbusBinarySensor::parse_and_publish(std::span<const uint8_t> data) {
11 bool value;
12 // For coils/discrete inputs this is the bit index; for registers it is the byte offset.
13 const size_t offset = this->offset;
14
15 switch (this->register_type) {
19 break;
20 default:
22 break;
23 }
24 // Is there a lambda registered
25 // call it with the pre converted value and the raw data array
26 if (this->transform_func_.has_value()) {
27 // the lambda can parse the response itself
28 auto val = (*this->transform_func_)(this, value, data);
29 if (val.has_value()) {
30 ESP_LOGV(TAG, "Value overwritten by lambda");
31 value = val.value();
32 }
33 }
34 this->publish_state(value);
35}
36
37} // namespace esphome::modbus_controller
void publish_state(bool new_state)
Publish a new state to the front-end.
void parse_and_publish(std::span< const uint8_t > data) override
uint8_t offset
Position of this sensor's data within its range's response - a byte offset for registers,...
mopeka_std_values val[3]
T get_data(const uint8_t *data, size_t buffer_offset)
Extract data from modbus response buffer.
bool bit_from_packed(int bit, std::span< const uint8_t > data)
Extract coil data from modbus response buffer Responses for coil are packed into bytes .
const std::vector< uint8_t > & data