Optimize async_generate_entity_id (#34440)

This commit is contained in:
J. Nick Koston 2020-04-19 18:19:11 -05:00 committed by GitHub
parent bd46a6130a
commit 0abcf2d176
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,6 @@ timer.
"""
import asyncio
from collections import OrderedDict
from itertools import chain
import logging
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.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
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 .typing import HomeAssistantType
@ -145,14 +144,21 @@ class EntityRegistry:
Conflicts checked against registered and currently existing entities.
"""
return ensure_unique_string(
f"{domain}.{slugify(suggested_object_id)}",
chain(
self.entities.keys(),
self.hass.states.async_entity_ids(domain),
known_object_ids if known_object_ids else [],
),
)
preferred_string = f"{domain}.{slugify(suggested_object_id)}"
test_string = preferred_string
if not known_object_ids:
known_object_ids = {}
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
def async_get_or_create(