[modbus_text_sensor] new default ANSI encoding type (#6975)

This commit is contained in:
Sergey Dudanov 2024-06-27 05:50:25 +04:00 committed by GitHub
parent decf50ed49
commit 9a26cdb336
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 3 deletions

View file

@ -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,

View file

@ -27,8 +27,11 @@ void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &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;

View file

@ -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: