mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Add SML (Smart Message Language) platform for energy meters (#2396)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -182,6 +182,7 @@ esphome/components/sht4x/* @sjtrny | ||||
| esphome/components/shutdown/* @esphome/core @jsuanet | ||||
| esphome/components/sim800l/* @glmnet | ||||
| esphome/components/sm2135/* @BoukeHaarsma23 | ||||
| esphome/components/sml/* @alengwenus | ||||
| esphome/components/socket/* @esphome/core | ||||
| esphome/components/sonoff_d1/* @anatoly-savchenkov | ||||
| esphome/components/spi/* @esphome/core | ||||
|   | ||||
							
								
								
									
										38
									
								
								esphome/components/sml/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								esphome/components/sml/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| import re | ||||
|  | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import uart | ||||
| from esphome.const import CONF_ID | ||||
|  | ||||
| CODEOWNERS = ["@alengwenus"] | ||||
|  | ||||
| DEPENDENCIES = ["uart"] | ||||
|  | ||||
| sml_ns = cg.esphome_ns.namespace("sml") | ||||
| Sml = sml_ns.class_("Sml", cg.Component, uart.UARTDevice) | ||||
| MULTI_CONF = True | ||||
|  | ||||
| CONF_SML_ID = "sml_id" | ||||
| CONF_OBIS_CODE = "obis_code" | ||||
| CONF_SERVER_ID = "server_id" | ||||
|  | ||||
| CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(Sml), | ||||
|     } | ||||
| ).extend(uart.UART_DEVICE_SCHEMA) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     await cg.register_component(var, config) | ||||
|     await uart.register_uart_device(var, config) | ||||
|  | ||||
|  | ||||
| def obis_code(value): | ||||
|     value = cv.string(value) | ||||
|     match = re.match(r"^\d{1,3}-\d{1,3}:\d{1,3}\.\d{1,3}\.\d{1,3}$", value) | ||||
|     if match is None: | ||||
|         raise cv.Invalid(f"{value} is not a valid OBIS code") | ||||
|     return value | ||||
							
								
								
									
										48
									
								
								esphome/components/sml/constants.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								esphome/components/sml/constants.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <cstdint> | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| enum SmlType : uint8_t { | ||||
|   SML_OCTET = 0, | ||||
|   SML_BOOL = 4, | ||||
|   SML_INT = 5, | ||||
|   SML_UINT = 6, | ||||
|   SML_LIST = 7, | ||||
|   SML_HEX = 10, | ||||
|   SML_UNDEFINED = 255 | ||||
| }; | ||||
|  | ||||
| enum SmlMessageType : uint16_t { SML_PUBLIC_OPEN_RES = 0x0101, SML_GET_LIST_RES = 0x701 }; | ||||
|  | ||||
| enum Crc16CheckResult : uint8_t { CHECK_CRC16_FAILED, CHECK_CRC16_X25_SUCCESS, CHECK_CRC16_KERMIT_SUCCESS }; | ||||
|  | ||||
| // masks with two-bit mapping 0x1b -> 0b01; 0x01 -> 0b10; 0x1a -> 0b11 | ||||
| const uint16_t START_MASK = 0x55aa;  // 0x1b 1b 1b 1b 1b 01 01 01 01 | ||||
| const uint16_t END_MASK = 0x0157;    // 0x1b 1b 1b 1b 1a | ||||
|  | ||||
| const uint16_t CRC16_X25_TABLE[256] = { | ||||
|     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, | ||||
|     0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, | ||||
|     0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, | ||||
|     0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, | ||||
|     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, | ||||
|     0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, | ||||
|     0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, | ||||
|     0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, | ||||
|     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, | ||||
|     0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, | ||||
|     0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, | ||||
|     0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, | ||||
|     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, | ||||
|     0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, | ||||
|     0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, | ||||
|     0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, | ||||
|     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, | ||||
|     0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, | ||||
|     0x3de3, 0x2c6a, 0x1ef1, 0x0f78}; | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										30
									
								
								esphome/components/sml/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								esphome/components/sml/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import sensor | ||||
| from esphome.const import CONF_ID | ||||
|  | ||||
| from .. import CONF_OBIS_CODE, CONF_SERVER_ID, CONF_SML_ID, Sml, obis_code, sml_ns | ||||
|  | ||||
| AUTO_LOAD = ["sml"] | ||||
|  | ||||
| SmlSensor = sml_ns.class_("SmlSensor", sensor.Sensor, cg.Component) | ||||
|  | ||||
|  | ||||
| CONFIG_SCHEMA = sensor.sensor_schema().extend( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(SmlSensor), | ||||
|         cv.GenerateID(CONF_SML_ID): cv.use_id(Sml), | ||||
|         cv.Required(CONF_OBIS_CODE): obis_code, | ||||
|         cv.Optional(CONF_SERVER_ID, default=""): cv.string, | ||||
|     } | ||||
| ) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable( | ||||
|         config[CONF_ID], config[CONF_SERVER_ID], config[CONF_OBIS_CODE] | ||||
|     ) | ||||
|     await cg.register_component(var, config) | ||||
|     await sensor.register_sensor(var, config) | ||||
|     sml = await cg.get_variable(config[CONF_SML_ID]) | ||||
|     cg.add(sml.register_sml_listener(var)) | ||||
							
								
								
									
										41
									
								
								esphome/components/sml/sensor/sml_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								esphome/components/sml/sensor/sml_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| #include "esphome/core/log.h" | ||||
| #include "sml_sensor.h" | ||||
| #include "../sml_parser.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| static const char *const TAG = "sml_sensor"; | ||||
|  | ||||
| SmlSensor::SmlSensor(std::string server_id, std::string obis_code) | ||||
|     : SmlListener(std::move(server_id), std::move(obis_code)) {} | ||||
|  | ||||
| void SmlSensor::publish_val(const ObisInfo &obis_info) { | ||||
|   switch (obis_info.value_type) { | ||||
|     case SML_INT: { | ||||
|       publish_state(bytes_to_int(obis_info.value)); | ||||
|       break; | ||||
|     } | ||||
|     case SML_BOOL: | ||||
|     case SML_UINT: { | ||||
|       publish_state(bytes_to_uint(obis_info.value)); | ||||
|       break; | ||||
|     } | ||||
|     case SML_OCTET: { | ||||
|       ESP_LOGW(TAG, "No number conversion for (%s) %s. Consider using SML TextSensor instead.", | ||||
|                bytes_repr(obis_info.server_id).c_str(), obis_info.code_repr().c_str()); | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| void SmlSensor::dump_config() { | ||||
|   LOG_SENSOR("", "SML", this); | ||||
|   if (!this->server_id.empty()) { | ||||
|     ESP_LOGCONFIG(TAG, "  Server ID: %s", this->server_id.c_str()); | ||||
|   } | ||||
|   ESP_LOGCONFIG(TAG, "  OBIS Code: %s", this->obis_code.c_str()); | ||||
| } | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										16
									
								
								esphome/components/sml/sensor/sml_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								esphome/components/sml/sensor/sml_sensor.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| #pragma once | ||||
| #include "esphome/components/sml/sml.h" | ||||
| #include "esphome/components/sensor/sensor.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| class SmlSensor : public SmlListener, public sensor::Sensor, public Component { | ||||
|  public: | ||||
|   SmlSensor(std::string server_id, std::string obis_code); | ||||
|   void publish_val(const ObisInfo &obis_info) override; | ||||
|   void dump_config() override; | ||||
| }; | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										146
									
								
								esphome/components/sml/sml.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								esphome/components/sml/sml.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,146 @@ | ||||
| #include "sml.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "sml_parser.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| static const char *const TAG = "sml"; | ||||
|  | ||||
| const char START_BYTES_DETECTED = 1; | ||||
| const char END_BYTES_DETECTED = 2; | ||||
|  | ||||
| SmlListener::SmlListener(std::string server_id, std::string obis_code) | ||||
|     : server_id(std::move(server_id)), obis_code(std::move(obis_code)) {} | ||||
|  | ||||
| char Sml::check_start_end_bytes_(uint8_t byte) { | ||||
|   this->incoming_mask_ = (this->incoming_mask_ << 2) | get_code(byte); | ||||
|  | ||||
|   if (this->incoming_mask_ == START_MASK) | ||||
|     return START_BYTES_DETECTED; | ||||
|   if ((this->incoming_mask_ >> 6) == END_MASK) | ||||
|     return END_BYTES_DETECTED; | ||||
|   return 0; | ||||
| } | ||||
|  | ||||
| void Sml::loop() { | ||||
|   while (available()) { | ||||
|     const char c = read(); | ||||
|  | ||||
|     if (this->record_) | ||||
|       this->sml_data_.emplace_back(c); | ||||
|  | ||||
|     switch (this->check_start_end_bytes_(c)) { | ||||
|       case START_BYTES_DETECTED: { | ||||
|         this->record_ = true; | ||||
|         this->sml_data_.clear(); | ||||
|         break; | ||||
|       }; | ||||
|       case END_BYTES_DETECTED: { | ||||
|         if (this->record_) { | ||||
|           this->record_ = false; | ||||
|  | ||||
|           if (!check_sml_data(this->sml_data_)) | ||||
|             break; | ||||
|  | ||||
|           // remove footer bytes | ||||
|           this->sml_data_.resize(this->sml_data_.size() - 8); | ||||
|           this->process_sml_file_(this->sml_data_); | ||||
|         } | ||||
|         break; | ||||
|       }; | ||||
|     }; | ||||
|   } | ||||
| } | ||||
|  | ||||
| void Sml::process_sml_file_(const bytes &sml_data) { | ||||
|   SmlFile sml_file = SmlFile(sml_data); | ||||
|   std::vector<ObisInfo> obis_info = sml_file.get_obis_info(); | ||||
|   this->publish_obis_info_(obis_info); | ||||
|  | ||||
|   this->log_obis_info_(obis_info); | ||||
| } | ||||
|  | ||||
| void Sml::log_obis_info_(const std::vector<ObisInfo> &obis_info_vec) { | ||||
|   ESP_LOGD(TAG, "OBIS info:"); | ||||
|   for (auto const &obis_info : obis_info_vec) { | ||||
|     std::string info; | ||||
|     info += "  (" + bytes_repr(obis_info.server_id) + ") "; | ||||
|     info += obis_info.code_repr(); | ||||
|     info += " [0x" + bytes_repr(obis_info.value) + "]"; | ||||
|     ESP_LOGD(TAG, "%s", info.c_str()); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void Sml::publish_obis_info_(const std::vector<ObisInfo> &obis_info_vec) { | ||||
|   for (auto const &obis_info : obis_info_vec) { | ||||
|     this->publish_value_(obis_info); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void Sml::publish_value_(const ObisInfo &obis_info) { | ||||
|   for (auto const &sml_listener : sml_listeners_) { | ||||
|     if ((!sml_listener->server_id.empty()) && (bytes_repr(obis_info.server_id) != sml_listener->server_id)) | ||||
|       continue; | ||||
|     if (obis_info.code_repr() != sml_listener->obis_code) | ||||
|       continue; | ||||
|     sml_listener->publish_val(obis_info); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void Sml::dump_config() { ESP_LOGCONFIG(TAG, "SML:"); } | ||||
|  | ||||
| void Sml::register_sml_listener(SmlListener *listener) { sml_listeners_.emplace_back(listener); } | ||||
|  | ||||
| bool check_sml_data(const bytes &buffer) { | ||||
|   if (buffer.size() < 2) { | ||||
|     ESP_LOGW(TAG, "Checksum error in received SML data."); | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1); | ||||
|   if (crc_received == calc_crc16_x25(buffer.begin(), buffer.end() - 2, 0x6e23)) { | ||||
|     ESP_LOGV(TAG, "Checksum verification successful with CRC16/X25."); | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   if (crc_received == calc_crc16_kermit(buffer.begin(), buffer.end() - 2, 0xed50)) { | ||||
|     ESP_LOGV(TAG, "Checksum verification successful with CRC16/KERMIT."); | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   ESP_LOGW(TAG, "Checksum error in received SML data."); | ||||
|   return false; | ||||
| } | ||||
|  | ||||
| uint16_t calc_crc16_p1021(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum) { | ||||
|   for (auto it = begin; it != end; it++) { | ||||
|     crcsum = (crcsum >> 8) ^ CRC16_X25_TABLE[(crcsum & 0xff) ^ *it]; | ||||
|   } | ||||
|   return crcsum; | ||||
| } | ||||
|  | ||||
| uint16_t calc_crc16_x25(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum = 0) { | ||||
|   crcsum = calc_crc16_p1021(begin, end, crcsum ^ 0xffff) ^ 0xffff; | ||||
|   return (crcsum >> 8) | ((crcsum & 0xff) << 8); | ||||
| } | ||||
|  | ||||
| uint16_t calc_crc16_kermit(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum = 0) { | ||||
|   return calc_crc16_p1021(begin, end, crcsum); | ||||
| } | ||||
|  | ||||
| uint8_t get_code(uint8_t byte) { | ||||
|   switch (byte) { | ||||
|     case 0x1b: | ||||
|       return 1; | ||||
|     case 0x01: | ||||
|       return 2; | ||||
|     case 0x1a: | ||||
|       return 3; | ||||
|     default: | ||||
|       return 0; | ||||
|   } | ||||
| } | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										47
									
								
								esphome/components/sml/sml.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								esphome/components/sml/sml.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
| #include <vector> | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/components/uart/uart.h" | ||||
| #include "sml_parser.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| class SmlListener { | ||||
|  public: | ||||
|   std::string server_id; | ||||
|   std::string obis_code; | ||||
|   SmlListener(std::string server_id, std::string obis_code); | ||||
|   virtual void publish_val(const ObisInfo &obis_info){}; | ||||
| }; | ||||
|  | ||||
| class Sml : public Component, public uart::UARTDevice { | ||||
|  public: | ||||
|   void register_sml_listener(SmlListener *listener); | ||||
|   void loop() override; | ||||
|   void dump_config() override; | ||||
|   std::vector<SmlListener *> sml_listeners_{}; | ||||
|  | ||||
|  protected: | ||||
|   void process_sml_file_(const bytes &sml_data); | ||||
|   void log_obis_info_(const std::vector<ObisInfo> &obis_info_vec); | ||||
|   void publish_obis_info_(const std::vector<ObisInfo> &obis_info_vec); | ||||
|   char check_start_end_bytes_(uint8_t byte); | ||||
|   void publish_value_(const ObisInfo &obis_info); | ||||
|  | ||||
|   // Serial parser | ||||
|   bool record_ = false; | ||||
|   uint16_t incoming_mask_ = 0; | ||||
|   bytes sml_data_; | ||||
| }; | ||||
|  | ||||
| bool check_sml_data(const bytes &buffer); | ||||
| uint16_t calc_crc16_p1021(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); | ||||
| uint16_t calc_crc16_x25(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); | ||||
| uint16_t calc_crc16_kermit(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); | ||||
|  | ||||
| uint8_t get_code(uint8_t byte); | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										131
									
								
								esphome/components/sml/sml_parser.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								esphome/components/sml/sml_parser.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,131 @@ | ||||
| #include "esphome/core/helpers.h" | ||||
| #include "constants.h" | ||||
| #include "sml_parser.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| SmlFile::SmlFile(bytes buffer) : buffer_(std::move(buffer)) { | ||||
|   // extract messages | ||||
|   this->pos_ = 0; | ||||
|   while (this->pos_ < this->buffer_.size()) { | ||||
|     if (this->buffer_[this->pos_] == 0x00) | ||||
|       break;  // fill byte detected -> no more messages | ||||
|  | ||||
|     SmlNode message = SmlNode(); | ||||
|     if (!this->setup_node(&message)) | ||||
|       break; | ||||
|     this->messages.emplace_back(message); | ||||
|   } | ||||
| } | ||||
|  | ||||
| bool SmlFile::setup_node(SmlNode *node) { | ||||
|   uint8_t type = this->buffer_[this->pos_] >> 4;      // type including overlength info | ||||
|   uint8_t length = this->buffer_[this->pos_] & 0x0f;  // length including TL bytes | ||||
|   bool is_list = (type & 0x07) == SML_LIST; | ||||
|   bool has_extended_length = type & 0x08;  // we have a long list/value (>15 entries) | ||||
|   uint8_t parse_length = length; | ||||
|   if (has_extended_length) { | ||||
|     length = (length << 4) + (this->buffer_[this->pos_ + 1] & 0x0f); | ||||
|     parse_length = length - 1; | ||||
|     this->pos_ += 1; | ||||
|   } | ||||
|  | ||||
|   if (this->pos_ + parse_length >= this->buffer_.size()) | ||||
|     return false; | ||||
|  | ||||
|   node->type = type & 0x07; | ||||
|   node->nodes.clear(); | ||||
|   node->value_bytes.clear(); | ||||
|   if (this->buffer_[this->pos_] == 0x00) {  // end of message | ||||
|     this->pos_ += 1; | ||||
|   } else if (is_list) {  // list | ||||
|     this->pos_ += 1; | ||||
|     node->nodes.reserve(parse_length); | ||||
|     for (size_t i = 0; i != parse_length; i++) { | ||||
|       SmlNode child_node = SmlNode(); | ||||
|       if (!this->setup_node(&child_node)) | ||||
|         return false; | ||||
|       node->nodes.emplace_back(child_node); | ||||
|     } | ||||
|   } else {  // value | ||||
|     node->value_bytes = | ||||
|         bytes(this->buffer_.begin() + this->pos_ + 1, this->buffer_.begin() + this->pos_ + parse_length); | ||||
|     this->pos_ += parse_length; | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| std::vector<ObisInfo> SmlFile::get_obis_info() { | ||||
|   std::vector<ObisInfo> obis_info; | ||||
|   for (auto const &message : messages) { | ||||
|     SmlNode message_body = message.nodes[3]; | ||||
|     uint16_t message_type = bytes_to_uint(message_body.nodes[0].value_bytes); | ||||
|     if (message_type != SML_GET_LIST_RES) | ||||
|       continue; | ||||
|  | ||||
|     SmlNode get_list_response = message_body.nodes[1]; | ||||
|     bytes server_id = get_list_response.nodes[1].value_bytes; | ||||
|     SmlNode val_list = get_list_response.nodes[4]; | ||||
|  | ||||
|     for (auto const &val_list_entry : val_list.nodes) { | ||||
|       obis_info.emplace_back(server_id, val_list_entry); | ||||
|     } | ||||
|   } | ||||
|   return obis_info; | ||||
| } | ||||
|  | ||||
| std::string bytes_repr(const bytes &buffer) { | ||||
|   std::string repr; | ||||
|   for (auto const value : buffer) { | ||||
|     repr += str_sprintf("%02x", value & 0xff); | ||||
|   } | ||||
|   return repr; | ||||
| } | ||||
|  | ||||
| uint64_t bytes_to_uint(const bytes &buffer) { | ||||
|   uint64_t val = 0; | ||||
|   for (auto const value : buffer) { | ||||
|     val = (val << 8) + value; | ||||
|   } | ||||
|   return val; | ||||
| } | ||||
|  | ||||
| int64_t bytes_to_int(const bytes &buffer) { | ||||
|   uint64_t tmp = bytes_to_uint(buffer); | ||||
|   int64_t val; | ||||
|  | ||||
|   switch (buffer.size()) { | ||||
|     case 1:  // int8 | ||||
|       val = (int8_t) tmp; | ||||
|       break; | ||||
|     case 2:  // int16 | ||||
|       val = (int16_t) tmp; | ||||
|       break; | ||||
|     case 4:  // int32 | ||||
|       val = (int32_t) tmp; | ||||
|       break; | ||||
|     default:  // int64 | ||||
|       val = (int64_t) tmp; | ||||
|   } | ||||
|   return val; | ||||
| } | ||||
|  | ||||
| std::string bytes_to_string(const bytes &buffer) { return std::string(buffer.begin(), buffer.end()); } | ||||
|  | ||||
| ObisInfo::ObisInfo(bytes server_id, SmlNode val_list_entry) : server_id(std::move(server_id)) { | ||||
|   this->code = val_list_entry.nodes[0].value_bytes; | ||||
|   this->status = val_list_entry.nodes[1].value_bytes; | ||||
|   this->unit = bytes_to_uint(val_list_entry.nodes[3].value_bytes); | ||||
|   this->scaler = bytes_to_int(val_list_entry.nodes[4].value_bytes); | ||||
|   SmlNode value_node = val_list_entry.nodes[5]; | ||||
|   this->value = value_node.value_bytes; | ||||
|   this->value_type = value_node.type; | ||||
| } | ||||
|  | ||||
| std::string ObisInfo::code_repr() const { | ||||
|   return str_sprintf("%d-%d:%d.%d.%d", this->code[0], this->code[1], this->code[2], this->code[3], this->code[4]); | ||||
| } | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										54
									
								
								esphome/components/sml/sml_parser.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								esphome/components/sml/sml_parser.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <cstdint> | ||||
| #include <cstdio> | ||||
| #include <string> | ||||
| #include <vector> | ||||
| #include "constants.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| using bytes = std::vector<uint8_t>; | ||||
|  | ||||
| class SmlNode { | ||||
|  public: | ||||
|   uint8_t type; | ||||
|   bytes value_bytes; | ||||
|   std::vector<SmlNode> nodes; | ||||
| }; | ||||
|  | ||||
| class ObisInfo { | ||||
|  public: | ||||
|   ObisInfo(bytes server_id, SmlNode val_list_entry); | ||||
|   bytes server_id; | ||||
|   bytes code; | ||||
|   bytes status; | ||||
|   char unit; | ||||
|   char scaler; | ||||
|   bytes value; | ||||
|   uint16_t value_type; | ||||
|   std::string code_repr() const; | ||||
| }; | ||||
|  | ||||
| class SmlFile { | ||||
|  public: | ||||
|   SmlFile(bytes buffer); | ||||
|   bool setup_node(SmlNode *node); | ||||
|   std::vector<SmlNode> messages; | ||||
|   std::vector<ObisInfo> get_obis_info(); | ||||
|  | ||||
|  protected: | ||||
|   const bytes buffer_; | ||||
|   size_t pos_; | ||||
| }; | ||||
|  | ||||
| std::string bytes_repr(const bytes &buffer); | ||||
|  | ||||
| uint64_t bytes_to_uint(const bytes &buffer); | ||||
|  | ||||
| int64_t bytes_to_int(const bytes &buffer); | ||||
|  | ||||
| std::string bytes_to_string(const bytes &buffer); | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										43
									
								
								esphome/components/sml/text_sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								esphome/components/sml/text_sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import text_sensor | ||||
| from esphome.const import CONF_FORMAT, CONF_ID | ||||
|  | ||||
| from .. import CONF_OBIS_CODE, CONF_SERVER_ID, CONF_SML_ID, Sml, obis_code, sml_ns | ||||
|  | ||||
| AUTO_LOAD = ["sml"] | ||||
|  | ||||
| SmlType = sml_ns.enum("SmlType") | ||||
| SML_TYPES = { | ||||
|     "text": SmlType.SML_OCTET, | ||||
|     "bool": SmlType.SML_BOOL, | ||||
|     "int": SmlType.SML_INT, | ||||
|     "uint": SmlType.SML_UINT, | ||||
|     "hex": SmlType.SML_HEX, | ||||
|     "": SmlType.SML_UNDEFINED, | ||||
| } | ||||
|  | ||||
| SmlTextSensor = sml_ns.class_("SmlTextSensor", text_sensor.TextSensor, cg.Component) | ||||
|  | ||||
| CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(SmlTextSensor), | ||||
|         cv.GenerateID(CONF_SML_ID): cv.use_id(Sml), | ||||
|         cv.Required(CONF_OBIS_CODE): obis_code, | ||||
|         cv.Optional(CONF_SERVER_ID, default=""): cv.string, | ||||
|         cv.Optional(CONF_FORMAT, default=""): cv.enum(SML_TYPES, lower=True), | ||||
|     } | ||||
| ) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable( | ||||
|         config[CONF_ID], | ||||
|         config[CONF_SERVER_ID], | ||||
|         config[CONF_OBIS_CODE], | ||||
|         config[CONF_FORMAT], | ||||
|     ) | ||||
|     await cg.register_component(var, config) | ||||
|     await text_sensor.register_text_sensor(var, config) | ||||
|     sml = await cg.get_variable(config[CONF_SML_ID]) | ||||
|     cg.add(sml.register_sml_listener(var)) | ||||
							
								
								
									
										54
									
								
								esphome/components/sml/text_sensor/sml_text_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								esphome/components/sml/text_sensor/sml_text_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| #include "esphome/core/helpers.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "sml_text_sensor.h" | ||||
| #include "../sml_parser.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| static const char *const TAG = "sml_text_sensor"; | ||||
|  | ||||
| SmlTextSensor::SmlTextSensor(std::string server_id, std::string obis_code, SmlType format) | ||||
|     : SmlListener(std::move(server_id), std::move(obis_code)), format_(format) {} | ||||
|  | ||||
| void SmlTextSensor::publish_val(const ObisInfo &obis_info) { | ||||
|   uint8_t value_type; | ||||
|   if (this->format_ == SML_UNDEFINED) { | ||||
|     value_type = obis_info.value_type; | ||||
|   } else { | ||||
|     value_type = this->format_; | ||||
|   } | ||||
|  | ||||
|   switch (value_type) { | ||||
|     case SML_HEX: { | ||||
|       publish_state("0x" + bytes_repr(obis_info.value)); | ||||
|       break; | ||||
|     } | ||||
|     case SML_INT: { | ||||
|       publish_state(to_string(bytes_to_int(obis_info.value))); | ||||
|       break; | ||||
|     } | ||||
|     case SML_BOOL: | ||||
|       publish_state(bytes_to_uint(obis_info.value) ? "True" : "False"); | ||||
|       break; | ||||
|     case SML_UINT: { | ||||
|       publish_state(to_string(bytes_to_uint(obis_info.value))); | ||||
|       break; | ||||
|     } | ||||
|     case SML_OCTET: { | ||||
|       publish_state(std::string(obis_info.value.begin(), obis_info.value.end())); | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| void SmlTextSensor::dump_config() { | ||||
|   LOG_TEXT_SENSOR("", "SML", this); | ||||
|   if (!this->server_id.empty()) { | ||||
|     ESP_LOGCONFIG(TAG, "  Server ID: %s", this->server_id.c_str()); | ||||
|   } | ||||
|   ESP_LOGCONFIG(TAG, "  OBIS Code: %s", this->obis_code.c_str()); | ||||
| } | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
							
								
								
									
										21
									
								
								esphome/components/sml/text_sensor/sml_text_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esphome/components/sml/text_sensor/sml_text_sensor.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "esphome/components/sml/sml.h" | ||||
| #include "esphome/components/text_sensor/text_sensor.h" | ||||
| #include "../constants.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace sml { | ||||
|  | ||||
| class SmlTextSensor : public SmlListener, public text_sensor::TextSensor, public Component { | ||||
|  public: | ||||
|   SmlTextSensor(std::string server_id, std::string obis_code, SmlType format); | ||||
|   void publish_val(const ObisInfo &obis_info) override; | ||||
|   void dump_config() override; | ||||
|  | ||||
|  protected: | ||||
|   SmlType format_; | ||||
| }; | ||||
|  | ||||
| }  // namespace sml | ||||
| }  // namespace esphome | ||||
		Reference in New Issue
	
	Block a user