Avoid ConfigEntry lookups in hass.config_entries.async_entries for domain index (#100598)

This commit is contained in:
J. Nick Koston 2023-09-20 18:43:15 +02:00 committed by GitHub
parent 1f0c9a48d2
commit a03ad87cfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View file

@ -1047,7 +1047,7 @@ class ConfigEntries:
self.options = OptionsFlowManager(hass)
self._hass_config = hass_config
self._entries: dict[str, ConfigEntry] = {}
self._domain_index: dict[str, list[str]] = {}
self._domain_index: dict[str, list[ConfigEntry]] = {}
self._store = storage.Store[dict[str, list[dict[str, Any]]]](
hass, STORAGE_VERSION, STORAGE_KEY
)
@ -1077,9 +1077,7 @@ class ConfigEntries:
"""Return all entries or entries for a specific domain."""
if domain is None:
return list(self._entries.values())
return [
self._entries[entry_id] for entry_id in self._domain_index.get(domain, [])
]
return list(self._domain_index.get(domain, []))
async def async_add(self, entry: ConfigEntry) -> None:
"""Add and setup an entry."""
@ -1088,7 +1086,7 @@ class ConfigEntries:
f"An entry with the id {entry.entry_id} already exists."
)
self._entries[entry.entry_id] = entry
self._domain_index.setdefault(entry.domain, []).append(entry.entry_id)
self._domain_index.setdefault(entry.domain, []).append(entry)
self._async_dispatch(ConfigEntryChange.ADDED, entry)
await self.async_setup(entry.entry_id)
self._async_schedule_save()
@ -1106,7 +1104,7 @@ class ConfigEntries:
await entry.async_remove(self.hass)
del self._entries[entry.entry_id]
self._domain_index[entry.domain].remove(entry.entry_id)
self._domain_index[entry.domain].remove(entry)
if not self._domain_index[entry.domain]:
del self._domain_index[entry.domain]
self._async_schedule_save()
@ -1173,7 +1171,7 @@ class ConfigEntries:
return
entries = {}
domain_index: dict[str, list[str]] = {}
domain_index: dict[str, list[ConfigEntry]] = {}
for entry in config["entries"]:
pref_disable_new_entities = entry.get("pref_disable_new_entities")
@ -1188,7 +1186,7 @@ class ConfigEntries:
domain = entry["domain"]
entry_id = entry["entry_id"]
entries[entry_id] = ConfigEntry(
config_entry = ConfigEntry(
version=entry["version"],
domain=domain,
entry_id=entry_id,
@ -1207,7 +1205,8 @@ class ConfigEntries:
pref_disable_new_entities=pref_disable_new_entities,
pref_disable_polling=entry.get("pref_disable_polling"),
)
domain_index.setdefault(domain, []).append(entry_id)
entries[entry_id] = config_entry
domain_index.setdefault(domain, []).append(config_entry)
self._domain_index = domain_index
self._entries = entries

View file

@ -891,7 +891,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
unique_id=None,
disabled_by=None,
reason=None,
):
) -> None:
"""Initialize a mock config entry."""
kwargs = {
"entry_id": entry_id or uuid_util.random_uuid_hex(),
@ -913,17 +913,15 @@ class MockConfigEntry(config_entries.ConfigEntry):
if reason is not None:
self.reason = reason
def add_to_hass(self, hass):
def add_to_hass(self, hass: HomeAssistant) -> None:
"""Test helper to add entry to hass."""
hass.config_entries._entries[self.entry_id] = self
hass.config_entries._domain_index.setdefault(self.domain, []).append(
self.entry_id
)
hass.config_entries._domain_index.setdefault(self.domain, []).append(self)
def add_to_manager(self, manager):
def add_to_manager(self, manager: config_entries.ConfigEntries) -> None:
"""Test helper to add entry to entry manager."""
manager._entries[self.entry_id] = self
manager._domain_index.setdefault(self.domain, []).append(self.entry_id)
manager._domain_index.setdefault(self.domain, []).append(self)
def patch_yaml_files(files_dict, endswith=True):