mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Remove redundant entity_id collision check in entity registry (#143660)
* Remove redundant entity_id collision check in entity registry * Update test
This commit is contained in:
parent
dd9dad80be
commit
cec8db173b
@ -883,7 +883,6 @@ class EntityPlatform:
|
|||||||
get_initial_options=entity.get_initial_entity_options,
|
get_initial_options=entity.get_initial_entity_options,
|
||||||
has_entity_name=entity.has_entity_name,
|
has_entity_name=entity.has_entity_name,
|
||||||
hidden_by=hidden_by,
|
hidden_by=hidden_by,
|
||||||
known_object_ids=self.entities,
|
|
||||||
original_device_class=entity.device_class,
|
original_device_class=entity.device_class,
|
||||||
original_icon=entity.icon,
|
original_icon=entity.icon,
|
||||||
original_name=entity_name,
|
original_name=entity_name,
|
||||||
@ -927,7 +926,7 @@ class EntityPlatform:
|
|||||||
f"{self.entity_namespace} {suggested_object_id}"
|
f"{self.entity_namespace} {suggested_object_id}"
|
||||||
)
|
)
|
||||||
entity.entity_id = entity_registry.async_generate_entity_id(
|
entity.entity_id = entity_registry.async_generate_entity_id(
|
||||||
self.domain, suggested_object_id, self.entities
|
self.domain, suggested_object_id
|
||||||
)
|
)
|
||||||
|
|
||||||
# Make sure it is valid in case an entity set the value themselves
|
# Make sure it is valid in case an entity set the value themselves
|
||||||
|
@ -11,7 +11,7 @@ timer.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from collections.abc import Callable, Container, Hashable, KeysView, Mapping
|
from collections.abc import Callable, Hashable, KeysView, Mapping
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
import logging
|
import logging
|
||||||
@ -787,26 +787,18 @@ class EntityRegistry(BaseRegistry):
|
|||||||
"""Return known device ids."""
|
"""Return known device ids."""
|
||||||
return list(self.entities.get_device_ids())
|
return list(self.entities.get_device_ids())
|
||||||
|
|
||||||
def _entity_id_available(
|
def _entity_id_available(self, entity_id: str) -> bool:
|
||||||
self, entity_id: str, known_object_ids: Container[str] | None
|
|
||||||
) -> bool:
|
|
||||||
"""Return True if the entity_id is available.
|
"""Return True if the entity_id is available.
|
||||||
|
|
||||||
An entity_id is available if:
|
An entity_id is available if:
|
||||||
- It's not registered
|
- It's not registered
|
||||||
- It's not known by the entity component adding the entity
|
- It's available (not in the state machine and not reserved)
|
||||||
- It's not in the state machine
|
|
||||||
|
|
||||||
Note that an entity_id which belongs to a deleted entity is considered
|
Note that an entity_id which belongs to a deleted entity is considered
|
||||||
available.
|
available.
|
||||||
"""
|
"""
|
||||||
if known_object_ids is None:
|
return entity_id not in self.entities and self.hass.states.async_available(
|
||||||
known_object_ids = {}
|
entity_id
|
||||||
|
|
||||||
return (
|
|
||||||
entity_id not in self.entities
|
|
||||||
and entity_id not in known_object_ids
|
|
||||||
and self.hass.states.async_available(entity_id)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -814,7 +806,6 @@ class EntityRegistry(BaseRegistry):
|
|||||||
self,
|
self,
|
||||||
domain: str,
|
domain: str,
|
||||||
suggested_object_id: str,
|
suggested_object_id: str,
|
||||||
known_object_ids: Container[str] | None = None,
|
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Generate an entity ID that does not conflict.
|
"""Generate an entity ID that does not conflict.
|
||||||
|
|
||||||
@ -826,11 +817,9 @@ class EntityRegistry(BaseRegistry):
|
|||||||
raise MaxLengthExceeded(domain, "domain", MAX_LENGTH_STATE_DOMAIN)
|
raise MaxLengthExceeded(domain, "domain", MAX_LENGTH_STATE_DOMAIN)
|
||||||
|
|
||||||
test_string = preferred_string[:MAX_LENGTH_STATE_ENTITY_ID]
|
test_string = preferred_string[:MAX_LENGTH_STATE_ENTITY_ID]
|
||||||
if known_object_ids is None:
|
|
||||||
known_object_ids = set()
|
|
||||||
|
|
||||||
tries = 1
|
tries = 1
|
||||||
while not self._entity_id_available(test_string, known_object_ids):
|
while not self._entity_id_available(test_string):
|
||||||
tries += 1
|
tries += 1
|
||||||
len_suffix = len(str(tries)) + 1
|
len_suffix = len(str(tries)) + 1
|
||||||
test_string = (
|
test_string = (
|
||||||
@ -847,7 +836,6 @@ class EntityRegistry(BaseRegistry):
|
|||||||
unique_id: str,
|
unique_id: str,
|
||||||
*,
|
*,
|
||||||
# To influence entity ID generation
|
# To influence entity ID generation
|
||||||
known_object_ids: Container[str] | None = None,
|
|
||||||
suggested_object_id: str | None = None,
|
suggested_object_id: str | None = None,
|
||||||
# To disable or hide an entity if it gets created
|
# To disable or hide an entity if it gets created
|
||||||
disabled_by: RegistryEntryDisabler | None = None,
|
disabled_by: RegistryEntryDisabler | None = None,
|
||||||
@ -921,7 +909,6 @@ class EntityRegistry(BaseRegistry):
|
|||||||
entity_id = self.async_generate_entity_id(
|
entity_id = self.async_generate_entity_id(
|
||||||
domain,
|
domain,
|
||||||
suggested_object_id or f"{platform}_{unique_id}",
|
suggested_object_id or f"{platform}_{unique_id}",
|
||||||
known_object_ids,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -1164,7 +1151,7 @@ class EntityRegistry(BaseRegistry):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if new_entity_id is not UNDEFINED and new_entity_id != old.entity_id:
|
if new_entity_id is not UNDEFINED and new_entity_id != old.entity_id:
|
||||||
if not self._entity_id_available(new_entity_id, None):
|
if not self._entity_id_available(new_entity_id):
|
||||||
raise ValueError("Entity with this ID is already registered")
|
raise ValueError("Entity with this ID is already registered")
|
||||||
|
|
||||||
if not valid_entity_id(new_entity_id):
|
if not valid_entity_id(new_entity_id):
|
||||||
|
@ -2017,7 +2017,9 @@ async def test_disabled_entities_excluded_from_entity_list(
|
|||||||
) == [entry1, entry2]
|
) == [entry1, entry2]
|
||||||
|
|
||||||
|
|
||||||
async def test_entity_max_length_exceeded(entity_registry: er.EntityRegistry) -> None:
|
async def test_entity_max_length_exceeded(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||||
|
) -> None:
|
||||||
"""Test that an exception is raised when the max character length is exceeded."""
|
"""Test that an exception is raised when the max character length is exceeded."""
|
||||||
|
|
||||||
long_domain_name = (
|
long_domain_name = (
|
||||||
@ -2042,20 +2044,13 @@ async def test_entity_max_length_exceeded(entity_registry: er.EntityRegistry) ->
|
|||||||
"1234567890123456789012345678901234567"
|
"1234567890123456789012345678901234567"
|
||||||
)
|
)
|
||||||
|
|
||||||
known = []
|
new_id = entity_registry.async_generate_entity_id("sensor", long_entity_id_name)
|
||||||
new_id = entity_registry.async_generate_entity_id(
|
|
||||||
"sensor", long_entity_id_name, known
|
|
||||||
)
|
|
||||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7]
|
assert new_id == "sensor." + long_entity_id_name[: 255 - 7]
|
||||||
known.append(new_id)
|
hass.states.async_reserve(new_id)
|
||||||
new_id = entity_registry.async_generate_entity_id(
|
new_id = entity_registry.async_generate_entity_id("sensor", long_entity_id_name)
|
||||||
"sensor", long_entity_id_name, known
|
|
||||||
)
|
|
||||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_2"
|
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_2"
|
||||||
known.append(new_id)
|
hass.states.async_reserve(new_id)
|
||||||
new_id = entity_registry.async_generate_entity_id(
|
new_id = entity_registry.async_generate_entity_id("sensor", long_entity_id_name)
|
||||||
"sensor", long_entity_id_name, known
|
|
||||||
)
|
|
||||||
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_3"
|
assert new_id == "sensor." + long_entity_id_name[: 255 - 7 - 2] + "_3"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user