mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
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:
parent
ced922bb1a
commit
6e134b325d
@ -1146,6 +1146,10 @@ class ConfigEntryItems(UserDict[str, ConfigEntry]):
|
|||||||
_LOGGER.error("An entry with the id %s already exists", entry_id)
|
_LOGGER.error("An entry with the id %s already exists", entry_id)
|
||||||
self._unindex_entry(entry_id)
|
self._unindex_entry(entry_id)
|
||||||
data[entry_id] = entry
|
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)
|
self._domain_index.setdefault(entry.domain, []).append(entry)
|
||||||
if entry.unique_id is not None:
|
if entry.unique_id is not None:
|
||||||
unique_id_hash = entry.unique_id
|
unique_id_hash = entry.unique_id
|
||||||
@ -1191,6 +1195,16 @@ class ConfigEntryItems(UserDict[str, ConfigEntry]):
|
|||||||
self._unindex_entry(entry_id)
|
self._unindex_entry(entry_id)
|
||||||
super().__delitem__(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]:
|
def get_entries_for_domain(self, domain: str) -> list[ConfigEntry]:
|
||||||
"""Get entries for a domain."""
|
"""Get entries for a domain."""
|
||||||
return self._domain_index.get(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:
|
if unique_id is not UNDEFINED and entry.unique_id != unique_id:
|
||||||
# Reindex the entry if the unique_id has changed
|
# Reindex the entry if the unique_id has changed
|
||||||
entry_id = entry.entry_id
|
self._entries.update_unique_id(entry, unique_id)
|
||||||
del self._entries[entry_id]
|
|
||||||
entry.unique_id = unique_id
|
|
||||||
self._entries[entry_id] = entry
|
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
for attr, value in (
|
for attr, value in (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user