ESPHome 2025.5.0
Loading...
Searching...
No Matches
modbus_binarysensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace modbus_controller {
6
7static const char *const TAG = "modbus_controller.binary_sensor";
8
9void ModbusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Modbus Controller Binary Sensor", this); }
10
11void ModbusBinarySensor::parse_and_publish(const std::vector<uint8_t> &data) {
12 bool value;
13
14 switch (this->register_type) {
17 // offset for coil is the actual number of the coil not the byte offset
18 value = coil_from_vector(this->offset, data);
19 break;
20 default:
21 value = get_data<uint16_t>(data, this->offset) & this->bitmask;
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 modbus_controller
38} // namespace esphome
void publish_state(bool state)
Publish a new state to the front-end.
void parse_and_publish(const std::vector< uint8_t > &data) override
bool has_value() const
Definition optional.h:87
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