ESPHome  2024.3.2
sml_sensor.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "sml_sensor.h"
3 #include "../sml_parser.h"
4 
5 namespace esphome {
6 namespace sml {
7 
8 static const char *const TAG = "sml_sensor";
9 
10 SmlSensor::SmlSensor(std::string server_id, std::string obis_code)
11  : SmlListener(std::move(server_id), std::move(obis_code)) {}
12 
13 void SmlSensor::publish_val(const ObisInfo &obis_info) {
14  switch (obis_info.value_type) {
15  case SML_INT: {
16  publish_state(bytes_to_int(obis_info.value));
17  break;
18  }
19  case SML_BOOL:
20  case SML_UINT: {
21  publish_state(bytes_to_uint(obis_info.value));
22  break;
23  }
24  case SML_OCTET: {
25  ESP_LOGW(TAG, "No number conversion for (%s) %s. Consider using SML TextSensor instead.",
26  bytes_repr(obis_info.server_id).c_str(), obis_info.code_repr().c_str());
27  break;
28  }
29  }
30 }
31 
33  LOG_SENSOR("", "SML", this);
34  if (!this->server_id.empty()) {
35  ESP_LOGCONFIG(TAG, " Server ID: %s", this->server_id.c_str());
36  }
37  ESP_LOGCONFIG(TAG, " OBIS Code: %s", this->obis_code.c_str());
38 }
39 
40 } // namespace sml
41 } // namespace esphome
std::string bytes_repr(const bytes &buffer)
Definition: sml_parser.cpp:78
STL namespace.
int64_t bytes_to_int(const bytes &buffer)
Definition: sml_parser.cpp:94
uint64_t bytes_to_uint(const bytes &buffer)
Definition: sml_parser.cpp:86
std::string server_id
Definition: sml.h:15
void dump_config() override
Definition: sml_sensor.cpp:32
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void publish_val(const ObisInfo &obis_info) override
Definition: sml_sensor.cpp:13
std::string obis_code
Definition: sml.h:16
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
SmlSensor(std::string server_id, std::string obis_code)
Definition: sml_sensor.cpp:10
std::string code_repr() const
Definition: sml_parser.cpp:122