Skip to content

Commit

Permalink
Make ConfigEntryItems responsible for updating unique ids (#110018)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bdraco committed Feb 9, 2024
1 parent ced922b commit 6e134b3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions homeassistant/config_entries.py
Expand Up @@ -1146,6 +1146,10 @@ def __setitem__(self, entry_id: str, entry: ConfigEntry) -> None:
_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
Expand Down Expand Up @@ -1191,6 +1195,16 @@ def __delitem__(self, entry_id: str) -> None:
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, [])
Expand Down Expand Up @@ -1517,10 +1531,7 @@ def async_update_entry(

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 (
Expand Down

0 comments on commit 6e134b3

Please sign in to comment.