From 471e2a17a2846f63ab975eb46186cc38a97ce22a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 14 Jun 2024 08:00:36 +0200 Subject: [PATCH] Improve error messages when config entry is in wrong state (#119591) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve error messages when config entry is in wrong state * Apply suggestions from code review Co-authored-by: Joakim Sørensen * Adjust tests --------- Co-authored-by: Joakim Sørensen --- homeassistant/config_entries.py | 26 ++++++++++++++------------ tests/test_config_entries.py | 14 ++++++++------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index fdcf4ad7604..c8d671e1fe1 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -1812,9 +1812,9 @@ class ConfigEntries: if entry.state is not ConfigEntryState.NOT_LOADED: raise OperationNotAllowed( - f"The config entry {entry.title} ({entry.domain}) with entry_id" - f" {entry.entry_id} cannot be set up because it is already loaded" - f" in the {entry.state} state" + f"The config entry '{entry.title}' ({entry.domain}) with entry_id" + f" '{entry.entry_id}' cannot be set up because it is in state " + f"{entry.state}, but needs to be in the {ConfigEntryState.NOT_LOADED} state" ) # Setup Component if not set up yet @@ -1844,9 +1844,9 @@ class ConfigEntries: if not entry.state.recoverable: raise OperationNotAllowed( - f"The config entry {entry.title} ({entry.domain}) with entry_id" - f" {entry.entry_id} cannot be unloaded because it is not in a" - f" recoverable state ({entry.state})" + f"The config entry '{entry.title}' ({entry.domain}) with entry_id" + f" '{entry.entry_id}' cannot be unloaded because it is in the non" + f" recoverable state {entry.state}" ) if _lock: @@ -2049,9 +2049,10 @@ class ConfigEntries: async with entry.setup_lock: if entry.state is not ConfigEntryState.LOADED: raise OperationNotAllowed( - f"The config entry {entry.title} ({entry.domain}) with entry_id" - f" {entry.entry_id} cannot forward setup for {platforms} because it" - f" is not loaded in the {entry.state} state" + f"The config entry '{entry.title}' ({entry.domain}) with " + f"entry_id '{entry.entry_id}' cannot forward setup for " + f"{platforms} because it is in state {entry.state}, but needs " + f"to be in the {ConfigEntryState.LOADED} state" ) await self._async_forward_entry_setups_locked(entry, platforms) else: @@ -2108,9 +2109,10 @@ class ConfigEntries: async with entry.setup_lock: if entry.state is not ConfigEntryState.LOADED: raise OperationNotAllowed( - f"The config entry {entry.title} ({entry.domain}) with entry_id" - f" {entry.entry_id} cannot forward setup for {domain} because it" - f" is not loaded in the {entry.state} state" + f"The config entry '{entry.title}' ({entry.domain}) with " + f"entry_id '{entry.entry_id}' cannot forward setup for " + f"{domain} because it is in state {entry.state}, but needs " + f"to be in the {ConfigEntryState.LOADED} state" ) return await self._async_forward_entry_setup(entry, domain, True) result = await self._async_forward_entry_setup(entry, domain, True) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index b23b247b7a3..cba7ad8f215 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -5606,9 +5606,10 @@ async def test_config_entry_unloaded_during_platform_setups( del task assert ( - "OperationNotAllowed: The config entry Mock Title (test) with " - "entry_id test2 cannot forward setup for ['light'] because it is " - "not loaded in the ConfigEntryState.NOT_LOADED state" + "OperationNotAllowed: The config entry 'Mock Title' (test) with " + "entry_id 'test2' cannot forward setup for ['light'] because it is " + "in state ConfigEntryState.NOT_LOADED, but needs to be in the " + "ConfigEntryState.LOADED state" ) in caplog.text @@ -5824,9 +5825,10 @@ async def test_config_entry_unloaded_during_platform_setup( del task assert ( - "OperationNotAllowed: The config entry Mock Title (test) with " - "entry_id test2 cannot forward setup for light because it is " - "not loaded in the ConfigEntryState.NOT_LOADED state" + "OperationNotAllowed: The config entry 'Mock Title' (test) with " + "entry_id 'test2' cannot forward setup for light because it is " + "in state ConfigEntryState.NOT_LOADED, but needs to be in the " + "ConfigEntryState.LOADED state" ) in caplog.text