Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,27 @@ async def async_setup(
"%s.async_setup_entry did not return boolean", integration.domain
)
result = False
except ConfigEntryNotReady:
except ConfigEntryNotReady as ex:
self.state = ENTRY_STATE_SETUP_RETRY
wait_time = 2 ** min(tries, 4) * 5
tries += 1
message = str(ex)
if not message and ex.__cause__:
message = str(ex.__cause__)
ready_message = f"ready yet: {message}" if message else "ready yet"
if tries == 1:
_LOGGER.warning(
"Config entry '%s' for %s integration not ready yet. Retrying in background",
"Config entry '%s' for %s integration not %s; Retrying in background",
self.title,
self.domain,
ready_message,
)
else:
_LOGGER.debug(
"Config entry '%s' for %s integration not ready yet. Retrying in %d seconds",
"Config entry '%s' for %s integration not %s; Retrying in %d seconds",
self.title,
self.domain,
ready_message,
wait_time,
)

Expand Down
31 changes: 29 additions & 2 deletions tests/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,15 +795,20 @@ async def test_setup_raise_not_ready(hass, caplog):
"""Test a setup raising not ready."""
entry = MockConfigEntry(title="test_title", domain="test")

mock_setup_entry = AsyncMock(side_effect=ConfigEntryNotReady)
mock_setup_entry = AsyncMock(
side_effect=ConfigEntryNotReady("The internet connection is offline")
)
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, "config_flow.test", None)

with patch("homeassistant.helpers.event.async_call_later") as mock_call:
await entry.async_setup(hass)

assert len(mock_call.mock_calls) == 1
assert "Config entry 'test_title' for test integration not ready yet" in caplog.text
assert (
"Config entry 'test_title' for test integration not ready yet: The internet connection is offline"
in caplog.text
)
p_hass, p_wait_time, p_setup = mock_call.mock_calls[0][1]

assert p_hass is hass
Expand All @@ -817,6 +822,28 @@ async def test_setup_raise_not_ready(hass, caplog):
assert entry.state == config_entries.ENTRY_STATE_LOADED


async def test_setup_raise_not_ready_from_exception(hass, caplog):
"""Test a setup raising not ready from another exception."""
entry = MockConfigEntry(title="test_title", domain="test")

original_exception = HomeAssistantError("The device dropped the connection")
config_entry_exception = ConfigEntryNotReady()
config_entry_exception.__cause__ = original_exception

mock_setup_entry = AsyncMock(side_effect=config_entry_exception)
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, "config_flow.test", None)

with patch("homeassistant.helpers.event.async_call_later") as mock_call:
await entry.async_setup(hass)

assert len(mock_call.mock_calls) == 1
assert (
"Config entry 'test_title' for test integration not ready yet: The device dropped the connection"
in caplog.text
)


async def test_setup_retrying_during_unload(hass):
"""Test if we unload an entry that is in retry mode."""
entry = MockConfigEntry(domain="test")
Expand Down