ESPHome
2026.1.5
Loading...
Searching...
No Matches
esphome
components
sml
text_sensor
sml_text_sensor.cpp
Go to the documentation of this file.
1
#include "
esphome/core/helpers.h
"
2
#include "
esphome/core/log.h
"
3
#include "
sml_text_sensor.h
"
4
#include "
../sml_parser.h
"
5
#include <cinttypes>
6
7
namespace
esphome
{
8
namespace
sml {
9
10
static
const
char
*
const
TAG =
"sml_text_sensor"
;
11
12
SmlTextSensor::SmlTextSensor
(std::string server_id, std::string obis_code,
SmlType
format)
13
:
SmlListener
(std::move(server_id), std::move(obis_code)), format_(format) {}
14
15
void
SmlTextSensor::publish_val
(
const
ObisInfo
&obis_info) {
16
uint8_t value_type;
17
if
(this->
format_
==
SML_UNDEFINED
) {
18
value_type = obis_info.
value_type
;
19
}
else
{
20
value_type = this->
format_
;
21
}
22
23
switch
(value_type) {
24
case
SML_HEX
: {
25
// Buffer for "0x" + up to 32 bytes as hex + null
26
char
buf[67];
27
// Max 32 bytes of data fit in buffer ((67-3)/2)
28
size_t
hex_bytes = std::min(obis_info.
value
.
size
(),
size_t
(32));
29
format_hex_prefixed_to
(buf, obis_info.
value
.
begin
(), hex_bytes);
30
publish_state
(buf, 2 + hex_bytes * 2);
31
break
;
32
}
33
case
SML_INT
: {
34
char
buf[21];
// Enough for int64_t (-9223372036854775808)
35
int
len
= snprintf(buf,
sizeof
(buf),
"%"
PRId64,
bytes_to_int
(obis_info.
value
));
36
publish_state
(buf,
static_cast<
size_t
>
(
len
));
37
break
;
38
}
39
case
SML_BOOL
:
40
publish_state
(
bytes_to_uint
(obis_info.
value
) ?
"True"
:
"False"
);
41
break
;
42
case
SML_UINT
: {
43
char
buf[21];
// Enough for uint64_t (18446744073709551615)
44
int
len
= snprintf(buf,
sizeof
(buf),
"%"
PRIu64,
bytes_to_uint
(obis_info.
value
));
45
publish_state
(buf,
static_cast<
size_t
>
(
len
));
46
break
;
47
}
48
case
SML_OCTET
: {
49
publish_state
(
reinterpret_cast<
const
char
*
>
(obis_info.
value
.
begin
()), obis_info.
value
.
size
());
50
break
;
51
}
52
}
53
}
54
55
void
SmlTextSensor::dump_config
() {
56
LOG_TEXT_SENSOR(
""
,
"SML"
,
this
);
57
if
(!this->
server_id
.empty()) {
58
ESP_LOGCONFIG(TAG,
" Server ID: %s"
, this->
server_id
.c_str());
59
}
60
ESP_LOGCONFIG(TAG,
" OBIS Code: %s"
, this->
obis_code
.c_str());
61
}
62
63
}
// namespace sml
64
}
// namespace esphome
esphome::sml::BytesView::begin
const uint8_t * begin() const noexcept
Definition
sml_parser.h:35
esphome::sml::BytesView::size
size_t size() const noexcept
Definition
sml_parser.h:23
esphome::sml::ObisInfo
Definition
sml_parser.h:51
esphome::sml::ObisInfo::value_type
uint16_t value_type
Definition
sml_parser.h:60
esphome::sml::ObisInfo::value
BytesView value
Definition
sml_parser.h:59
esphome::sml::SmlListener
Definition
sml.h:13
esphome::sml::SmlListener::server_id
std::string server_id
Definition
sml.h:15
esphome::sml::SmlListener::obis_code
std::string obis_code
Definition
sml.h:16
esphome::sml::SmlTextSensor::SmlTextSensor
SmlTextSensor(std::string server_id, std::string obis_code, SmlType format)
Definition
sml_text_sensor.cpp:12
esphome::sml::SmlTextSensor::publish_val
void publish_val(const ObisInfo &obis_info) override
Definition
sml_text_sensor.cpp:15
esphome::sml::SmlTextSensor::dump_config
void dump_config() override
Definition
sml_text_sensor.cpp:55
esphome::sml::SmlTextSensor::format_
SmlType format_
Definition
sml_text_sensor.h:17
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.cpp:28
helpers.h
log.h
esphome::sml::bytes_to_int
int64_t bytes_to_int(const BytesView &buffer)
Definition
sml_parser.cpp:120
esphome::sml::SmlType
SmlType
Definition
constants.h:8
esphome::sml::SML_UNDEFINED
@ SML_UNDEFINED
Definition
constants.h:15
esphome::sml::SML_OCTET
@ SML_OCTET
Definition
constants.h:9
esphome::sml::SML_BOOL
@ SML_BOOL
Definition
constants.h:10
esphome::sml::SML_HEX
@ SML_HEX
Definition
constants.h:14
esphome::sml::SML_UINT
@ SML_UINT
Definition
constants.h:12
esphome::sml::SML_INT
@ SML_INT
Definition
constants.h:11
esphome::sml::bytes_to_uint
uint64_t bytes_to_uint(const BytesView &buffer)
Definition
sml_parser.cpp:112
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
esphome::len
std::string size_t len
Definition
helpers.h:595
esphome::format_hex_prefixed_to
char * format_hex_prefixed_to(char(&buffer)[N], T val)
Format an unsigned integer as "0x" prefixed lowercase hex to buffer.
Definition
helpers.h:811
sml_parser.h
sml_text_sensor.h
Generated by
1.12.0