mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Limit autogenerated entity_id string length (#69739)
This commit is contained in:
parent
7b75a16745
commit
70e125850c
@ -290,7 +290,7 @@ class EntityRegistry:
|
||||
if len(domain) > MAX_LENGTH_STATE_DOMAIN:
|
||||
raise MaxLengthExceeded(domain, "domain", MAX_LENGTH_STATE_DOMAIN)
|
||||
|
||||
test_string = preferred_string
|
||||
test_string = preferred_string[:MAX_LENGTH_STATE_ENTITY_ID]
|
||||
if not known_object_ids:
|
||||
known_object_ids = {}
|
||||
|
||||
@ -301,11 +301,9 @@ class EntityRegistry:
|
||||
or not self.hass.states.async_available(test_string)
|
||||
):
|
||||
tries += 1
|
||||
test_string = f"{preferred_string}_{tries}"
|
||||
|
||||
if len(test_string) > MAX_LENGTH_STATE_ENTITY_ID:
|
||||
raise MaxLengthExceeded(
|
||||
test_string, "generated_entity_id", MAX_LENGTH_STATE_ENTITY_ID
|
||||
len_suffix = len(str(tries)) + 1
|
||||
test_string = (
|
||||
f"{preferred_string[:MAX_LENGTH_STATE_ENTITY_ID-len_suffix]}_{tries}"
|
||||
)
|
||||
|
||||
return test_string
|
||||
|
@ -1119,22 +1119,13 @@ async def test_disabled_entities_excluded_from_entity_list(hass, registry):
|
||||
async def test_entity_max_length_exceeded(hass, registry):
|
||||
"""Test that an exception is raised when the max character length is exceeded."""
|
||||
|
||||
long_entity_id_name = (
|
||||
long_domain_name = (
|
||||
"1234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
"1234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
"1234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
"1234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
)
|
||||
|
||||
with pytest.raises(MaxLengthExceeded) as exc_info:
|
||||
registry.async_generate_entity_id("sensor", long_entity_id_name)
|
||||
|
||||
assert exc_info.value.property_name == "generated_entity_id"
|
||||
assert exc_info.value.max_length == 255
|
||||
assert exc_info.value.value == f"sensor.{long_entity_id_name}"
|
||||
|
||||
# Try again but against the domain
|
||||
long_domain_name = long_entity_id_name
|
||||
with pytest.raises(MaxLengthExceeded) as exc_info:
|
||||
registry.async_generate_entity_id(long_domain_name, "sensor")
|
||||
|
||||
@ -1150,14 +1141,15 @@ async def test_entity_max_length_exceeded(hass, registry):
|
||||
"1234567890123456789012345678901234567"
|
||||
)
|
||||
|
||||
with pytest.raises(MaxLengthExceeded) as exc_info:
|
||||
registry.async_generate_entity_id(
|
||||
"sensor", long_entity_id_name, [f"sensor.{long_entity_id_name}"]
|
||||
)
|
||||
|
||||
assert exc_info.value.property_name == "generated_entity_id"
|
||||
assert exc_info.value.max_length == 255
|
||||
assert exc_info.value.value == f"sensor.{long_entity_id_name}_2"
|
||||
known = []
|
||||
new_id = registry.async_generate_entity_id("sensor", long_entity_id_name, known)
|
||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7]
|
||||
known.append(new_id)
|
||||
new_id = registry.async_generate_entity_id("sensor", long_entity_id_name, known)
|
||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_2"
|
||||
known.append(new_id)
|
||||
new_id = registry.async_generate_entity_id("sensor", long_entity_id_name, known)
|
||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_3"
|
||||
|
||||
|
||||
async def test_resolve_entity_ids(hass, registry):
|
||||
|
Loading…
x
Reference in New Issue
Block a user