diff --git a/esphome/components/rc522/rc522.cpp b/esphome/components/rc522/rc522.cpp index 8f8740c925..470c50109e 100644 --- a/esphome/components/rc522/rc522.cpp +++ b/esphome/components/rc522/rc522.cpp @@ -492,7 +492,10 @@ bool RC522BinarySensor::process(std::vector &data) { this->found_ = result; return result; } -void RC522Trigger::process(std::vector &data) { this->trigger(format_hex_pretty(data, '-', false)); } +void RC522Trigger::process(std::vector &data) { + char uid_buf[format_hex_pretty_size(RC522_MAX_UID_SIZE)]; + this->trigger(format_hex_pretty_to(uid_buf, data.data(), data.size(), '-')); +} } // namespace rc522 } // namespace esphome diff --git a/esphome/components/remote_base/midea_protocol.h b/esphome/components/remote_base/midea_protocol.h index 0a5de8e9df..c3030d565e 100644 --- a/esphome/components/remote_base/midea_protocol.h +++ b/esphome/components/remote_base/midea_protocol.h @@ -29,6 +29,8 @@ class MideaData { bool is_valid() const { return this->data_[OFFSET_CS] == this->calc_cs_(); } void finalize() { this->data_[OFFSET_CS] = this->calc_cs_(); } bool is_compliment(const MideaData &rhs) const; + /// @deprecated Allocates heap memory. Use to_str() instead. Removed in 2026.7.0. + ESPDEPRECATED("Allocates heap memory. Use to_str() instead. Removed in 2026.7.0.", "2026.1.0") std::string to_string() const { return format_hex_pretty(this->data_.data(), this->data_.size()); } /// Buffer size for to_str(): 6 bytes = "AA.BB.CC.DD.EE.FF\0" static constexpr size_t TO_STR_BUFFER_SIZE = format_hex_pretty_size(6); diff --git a/esphome/components/tuya/text_sensor/tuya_text_sensor.cpp b/esphome/components/tuya/text_sensor/tuya_text_sensor.cpp index 3c492d609d..36b6d630ae 100644 --- a/esphome/components/tuya/text_sensor/tuya_text_sensor.cpp +++ b/esphome/components/tuya/text_sensor/tuya_text_sensor.cpp @@ -1,4 +1,5 @@ #include "tuya_text_sensor.h" +#include "esphome/core/entity_base.h" #include "esphome/core/log.h" namespace esphome { @@ -14,9 +15,11 @@ void TuyaTextSensor::setup() { this->publish_state(datapoint.value_string); break; case TuyaDatapointType::RAW: { - std::string data = format_hex_pretty(datapoint.value_raw); - ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, data.c_str()); - this->publish_state(data); + char hex_buf[MAX_STATE_LEN + 1]; + const char *formatted = + format_hex_pretty_to(hex_buf, sizeof(hex_buf), datapoint.value_raw.data(), datapoint.value_raw.size()); + ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, formatted); + this->publish_state(formatted); break; } case TuyaDatapointType::ENUM: { diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 5f75872a0f..f91bd9b20c 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -26,6 +26,9 @@ static constexpr size_t ESPHOME_DOMAIN_MAX_LEN = 20; // Maximum size for object_id buffer (friendly_name + null + margin) static constexpr size_t OBJECT_ID_MAX_LEN = 128; +// Maximum state length that Home Assistant will accept without raising ValueError +static constexpr size_t MAX_STATE_LEN = 255; + enum EntityCategory : uint8_t { ENTITY_CATEGORY_NONE = 0, ENTITY_CATEGORY_CONFIG = 1,