From 8b46610281a6faeb3ff7b2cc4e16946575bfa53b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jan 2026 19:39:11 -1000 Subject: [PATCH] tweaks --- esphome/components/mqtt/mqtt_component.cpp | 5 ++--- esphome/core/config.py | 2 ++ esphome/core/entity_base.h | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index 8d04d376f6..d838d1789f 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -28,10 +28,9 @@ inline char *append_char(char *p, char c) { // These limits are enforced at Python config validation time in mqtt/__init__.py // using cv.Length() validators for topic_prefix and discovery_prefix. // MQTT_COMPONENT_TYPE_MAX_LEN and MQTT_SUFFIX_MAX_LEN are defined in mqtt_component.h. -// OBJECT_ID_MAX_LEN is defined in entity_base.h and derived from friendly_name limits. +// ESPHOME_DEVICE_NAME_MAX_LEN and OBJECT_ID_MAX_LEN are defined in entity_base.h. // This ensures the stack buffers below are always large enough. static constexpr size_t TOPIC_PREFIX_MAX_LEN = 64; // Validated in Python: cv.Length(max=64) -static constexpr size_t SANITIZED_NAME_MAX_LEN = 64; // Safe: hostname validation limits to 31 chars static constexpr size_t DISCOVERY_PREFIX_MAX_LEN = 64; // Validated in Python: cv.Length(max=64) // Stack buffer sizes - safe because all inputs are length-validated at config time @@ -40,7 +39,7 @@ static constexpr size_t DEFAULT_TOPIC_MAX_LEN = TOPIC_PREFIX_MAX_LEN + 1 + MQTT_COMPONENT_TYPE_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 1 + MQTT_SUFFIX_MAX_LEN + 1; // Format: prefix + "/" + type + "/" + name + "/" + object_id + "/config" + null static constexpr size_t DISCOVERY_TOPIC_MAX_LEN = DISCOVERY_PREFIX_MAX_LEN + 1 + MQTT_COMPONENT_TYPE_MAX_LEN + 1 + - SANITIZED_NAME_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 7 + 1; + ESPHOME_DEVICE_NAME_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 7 + 1; void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; } diff --git a/esphome/core/config.py b/esphome/core/config.py index f9c3011507..b7e6ab9bee 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -76,6 +76,7 @@ VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"} def validate_hostname(config): + # Keep in sync with ESPHOME_DEVICE_NAME_MAX_LEN in esphome/core/entity_base.h max_length = 31 if config[CONF_NAME_ADD_MAC_SUFFIX]: max_length -= 7 # "-AABBCC" is appended when add mac suffix option is used @@ -207,6 +208,7 @@ CONFIG_SCHEMA = cv.All( cv.Schema( { cv.Required(CONF_NAME): cv.valid_name, + # Keep max=120 in sync with OBJECT_ID_MAX_LEN in esphome/core/entity_base.h cv.Optional(CONF_FRIENDLY_NAME, ""): cv.All( cv.string_no_slash, cv.Length(max=120) ), diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index a45c7795bf..1649077dd0 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -13,7 +13,10 @@ namespace esphome { -// Maximum size for object_id buffer (friendly_name max ~120 + margin) +// Maximum device name length - keep in sync with validate_hostname() in esphome/core/config.py +static constexpr size_t ESPHOME_DEVICE_NAME_MAX_LEN = 31; + +// Maximum size for object_id buffer - keep in sync with friendly_name cv.Length(max=120) in esphome/core/config.py static constexpr size_t OBJECT_ID_MAX_LEN = 128; enum EntityCategory : uint8_t {