From 9a26cdb3364fa40957a05309d16b007f96f29c0f Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Thu, 27 Jun 2024 05:50:25 +0400 Subject: [PATCH] [modbus_text_sensor] new default ANSI encoding type (#6975) --- esphome/components/modbus_controller/text_sensor/__init__.py | 3 ++- .../modbus_controller/text_sensor/modbus_textsensor.cpp | 5 ++++- .../modbus_controller/text_sensor/modbus_textsensor.h | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/esphome/components/modbus_controller/text_sensor/__init__.py b/esphome/components/modbus_controller/text_sensor/__init__.py index 763336e104..81d6453c6f 100644 --- a/esphome/components/modbus_controller/text_sensor/__init__.py +++ b/esphome/components/modbus_controller/text_sensor/__init__.py @@ -37,6 +37,7 @@ RAW_ENCODING = { "NONE": RawEncoding.NONE, "HEXBYTES": RawEncoding.HEXBYTES, "COMMA": RawEncoding.COMMA, + "ANSI": RawEncoding.ANSI, } CONFIG_SCHEMA = cv.All( @@ -49,7 +50,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE), cv.Optional(CONF_REGISTER_COUNT, default=0): cv.positive_int, cv.Optional(CONF_RESPONSE_SIZE, default=2): cv.positive_int, - cv.Optional(CONF_RAW_ENCODE, default="NONE"): cv.enum(RAW_ENCODING), + cv.Optional(CONF_RAW_ENCODE, default="ANSI"): cv.enum(RAW_ENCODING), } ), validate_modbus_register, diff --git a/esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp b/esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp index da5c0fba37..acdcacc083 100644 --- a/esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp +++ b/esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp @@ -27,8 +27,11 @@ void ModbusTextSensor::parse_and_publish(const std::vector &data) { sprintf(buffer, index != this->offset ? ",%d" : "%d", b); output << buffer; break; + case RawEncoding::ANSI: + if (b < 0x20) + break; + // FALLTHROUGH // Anything else no encoding - case RawEncoding::NONE: default: output << (char) b; break; diff --git a/esphome/components/modbus_controller/text_sensor/modbus_textsensor.h b/esphome/components/modbus_controller/text_sensor/modbus_textsensor.h index 9cc0db05a5..d6eb5fd230 100644 --- a/esphome/components/modbus_controller/text_sensor/modbus_textsensor.h +++ b/esphome/components/modbus_controller/text_sensor/modbus_textsensor.h @@ -9,7 +9,7 @@ namespace esphome { namespace modbus_controller { -enum class RawEncoding { NONE = 0, HEXBYTES = 1, COMMA = 2 }; +enum class RawEncoding { NONE = 0, HEXBYTES = 1, COMMA = 2, ANSI = 3 }; class ModbusTextSensor : public Component, public text_sensor::TextSensor, public SensorItem { public: