ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
modbus_sensor.cpp
Go to the documentation of this file.
1
2#include "modbus_sensor.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace modbus_controller {
7
8static const char *const TAG = "modbus_controller.sensor";
9
10void ModbusSensor::dump_config() { LOG_SENSOR(TAG, "Modbus Controller Sensor", this); }
11
12void 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
void parse_and_publish(const std::vector< uint8_t > &data) override
optional< transform_func_t > transform_func_
bool has_value() const
Definition optional.h:87
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
mopeka_std_values val[4]
float payload_to_float(const std::vector< uint8_t > &data, const SensorItem &item)
Convert vector<uint8_t> response payload to float.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7