ESPHome  2024.11.1
modbus_sensor.cpp
Go to the documentation of this file.
1 
2 #include "modbus_sensor.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace modbus_controller {
7 
8 static const char *const TAG = "modbus_controller.sensor";
9 
10 void ModbusSensor::dump_config() { LOG_SENSOR(TAG, "Modbus Controller Sensor", this); }
11 
12 void ModbusSensor::parse_and_publish(const std::vector<uint8_t> &data) {
13  float result = payload_to_float(data, *this);
14 
15  // Is there a lambda registered
16  // call it with the pre converted value and the raw data array
17  if (this->transform_func_.has_value()) {
18  // the lambda can parse the response itself
19  auto val = (*this->transform_func_)(this, result, data);
20  if (val.has_value()) {
21  ESP_LOGV(TAG, "Value overwritten by lambda");
22  result = val.value();
23  }
24  }
25  ESP_LOGD(TAG, "Sensor new state: %.02f", result);
26  // this->sensor_->raw_state = result;
27  this->publish_state(result);
28 }
29 
30 } // namespace modbus_controller
31 } // namespace esphome
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
optional< transform_func_t > transform_func_
Definition: modbus_sensor.h:33
float payload_to_float(const std::vector< uint8_t > &data, const SensorItem &item)
Convert vector<uint8_t> response payload to float.
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void parse_and_publish(const std::vector< uint8_t > &data) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7