ESPHome
2024.10.2
esphome
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
6
namespace
esphome
{
7
namespace
sml {
8
9
static
const
char
*
const
TAG =
"sml_text_sensor"
;
10
11
SmlTextSensor::SmlTextSensor
(std::string server_id, std::string obis_code,
SmlType
format)
12
:
SmlListener
(
std
::move(server_id),
std
::move(obis_code)), format_(format) {}
13
14
void
SmlTextSensor::publish_val
(
const
ObisInfo
&obis_info) {
15
uint8_t value_type;
16
if
(this->
format_
==
SML_UNDEFINED
) {
17
value_type = obis_info.
value_type
;
18
}
else
{
19
value_type = this->
format_
;
20
}
21
22
switch
(value_type) {
23
case
SML_HEX
: {
24
publish_state
(
"0x"
+
bytes_repr
(obis_info.
value
));
25
break
;
26
}
27
case
SML_INT
: {
28
publish_state
(
to_string
(
bytes_to_int
(obis_info.
value
)));
29
break
;
30
}
31
case
SML_BOOL
:
32
publish_state
(
bytes_to_uint
(obis_info.
value
) ?
"True"
:
"False"
);
33
break
;
34
case
SML_UINT
: {
35
publish_state
(
to_string
(
bytes_to_uint
(obis_info.
value
)));
36
break
;
37
}
38
case
SML_OCTET
: {
39
publish_state
(std::string(obis_info.
value
.begin(), obis_info.
value
.end()));
40
break
;
41
}
42
}
43
}
44
45
void
SmlTextSensor::dump_config
() {
46
LOG_TEXT_SENSOR(
""
,
"SML"
,
this
);
47
if
(!this->
server_id
.empty()) {
48
ESP_LOGCONFIG(TAG,
" Server ID: %s"
, this->
server_id
.c_str());
49
}
50
ESP_LOGCONFIG(TAG,
" OBIS Code: %s"
, this->
obis_code
.c_str());
51
}
52
53
}
// namespace sml
54
}
// namespace esphome
esphome::sml::bytes_repr
std::string bytes_repr(const bytes &buffer)
Definition:
sml_parser.cpp:106
esphome::sml::SmlTextSensor::SmlTextSensor
SmlTextSensor(std::string server_id, std::string obis_code, SmlType format)
Definition:
sml_text_sensor.cpp:11
esphome::sml::SML_UINT
Definition:
constants.h:12
std
STL namespace.
esphome::sml::bytes_to_int
int64_t bytes_to_int(const bytes &buffer)
Definition:
sml_parser.cpp:122
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition:
text_sensor.cpp:9
esphome::sml::bytes_to_uint
uint64_t bytes_to_uint(const bytes &buffer)
Definition:
sml_parser.cpp:114
esphome::sml::SmlType
SmlType
Definition:
constants.h:8
esphome::sml::SML_INT
Definition:
constants.h:11
esphome::sml::SmlTextSensor::format_
SmlType format_
Definition:
sml_text_sensor.h:17
sml_text_sensor.h
esphome::sml::SmlListener
Definition:
sml.h:13
esphome::sml::ObisInfo::value
bytes value
Definition:
sml_parser.h:29
esphome::sml::SmlListener::server_id
std::string server_id
Definition:
sml.h:15
esphome::sml::SML_UNDEFINED
Definition:
constants.h:15
esphome::to_string
std::string to_string(int value)
Definition:
helpers.cpp:80
esphome::sml::SML_OCTET
Definition:
constants.h:9
esphome::sml::ObisInfo
Definition:
sml_parser.h:21
esphome::sml::SmlListener::obis_code
std::string obis_code
Definition:
sml.h:16
esphome::sml::SmlTextSensor::publish_val
void publish_val(const ObisInfo &obis_info) override
Definition:
sml_text_sensor.cpp:14
esphome
Implementation of SPI Controller mode.
Definition:
a01nyub.cpp:7
helpers.h
esphome::sml::SML_HEX
Definition:
constants.h:14
log.h
esphome::sml::SmlTextSensor::dump_config
void dump_config() override
Definition:
sml_text_sensor.cpp:45
esphome::sml::SML_BOOL
Definition:
constants.h:10
esphome::sml::ObisInfo::value_type
uint16_t value_type
Definition:
sml_parser.h:30
Generated by
1.8.13