mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Add ability to get config_entry as required (#131400)
* Add ability to get config_entry as required * One more * Use new API
This commit is contained in:
parent
33983fa9a7
commit
b11d951ed7
@ -1828,6 +1828,16 @@ class ConfigEntries:
|
|||||||
"""Return entry with matching entry_id."""
|
"""Return entry with matching entry_id."""
|
||||||
return self._entries.data.get(entry_id)
|
return self._entries.data.get(entry_id)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_get_known_entry(self, entry_id: str) -> ConfigEntry:
|
||||||
|
"""Return entry with matching entry_id.
|
||||||
|
|
||||||
|
Raises UnknownEntry if entry is not found.
|
||||||
|
"""
|
||||||
|
if (entry := self.async_get_entry(entry_id)) is None:
|
||||||
|
raise UnknownEntry
|
||||||
|
return entry
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_entry_ids(self) -> list[str]:
|
def async_entry_ids(self) -> list[str]:
|
||||||
"""Return entry ids."""
|
"""Return entry ids."""
|
||||||
@ -1917,8 +1927,7 @@ class ConfigEntries:
|
|||||||
|
|
||||||
async def _async_remove(self, entry_id: str) -> tuple[bool, ConfigEntry]:
|
async def _async_remove(self, entry_id: str) -> tuple[bool, ConfigEntry]:
|
||||||
"""Remove and unload an entry."""
|
"""Remove and unload an entry."""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
async with entry.setup_lock:
|
async with entry.setup_lock:
|
||||||
if not entry.state.recoverable:
|
if not entry.state.recoverable:
|
||||||
@ -2011,8 +2020,7 @@ class ConfigEntries:
|
|||||||
|
|
||||||
Return True if entry has been successfully loaded.
|
Return True if entry has been successfully loaded.
|
||||||
"""
|
"""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
if entry.state is not ConfigEntryState.NOT_LOADED:
|
if entry.state is not ConfigEntryState.NOT_LOADED:
|
||||||
raise OperationNotAllowed(
|
raise OperationNotAllowed(
|
||||||
@ -2043,8 +2051,7 @@ class ConfigEntries:
|
|||||||
|
|
||||||
async def async_unload(self, entry_id: str, _lock: bool = True) -> bool:
|
async def async_unload(self, entry_id: str, _lock: bool = True) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
if not entry.state.recoverable:
|
if not entry.state.recoverable:
|
||||||
raise OperationNotAllowed(
|
raise OperationNotAllowed(
|
||||||
@ -2062,8 +2069,7 @@ class ConfigEntries:
|
|||||||
@callback
|
@callback
|
||||||
def async_schedule_reload(self, entry_id: str) -> None:
|
def async_schedule_reload(self, entry_id: str) -> None:
|
||||||
"""Schedule a config entry to be reloaded."""
|
"""Schedule a config entry to be reloaded."""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
entry.async_cancel_retry_setup()
|
entry.async_cancel_retry_setup()
|
||||||
self.hass.async_create_task(
|
self.hass.async_create_task(
|
||||||
self.async_reload(entry_id),
|
self.async_reload(entry_id),
|
||||||
@ -2081,8 +2087,7 @@ class ConfigEntries:
|
|||||||
|
|
||||||
If an entry was not loaded, will just load.
|
If an entry was not loaded, will just load.
|
||||||
"""
|
"""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
# Cancel the setup retry task before waiting for the
|
# Cancel the setup retry task before waiting for the
|
||||||
# reload lock to reduce the chance of concurrent reload
|
# reload lock to reduce the chance of concurrent reload
|
||||||
@ -2112,8 +2117,7 @@ class ConfigEntries:
|
|||||||
|
|
||||||
If disabled_by is changed, the config entry will be reloaded.
|
If disabled_by is changed, the config entry will be reloaded.
|
||||||
"""
|
"""
|
||||||
if (entry := self.async_get_entry(entry_id)) is None:
|
entry = self.async_get_known_entry(entry_id)
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
_validate_item(disabled_by=disabled_by)
|
_validate_item(disabled_by=disabled_by)
|
||||||
if entry.disabled_by is disabled_by:
|
if entry.disabled_by is disabled_by:
|
||||||
@ -3000,9 +3004,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
|
|||||||
@callback
|
@callback
|
||||||
def _get_reauth_entry(self) -> ConfigEntry:
|
def _get_reauth_entry(self) -> ConfigEntry:
|
||||||
"""Return the reauth config entry linked to the current context."""
|
"""Return the reauth config entry linked to the current context."""
|
||||||
if entry := self.hass.config_entries.async_get_entry(self._reauth_entry_id):
|
return self.hass.config_entries.async_get_known_entry(self._reauth_entry_id)
|
||||||
return entry
|
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _reconfigure_entry_id(self) -> str:
|
def _reconfigure_entry_id(self) -> str:
|
||||||
@ -3014,11 +3016,9 @@ class ConfigFlow(ConfigEntryBaseFlow):
|
|||||||
@callback
|
@callback
|
||||||
def _get_reconfigure_entry(self) -> ConfigEntry:
|
def _get_reconfigure_entry(self) -> ConfigEntry:
|
||||||
"""Return the reconfigure config entry linked to the current context."""
|
"""Return the reconfigure config entry linked to the current context."""
|
||||||
if entry := self.hass.config_entries.async_get_entry(
|
return self.hass.config_entries.async_get_known_entry(
|
||||||
self._reconfigure_entry_id
|
self._reconfigure_entry_id
|
||||||
):
|
)
|
||||||
return entry
|
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
|
|
||||||
class OptionsFlowManager(
|
class OptionsFlowManager(
|
||||||
@ -3030,11 +3030,7 @@ class OptionsFlowManager(
|
|||||||
|
|
||||||
def _async_get_config_entry(self, config_entry_id: str) -> ConfigEntry:
|
def _async_get_config_entry(self, config_entry_id: str) -> ConfigEntry:
|
||||||
"""Return config entry or raise if not found."""
|
"""Return config entry or raise if not found."""
|
||||||
entry = self.hass.config_entries.async_get_entry(config_entry_id)
|
return self.hass.config_entries.async_get_known_entry(config_entry_id)
|
||||||
if entry is None:
|
|
||||||
raise UnknownEntry(config_entry_id)
|
|
||||||
|
|
||||||
return entry
|
|
||||||
|
|
||||||
async def async_create_flow(
|
async def async_create_flow(
|
||||||
self,
|
self,
|
||||||
@ -3068,9 +3064,8 @@ class OptionsFlowManager(
|
|||||||
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
|
if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
entry = self.hass.config_entries.async_get_entry(flow.handler)
|
entry = self.hass.config_entries.async_get_known_entry(flow.handler)
|
||||||
if entry is None:
|
|
||||||
raise UnknownEntry(flow.handler)
|
|
||||||
if result["data"] is not None:
|
if result["data"] is not None:
|
||||||
self.hass.config_entries.async_update_entry(entry, options=result["data"])
|
self.hass.config_entries.async_update_entry(entry, options=result["data"])
|
||||||
|
|
||||||
@ -3142,9 +3137,7 @@ class OptionsFlow(ConfigEntryBaseFlow):
|
|||||||
|
|
||||||
if self.hass is None:
|
if self.hass is None:
|
||||||
raise ValueError("The config entry is not available during initialisation")
|
raise ValueError("The config entry is not available during initialisation")
|
||||||
if entry := self.hass.config_entries.async_get_entry(self._config_entry_id):
|
return self.hass.config_entries.async_get_known_entry(self._config_entry_id)
|
||||||
return entry
|
|
||||||
raise UnknownEntry
|
|
||||||
|
|
||||||
@config_entry.setter
|
@config_entry.setter
|
||||||
def config_entry(self, value: ConfigEntry) -> None:
|
def config_entry(self, value: ConfigEntry) -> None:
|
||||||
@ -3223,10 +3216,9 @@ class EntityRegistryDisabledHandler:
|
|||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
config_entry = self.hass.config_entries.async_get_entry(
|
config_entry = self.hass.config_entries.async_get_known_entry(
|
||||||
entity_entry.config_entry_id
|
entity_entry.config_entry_id
|
||||||
)
|
)
|
||||||
assert config_entry is not None
|
|
||||||
|
|
||||||
if config_entry.entry_id not in self.changed and config_entry.supports_unload:
|
if config_entry.entry_id not in self.changed and config_entry.supports_unload:
|
||||||
self.changed.add(config_entry.entry_id)
|
self.changed.add(config_entry.entry_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user