ESPHome
2026.6.0-dev
Loading...
Searching...
No Matches
esphome
components
modbus_controller
text_sensor
modbus_textsensor.cpp
Go to the documentation of this file.
1
2
#include "
modbus_textsensor.h
"
3
#include "
esphome/core/log.h
"
4
5
namespace
esphome::modbus_controller
{
6
7
static
const
char
*
const
TAG =
"modbus_controller.text_sensor"
;
8
9
void
ModbusTextSensor::dump_config
() { LOG_TEXT_SENSOR(
""
,
"Modbus Controller Text Sensor"
,
this
); }
10
11
void
ModbusTextSensor::parse_and_publish
(
const
std::vector<uint8_t> &
data
) {
12
std::string output_str{};
13
uint8_t items_left = this->
response_bytes
;
14
uint8_t index = this->
offset
;
15
while
((items_left > 0) && index <
data
.size()) {
16
uint8_t b =
data
[index];
17
switch
(this->
encode_
) {
18
case
RawEncoding::HEXBYTES
: {
19
// max 3: 2 hex digits + null
20
char
hex_buf[3];
21
snprintf(hex_buf,
sizeof
(hex_buf),
"%02x"
, b);
22
output_str += hex_buf;
23
break
;
24
}
25
case
RawEncoding::COMMA
: {
26
// max 5: optional ','(1) + uint8(3) + null, for both ",%d" and "%d"
27
char
dec_buf[5];
28
snprintf(dec_buf,
sizeof
(dec_buf), index != this->
offset
?
",%d"
:
"%d"
, b);
29
output_str += dec_buf;
30
break
;
31
}
32
case
RawEncoding::ANSI
:
33
if
(b < 0x20)
34
break
;
35
// FALLTHROUGH
36
// Anything else no encoding
37
default
:
38
output_str += (char) b;
39
break
;
40
}
41
items_left--;
42
index++;
43
}
44
45
// Is there a lambda registered
46
// call it with the pre converted value and the raw data array
47
if
(this->
transform_func_
.has_value()) {
48
// the lambda can parse the response itself
49
auto
val
= (*this->
transform_func_
)(
this
, output_str,
data
);
50
if
(
val
.has_value()) {
51
ESP_LOGV(TAG,
"Value overwritten by lambda"
);
52
output_str =
val
.value();
53
}
54
}
55
this->
publish_state
(output_str);
56
}
57
58
}
// namespace esphome::modbus_controller
esphome::modbus_controller::ModbusTextSensor::parse_and_publish
void parse_and_publish(const std::vector< uint8_t > &data) override
Definition
modbus_textsensor.cpp:11
esphome::modbus_controller::ModbusTextSensor::transform_func_
optional< transform_func_t > transform_func_
Definition
modbus_textsensor.h:36
esphome::modbus_controller::ModbusTextSensor::dump_config
void dump_config() override
Definition
modbus_textsensor.cpp:9
esphome::modbus_controller::ModbusTextSensor::encode_
RawEncoding encode_
Definition
modbus_textsensor.h:38
esphome::modbus_controller::SensorItem::response_bytes
uint8_t response_bytes
Definition
modbus_controller.h:116
esphome::modbus_controller::SensorItem::offset
uint8_t offset
Definition
modbus_controller.h:114
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.cpp:21
log.h
modbus_textsensor.h
val
mopeka_std_values val[3]
Definition
mopeka_std_check.h:8
esphome::modbus_controller
Definition
modbus_binarysensor.cpp:4
esphome::modbus_controller::data
const std::vector< uint8_t > & data
Definition
modbus_controller.h:68
esphome::modbus_controller::RawEncoding::COMMA
@ COMMA
esphome::modbus_controller::RawEncoding::HEXBYTES
@ HEXBYTES
esphome::modbus_controller::RawEncoding::ANSI
@ ANSI
Generated by
1.12.0