ESPHome 2026.6.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(const std::vector<uint8_t> &data) {
11 bool value;
12
13 switch (this->register_type) {
16 // offset for coil is the actual number of the coil not the byte offset
17 value = modbus::helpers::coil_from_vector(this->offset, data);
18 break;
19 default:
21 break;
22 }
23 // Is there a lambda registered
24 // call it with the pre converted value and the raw data array
25 if (this->transform_func_.has_value()) {
26 // the lambda can parse the response itself
27 auto val = (*this->transform_func_)(this, value, data);
28 if (val.has_value()) {
29 ESP_LOGV(TAG, "Value overwritten by lambda");
30 value = val.value();
31 }
32 }
33 this->publish_state(value);
34}
35
36} // namespace esphome::modbus_controller
void publish_state(bool new_state)
Publish a new state to the front-end.
void parse_and_publish(const std::vector< uint8_t > &data) override
mopeka_std_values val[3]
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.
const std::vector< uint8_t > & data