mirror of
https://github.com/esphome/esphome.git
synced 2026-10-02 09:20:22 +00:00
[api] Add max_data_length proto option and optimize entity string encoding
Add a max_data_length field option to api_options.proto for string/bytes fields. When max_data_length < 128 and force = true, the code generator uses encode_short_string_force() — a single call that writes the tag byte, 1-byte length varint, and raw string data with no branching. Size calculation simplifies from calc_length(1, size) to 2 + size. Annotate entity fields across all 25 ListEntities*Response messages: - name and object_id: max_data_length = 120, force = true (50 fields) - icon: max_data_length = 63 (25 fields) The 120 and 63 values match NAME_MAX_LENGTH and ICON_MAX_LENGTH in esphome/core/config.py, validated at config time.
This commit is contained in:
@@ -28,6 +28,7 @@ from esphome.core.entity_helpers import (
|
||||
get_base_entity_object_id,
|
||||
register_device_class,
|
||||
register_icon,
|
||||
register_unit_of_measurement,
|
||||
setup_device_class,
|
||||
setup_entity,
|
||||
setup_unit_of_measurement,
|
||||
@@ -925,6 +926,22 @@ def test_register_device_class_max_length() -> None:
|
||||
assert register_device_class("") == 0
|
||||
|
||||
|
||||
def test_register_unit_of_measurement_max_length() -> None:
|
||||
"""Test register_unit_of_measurement rejects units exceeding 63 characters."""
|
||||
# 63 chars should succeed
|
||||
max_uom = "a" * 63
|
||||
idx = register_unit_of_measurement(max_uom)
|
||||
assert idx > 0
|
||||
|
||||
# 64 chars should fail
|
||||
too_long = "a" * 64
|
||||
with pytest.raises(ValueError, match="Unit of measurement string too long"):
|
||||
register_unit_of_measurement(too_long)
|
||||
|
||||
# Empty string returns 0
|
||||
assert register_unit_of_measurement("") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_entity_with_entity_category(
|
||||
setup_test_environment: list[str],
|
||||
|
||||
Reference in New Issue
Block a user