mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Optimize async_generate_entity_id (#34440)
This commit is contained in:
parent
bd46a6130a
commit
0abcf2d176
@ -9,7 +9,6 @@ timer.
|
|||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from itertools import chain
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, cast
|
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, cast
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import Event, callback, split_entity_id, valid_entity_id
|
from homeassistant.core import Event, callback, split_entity_id, valid_entity_id
|
||||||
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
|
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util import ensure_unique_string, slugify
|
from homeassistant.util import slugify
|
||||||
from homeassistant.util.yaml import load_yaml
|
from homeassistant.util.yaml import load_yaml
|
||||||
|
|
||||||
from .typing import HomeAssistantType
|
from .typing import HomeAssistantType
|
||||||
@ -145,14 +144,21 @@ class EntityRegistry:
|
|||||||
|
|
||||||
Conflicts checked against registered and currently existing entities.
|
Conflicts checked against registered and currently existing entities.
|
||||||
"""
|
"""
|
||||||
return ensure_unique_string(
|
preferred_string = f"{domain}.{slugify(suggested_object_id)}"
|
||||||
f"{domain}.{slugify(suggested_object_id)}",
|
test_string = preferred_string
|
||||||
chain(
|
if not known_object_ids:
|
||||||
self.entities.keys(),
|
known_object_ids = {}
|
||||||
self.hass.states.async_entity_ids(domain),
|
|
||||||
known_object_ids if known_object_ids else [],
|
tries = 1
|
||||||
),
|
while (
|
||||||
)
|
test_string in self.entities
|
||||||
|
or test_string in known_object_ids
|
||||||
|
or self.hass.states.get(test_string)
|
||||||
|
):
|
||||||
|
tries += 1
|
||||||
|
test_string = f"{preferred_string}_{tries}"
|
||||||
|
|
||||||
|
return test_string
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_get_or_create(
|
def async_get_or_create(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user