Make ConfigEntryItems responsible for updating unique ids (#110018)

* Make ConfigEntryItems responsible for updating unique ids

* Make ConfigEntryItems responsible for updating unique ids

* Make ConfigEntryItems responsible for updating unique ids

* Make ConfigEntryItems responsible for updating unique ids

* Make ConfigEntryItems responsible for updating unique ids
This commit is contained in:
J. Nick Koston 2024-02-09 07:51:02 -06:00 committed by GitHub
parent ced922bb1a
commit 6e134b325d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1146,6 +1146,10 @@ class ConfigEntryItems(UserDict[str, ConfigEntry]):
_LOGGER.error("An entry with the id %s already exists", entry_id)
self._unindex_entry(entry_id)
data[entry_id] = entry
self._index_entry(entry)
def _index_entry(self, entry: ConfigEntry) -> None:
"""Index an entry."""
self._domain_index.setdefault(entry.domain, []).append(entry)
if entry.unique_id is not None:
unique_id_hash = entry.unique_id
@ -1191,6 +1195,16 @@ class ConfigEntryItems(UserDict[str, ConfigEntry]):
self._unindex_entry(entry_id)
super().__delitem__(entry_id)
def update_unique_id(self, entry: ConfigEntry, new_unique_id: str | None) -> None:
"""Update unique id for an entry.
This method mutates the entry with the new unique id and updates the indexes.
"""
entry_id = entry.entry_id
self._unindex_entry(entry_id)
entry.unique_id = new_unique_id
self._index_entry(entry)
def get_entries_for_domain(self, domain: str) -> list[ConfigEntry]:
"""Get entries for a domain."""
return self._domain_index.get(domain, [])
@ -1517,10 +1531,7 @@ class ConfigEntries:
if unique_id is not UNDEFINED and entry.unique_id != unique_id:
# Reindex the entry if the unique_id has changed
entry_id = entry.entry_id
del self._entries[entry_id]
entry.unique_id = unique_id
self._entries[entry_id] = entry
self._entries.update_unique_id(entry, unique_id)
changed = True
for attr, value in (