From 6e134b325dafd3b33b430db3ee7b4d58f597e049 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Feb 2024 07:51:02 -0600 Subject: [PATCH] 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 --- homeassistant/config_entries.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 770693289b1..e4cd2205671 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -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 (