From 71acb2c665abeb93494408fab5f39ac348ad3f98 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 24 Aug 2020 10:54:26 +0200 Subject: [PATCH] Only reload config entry if it is loaded (#39202) --- homeassistant/config_entries.py | 6 +++++- tests/components/volumio/test_config_flow.py | 1 + tests/test_config_entries.py | 21 +++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 90d9c623fac..9bfc9a1f1d0 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -885,7 +885,11 @@ class ConfigFlow(data_entry_flow.FlowHandler): changed = self.hass.config_entries.async_update_entry( entry, data={**entry.data, **updates} ) - if changed and reload_on_update: + if ( + changed + and reload_on_update + and entry.state in (ENTRY_STATE_LOADED, ENTRY_STATE_SETUP_RETRY) + ): self.hass.async_create_task( self.hass.config_entries.async_reload(entry.entry_id) ) diff --git a/tests/components/volumio/test_config_flow.py b/tests/components/volumio/test_config_flow.py index 6fc7390da79..b0532ce55d8 100644 --- a/tests/components/volumio/test_config_flow.py +++ b/tests/components/volumio/test_config_flow.py @@ -239,6 +239,7 @@ async def test_discovery_updates_unique_id(hass): "name": "dummy", "id": TEST_DISCOVERY_RESULT["id"], }, + state=config_entries.ENTRY_STATE_SETUP_RETRY, ) entry.add_to_hass(hass) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index ab28ecc7af3..644987de43c 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -1169,6 +1169,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): domain="comp", data={"additional": "data", "host": "0.0.0.0"}, unique_id="mock-unique-id", + state=config_entries.ENTRY_STATE_LOADED, ) entry.add_to_hass(hass) @@ -1176,6 +1177,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): hass, MockModule("comp"), ) mock_entity_platform(hass, "config_flow.comp", None) + updates = {"host": "1.1.1.1"} class TestFlow(config_entries.ConfigFlow): """Test flow.""" @@ -1186,7 +1188,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): """Test user step.""" await self.async_set_unique_id("mock-unique-id") await self._abort_if_unique_id_configured( - updates={"host": "1.1.1.1"}, reload_on_update=True + updates=updates, reload_on_update=True ) with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch( @@ -1203,6 +1205,23 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): assert entry.data["additional"] == "data" assert len(async_reload.mock_calls) == 1 + # Test we don't reload if entry not started + updates["host"] = "2.2.2.2" + entry.state = config_entries.ENTRY_STATE_NOT_LOADED + with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch( + "homeassistant.config_entries.ConfigEntries.async_reload" + ) as async_reload: + result = await manager.flow.async_init( + "comp", context={"source": config_entries.SOURCE_USER} + ) + await hass.async_block_till_done() + + assert result["type"] == "abort" + assert result["reason"] == "already_configured" + assert entry.data["host"] == "2.2.2.2" + assert entry.data["additional"] == "data" + assert len(async_reload.mock_calls) == 0 + async def test_unique_id_not_update_existing_entry(hass, manager): """Test that we do not update an entry if existing entry has the data."""