Add ConfigEntries.async_get_loaded (#124705)

This commit is contained in:
Erik Montnemery 2024-08-27 14:20:57 +02:00 committed by GitHub
parent 48292beec8
commit 9cae786f40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 0 deletions

View File

@ -1742,6 +1742,16 @@ class ConfigEntries:
and (include_disabled or not entry.disabled_by)
]
@callback
def async_loaded_entries(self, domain: str) -> list[ConfigEntry]:
"""Return loaded entries for a specific domain.
This will exclude ignored or disabled config entruis.
"""
entries = self._entries.get_entries_for_domain(domain)
return [entry for entry in entries if entry.state == ConfigEntryState.LOADED]
@callback
def async_entry_for_domain_unique_id(
self, domain: str, unique_id: str

View File

@ -6001,3 +6001,44 @@ async def test_migration_from_1_2(
]
},
}
async def test_async_loaded_entries(
hass: HomeAssistant, manager: config_entries.ConfigEntries
) -> None:
"""Test that we can get loaded config entries."""
entry1 = MockConfigEntry(domain="comp")
entry1.add_to_hass(hass)
entry2 = MockConfigEntry(domain="comp", source=config_entries.SOURCE_IGNORE)
entry2.add_to_hass(hass)
entry3 = MockConfigEntry(
domain="comp", disabled_by=config_entries.ConfigEntryDisabler.USER
)
entry3.add_to_hass(hass)
mock_setup = AsyncMock(return_value=True)
mock_setup_entry = AsyncMock(return_value=True)
mock_unload_entry = AsyncMock(return_value=True)
mock_integration(
hass,
MockModule(
"comp",
async_setup=mock_setup,
async_setup_entry=mock_setup_entry,
async_unload_entry=mock_unload_entry,
),
)
mock_platform(hass, "comp.config_flow", None)
assert hass.config_entries.async_loaded_entries("comp") == []
assert await manager.async_setup(entry1.entry_id)
assert not await manager.async_setup(entry2.entry_id)
assert not await manager.async_setup(entry3.entry_id)
assert hass.config_entries.async_loaded_entries("comp") == [entry1]
assert await hass.config_entries.async_unload(entry1.entry_id)
assert hass.config_entries.async_loaded_entries("comp") == []