Modbus controller (#1779)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
1 parent
4d28afc153
commit
7672ba2c8d
27 files changed
+2505
-34
No files matched your search
@@ -0,0 +1,36 @@
|
||||
|
||||
#include "modbus_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace modbus_controller {
|
||||
|
||||
static const char *const TAG = "modbus_controller.sensor";
|
||||
|
||||
void ModbusSensor::dump_config() { LOG_SENSOR(TAG, "Modbus Controller Sensor", this); }
|
||||
|
||||
void ModbusSensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
union {
|
||||
float float_value;
|
||||
uint32_t raw;
|
||||
} raw_to_float;
|
||||
|
||||
float result = payload_to_float(data, *this);
|
||||
|
||||
// Is there a lambda registered
|
||||
// call it with the pre converted value and the raw data array
|
||||
if (this->transform_func_.has_value()) {
|
||||
// the lambda can parse the response itself
|
||||
auto val = (*this->transform_func_)(this, result, data);
|
||||
if (val.has_value()) {
|
||||
ESP_LOGV(TAG, "Value overwritten by lambda");
|
||||
result = val.value();
|
||||
}
|
||||
}
|
||||
ESP_LOGD(TAG, "Sensor new state: %.02f", result);
|
||||
// this->sensor_->raw_state = result;
|
||||
this->publish_state(result);
|
||||
}
|
||||
|
||||
} // namespace modbus_controller
|
||||
} // namespace esphome
|
||||
Reference in new issue
Block a user