ESPHome  2024.3.1
modbus_binarysensor.cpp
Go to the documentation of this file.
1 #include "modbus_binarysensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace modbus_controller {
6 
7 static const char *const TAG = "modbus_controller.binary_sensor";
8 
9 void ModbusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Modbus Controller Binary Sensor", this); }
10 
11 void 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
mopeka_std_values val[4]
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 ...
void publish_state(bool state)
Publish a new state to the front-end.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void parse_and_publish(const std::vector< uint8_t > &data) override