Only reload config entry if it is loaded (#39202)

This commit is contained in:
Paulus Schoutsen 2020-08-24 10:54:26 +02:00 committed by GitHub
parent e17c87ef72
commit 71acb2c665
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -885,7 +885,11 @@ class ConfigFlow(data_entry_flow.FlowHandler):
changed = self.hass.config_entries.async_update_entry( changed = self.hass.config_entries.async_update_entry(
entry, data={**entry.data, **updates} 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.async_create_task(
self.hass.config_entries.async_reload(entry.entry_id) self.hass.config_entries.async_reload(entry.entry_id)
) )

View file

@ -239,6 +239,7 @@ async def test_discovery_updates_unique_id(hass):
"name": "dummy", "name": "dummy",
"id": TEST_DISCOVERY_RESULT["id"], "id": TEST_DISCOVERY_RESULT["id"],
}, },
state=config_entries.ENTRY_STATE_SETUP_RETRY,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)

View file

@ -1169,6 +1169,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager):
domain="comp", domain="comp",
data={"additional": "data", "host": "0.0.0.0"}, data={"additional": "data", "host": "0.0.0.0"},
unique_id="mock-unique-id", unique_id="mock-unique-id",
state=config_entries.ENTRY_STATE_LOADED,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -1176,6 +1177,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager):
hass, MockModule("comp"), hass, MockModule("comp"),
) )
mock_entity_platform(hass, "config_flow.comp", None) mock_entity_platform(hass, "config_flow.comp", None)
updates = {"host": "1.1.1.1"}
class TestFlow(config_entries.ConfigFlow): class TestFlow(config_entries.ConfigFlow):
"""Test flow.""" """Test flow."""
@ -1186,7 +1188,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager):
"""Test user step.""" """Test user step."""
await self.async_set_unique_id("mock-unique-id") await self.async_set_unique_id("mock-unique-id")
await self._abort_if_unique_id_configured( 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( 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 entry.data["additional"] == "data"
assert len(async_reload.mock_calls) == 1 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): 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.""" """Test that we do not update an entry if existing entry has the data."""