2#if defined(USE_ESP32) || defined(USE_ARDUINO) || defined(USE_HOST)
14static void log_callback(dlms_parser::LogLevel level,
const char *
fmt, va_list args) {
15 std::array<char, 256> buf;
16 vsnprintf(buf.data(), buf.size(),
fmt, args);
18 case dlms_parser::LogLevel::ERROR:
19 ESP_LOGE(TAG,
"%s", buf.data());
21 case dlms_parser::LogLevel::WARNING:
22 ESP_LOGW(TAG,
"%s", buf.data());
24 case dlms_parser::LogLevel::INFO:
25 ESP_LOGI(TAG,
"%s", buf.data());
27 case dlms_parser::LogLevel::VERBOSE:
28 ESP_LOGV(TAG,
"%s", buf.data());
30 case dlms_parser::LogLevel::VERY_VERBOSE:
31 ESP_LOGVV(TAG,
"%s", buf.data());
33 case dlms_parser::LogLevel::DEBUG:
34 ESP_LOGD(TAG,
"%s", buf.data());
40 const char *decryption_key,
const char *authentication_key,
41 std::vector<CustomPattern> custom_patterns)
42 : receive_timeout_ms_(receive_timeout_ms),
43 skip_crc_check_(skip_crc_check),
44 custom_patterns_(
std::move(custom_patterns)),
45 parser_([this](const dlms_parser::AxdrCapture &capture) { this->
on_data_(capture); }, &this->decryptor_) {
46 dlms_parser::Logger::set_log_function(log_callback);
48 if (decryption_key !=
nullptr) {
49 const auto key = dlms_parser::Aes128GcmDecryptionKey::from_hex(decryption_key);
50 if (
key.has_value()) {
51 this->parser_.set_decryption_key(*
key);
53 ESP_LOGE(TAG,
"Failed to set decryption key: invalid key format");
57 if (authentication_key !=
nullptr) {
58 const auto key = dlms_parser::Aes128GcmAuthenticationKey::from_hex(authentication_key);
59 if (
key.has_value()) {
60 this->parser_.set_authentication_key(*
key);
62 ESP_LOGE(TAG,
"Failed to set authentication key: invalid key format");
66 this->parser_.set_skip_crc_check(this->skip_crc_check_);
68 this->parser_.load_default_patterns();
70 for (
const auto &custom_pattern : this->custom_patterns_) {
71 this->parser_.register_pattern(custom_pattern.name, custom_pattern.pattern, custom_pattern.priority,
72 custom_pattern.default_obis);
79 ESP_LOGCONFIG(TAG,
"DLMS Meter:");
81 ESP_LOGCONFIG(TAG,
" Skip CRC Check: %s", YESNO(this->
skip_crc_check_));
83 std::array<char, 24> obis_buf;
85 for (
const auto &custom_pattern : this->custom_patterns_) {
86 const auto default_obis = custom_pattern.default_obis.to_string(obis_buf);
87 ESP_LOGCONFIG(TAG,
" Custom Pattern: '%s' (name: %s, priority: %d, default_obis: %.*s)", custom_pattern.pattern,
88 custom_pattern.name, custom_pattern.priority,
static_cast<int>(default_obis.size()),
93 for (
const auto &item : this->
sensors_) {
94 LOG_SENSOR(
" ",
"Numeric Sensor (OBIS)", item.sensor);
95 const auto obis = item.obis.to_string(obis_buf);
96 ESP_LOGCONFIG(TAG,
" OBIS: %.*s",
static_cast<int>(obis.size()), obis.data());
100#ifdef USE_TEXT_SENSOR
102 LOG_TEXT_SENSOR(
" ",
"Text Sensor (OBIS)", item.sensor);
103 const auto obis = item.obis.to_string(obis_buf);
104 ESP_LOGCONFIG(TAG,
" OBIS: %.*s",
static_cast<int>(obis.size()), obis.data());
108#ifdef USE_BINARY_SENSOR
110 LOG_BINARY_SENSOR(
" ",
"Binary Sensor (OBIS)", item.sensor);
111 const auto obis = item.obis.to_string(obis_buf);
112 ESP_LOGCONFIG(TAG,
" OBIS: %.*s",
static_cast<int>(obis.size()), obis.data());
131 ESP_LOGW(TAG,
"RX Buffer overflow. Frame too large! Dropping frame.");
140 ESP_LOGW(TAG,
"UART read failed. Dropping frame.");
160 int updated_count = 0;
162 if (capture.is_numeric()) {
163#if defined(USE_SENSOR) || defined(USE_BINARY_SENSOR)
164 const float value = capture.value_as_float_with_scaler_applied();
167 for (
const auto &item : this->
sensors_) {
168 if (item.obis == capture.obis) {
169 item.sensor->publish_state(
value);
174#ifdef USE_BINARY_SENSOR
176 if (item.obis == capture.obis) {
177 item.sensor->publish_state(
value != 0.0f);
183#ifdef USE_TEXT_SENSOR
185 if (item.obis == capture.obis) {
186 std::array<char, 128> value_buf;
187 const auto value = capture.value_as_string(value_buf);
188 item.sensor->publish_state(
value.data(),
value.size());
195 if (updated_count == 0) {
196 std::array<char, 24> obis_buf;
197 const auto obis = capture.obis.to_string(obis_buf);
198 ESP_LOGV(TAG,
"Received OBIS %.*s, but no sensors are registered for it.",
static_cast<int>(obis.size()),
205 this->
sensors_.push_back({obis, sensor});
209#ifdef USE_TEXT_SENSOR
215#ifdef USE_BINARY_SENSOR
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
Base class for all binary_sensor-type classes.
DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check, const char *decryption_key, const char *authentication_key, std::vector< CustomPattern > custom_patterns)
StaticVector< SensorItem, DLMS_MAX_SENSORS > sensors_
void on_data_(const dlms_parser::AxdrCapture &capture)
std::array< uint8_t, 2048 > rx_buffer_
void register_text_sensor(const dlms_parser::ObisId &obis, text_sensor::TextSensor *sensor)
void dump_config() override
uint32_t receive_timeout_ms_
StaticVector< TextSensorItem, DLMS_MAX_TEXT_SENSORS > text_sensors_
void register_sensor(const dlms_parser::ObisId &obis, sensor::Sensor *sensor)
StaticVector< BinarySensorItem, DLMS_MAX_BINARY_SENSORS > binary_sensors_
size_t bytes_accumulated_
dlms_parser::DlmsParser parser_
void register_binary_sensor(const dlms_parser::ObisId &obis, binary_sensor::BinarySensor *sensor)
uint32_t last_rx_char_time_
Base-class for all sensors.
optional< std::array< uint8_t, N > > read_array()
void discard_available()
Read and drop every byte waiting in the receive buffer.
ESPHOME_LOG_TAG(TAG, "dlms_meter")
size_t size_t const char * fmt
Application App
Global storage of Application pointer - only one Application can exist.