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