move DEVICE_CLASS_MAX_LENGTH to esphome/core/config.py alongside other max length constants

This commit is contained in:
J. Nick Koston
2026-03-03 12:03:51 -10:00
parent 151f1377a3
commit 5373412f60
3 changed files with 7 additions and 8 deletions
-5
View File
@@ -398,11 +398,6 @@ def string_strict(value):
)
# Max device class string length (47 chars + null = 48-byte PROGMEM buffer)
# Keep in sync with MAX_DEVICE_CLASS_LENGTH in esphome/core/entity_base.h
DEVICE_CLASS_MAX_LENGTH = 47
def icon(value):
"""Validate that a given config value is a valid icon."""
from esphome.core.config import ICON_MAX_LENGTH
+4
View File
@@ -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 device class string length (47 chars + null = 48-byte PROGMEM buffer)
# Keep in sync with MAX_DEVICE_CLASS_LENGTH in esphome/core/entity_base.h
DEVICE_CLASS_MAX_LENGTH = 47
# 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
+3 -3
View File
@@ -17,7 +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.core.config import DEVICE_CLASS_MAX_LENGTH, 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
@@ -179,9 +179,9 @@ def _register_string(
def register_device_class(value: str) -> int:
"""Register a device_class string and return its 1-based index."""
if value and len(value) > cv.DEVICE_CLASS_MAX_LENGTH:
if value and len(value) > DEVICE_CLASS_MAX_LENGTH:
raise ValueError(
f"Device class string too long ({len(value)} chars, max {cv.DEVICE_CLASS_MAX_LENGTH}): '{value}'"
f"Device class string too long ({len(value)} chars, max {DEVICE_CLASS_MAX_LENGTH}): '{value}'"
)
return _register_string(
value, _get_pool().device_classes, _MAX_DEVICE_CLASSES, "device_class"