diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 368b4f9f4a..1eac53e9b2 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -398,13 +398,10 @@ def string_strict(value): ) -# Max icon string length (63 chars + null = 64-byte PROGMEM buffer) -# Keep in sync with MAX_ICON_LENGTH in esphome/core/entity_base.h -ICON_MAX_LENGTH = 63 - - def icon(value): """Validate that a given config value is a valid icon.""" + from esphome.core.config import ICON_MAX_LENGTH + value = string_strict(value) if not value: return value diff --git a/esphome/core/config.py b/esphome/core/config.py index 9411949bb9..cde2d280a4 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -188,6 +188,10 @@ else: # Keep in sync with ESPHOME_FRIENDLY_NAME_MAX_LEN in esphome/core/entity_base.h FRIENDLY_NAME_MAX_LEN = 120 +# Max icon string length (63 chars + null = 64-byte PROGMEM buffer) +# Keep in sync with MAX_ICON_LENGTH in esphome/core/entity_base.h +ICON_MAX_LENGTH = 63 + AREA_SCHEMA = cv.Schema( { cv.GenerateID(CONF_ID): cv.declare_id(Area), diff --git a/esphome/core/entity_helpers.py b/esphome/core/entity_helpers.py index a8ca2f7432..01fa27b833 100644 --- a/esphome/core/entity_helpers.py +++ b/esphome/core/entity_helpers.py @@ -17,6 +17,7 @@ from esphome.const import ( CONF_UNIT_OF_MEASUREMENT, ) from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority +from esphome.core.config import ICON_MAX_LENGTH from esphome.cpp_generator import MockObj, RawStatement, add, get_variable import esphome.final_validate as fv from esphome.helpers import cpp_string_escape, fnv1_hash_object_id, sanitize, snake_case @@ -190,9 +191,9 @@ def register_unit_of_measurement(value: str) -> int: def register_icon(value: str) -> int: """Register an icon string and return its 1-based index.""" - if value and len(value) > cv.ICON_MAX_LENGTH: + if value and len(value) > ICON_MAX_LENGTH: raise ValueError( - f"Icon string too long ({len(value)} chars, max {cv.ICON_MAX_LENGTH}): '{value}'" + f"Icon string too long ({len(value)} chars, max {ICON_MAX_LENGTH}): '{value}'" ) return _register_string(value, _get_pool().icons, _MAX_ICONS, "icon")