1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Add support for reload_on_update to _abort_if_unique_id_configured (#38638)

* Add support for reload_on_update to _abort_if_unique_id_configured

async_update_entry now avoids firing update listeners and writing
the storage if there are no actual changes.

* Actually add the tests

* collapse branch

* Update homeassistant/config_entries.py

Co-authored-by: Franck Nijhof <git@frenck.dev>

* handle entries that lack the ability to reload

* reduce

* adjust konnected tests

* update axis tests

* fix blocking

* more mocking

* config flow tests outside of test_config_flow

* reduce

* volumio

* Update homeassistant/config_entries.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* set reload_on_update=False for integrations that implement self._abort_if_unique_id_configured(updates= and a reload listen

* get rid of copy

* revert test change

Co-authored-by: Franck Nijhof <git@frenck.dev>
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
J. Nick Koston
2020-08-08 13:23:56 -05:00
committed by GitHub
parent 1cb161017e
commit 74c23c3e96
9 changed files with 272 additions and 82 deletions

View File

@@ -624,10 +624,10 @@ async def test_updating_entry_data(manager):
)
entry.add_to_manager(manager)
manager.async_update_entry(entry)
assert manager.async_update_entry(entry) is False
assert entry.data == {"first": True}
manager.async_update_entry(entry, data={"second": True})
assert manager.async_update_entry(entry, data={"second": True}) is True
assert entry.data == {"second": True}
@@ -658,7 +658,7 @@ async def test_update_entry_options_and_trigger_listener(hass, manager):
entry.add_update_listener(update_listener)
manager.async_update_entry(entry, options={"second": True})
assert manager.async_update_entry(entry, options={"second": True}) is True
assert entry.options == {"second": True}
@@ -1120,7 +1120,7 @@ async def test_unique_id_existing_entry(hass, manager):
assert len(async_remove_entry.mock_calls) == 1
async def test_unique_id_update_existing_entry(hass, manager):
async def test_unique_id_update_existing_entry_without_reload(hass, manager):
"""Test that we update an entry if there already is an entry with unique ID."""
hass.config.components.add("comp")
entry = MockConfigEntry(
@@ -1143,17 +1143,65 @@ async def test_unique_id_update_existing_entry(hass, manager):
async def async_step_user(self, user_input=None):
"""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"})
await self._abort_if_unique_id_configured(
updates={"host": "1.1.1.1"}, reload_on_update=False
)
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
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"] == "1.1.1.1"
assert entry.data["additional"] == "data"
assert len(async_reload.mock_calls) == 0
async def test_unique_id_update_existing_entry_with_reload(hass, manager):
"""Test that we update an entry if there already is an entry with unique ID and we reload on changes."""
hass.config.components.add("comp")
entry = MockConfigEntry(
domain="comp",
data={"additional": "data", "host": "0.0.0.0"},
unique_id="mock-unique-id",
)
entry.add_to_hass(hass)
mock_integration(
hass, MockModule("comp"),
)
mock_entity_platform(hass, "config_flow.comp", None)
class TestFlow(config_entries.ConfigFlow):
"""Test flow."""
VERSION = 1
async def async_step_user(self, user_input=None):
"""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
)
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"] == "1.1.1.1"
assert entry.data["additional"] == "data"
assert len(async_reload.mock_calls) == 1
async def test_unique_id_not_update_existing_entry(hass, manager):
@@ -1179,20 +1227,23 @@ async def test_unique_id_not_update_existing_entry(hass, manager):
async def async_step_user(self, user_input=None):
"""Test user step."""
await self.async_set_unique_id("mock-unique-id")
await self._abort_if_unique_id_configured(updates={"host": "0.0.0.0"})
await self._abort_if_unique_id_configured(
updates={"host": "0.0.0.0"}, reload_on_update=True
)
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
"homeassistant.config_entries.ConfigEntries.async_update_entry"
) as async_update_entry:
"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"] == "0.0.0.0"
assert entry.data["additional"] == "data"
assert len(async_update_entry.mock_calls) == 0
assert len(async_reload.mock_calls) == 0
async def test_unique_id_in_progress(hass, manager):
@@ -1567,8 +1618,11 @@ async def test_async_setup_update_entry(hass):
async def async_step_import(self, user_input):
"""Test import step updating existing entry."""
self.hass.config_entries.async_update_entry(
entry, data={"value": "updated"}
assert (
self.hass.config_entries.async_update_entry(
entry, data={"value": "updated"}
)
is True
)
return self.async_abort(reason="yo")
@@ -1758,3 +1812,60 @@ async def test_default_discovery_abort_on_new_unique_flow(hass, manager):
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
async def test_updating_entry_with_and_without_changes(manager):
"""Test that we can update an entry data."""
entry = MockConfigEntry(
domain="test",
data={"first": True},
title="thetitle",
options={"option": True},
unique_id="abc123",
state=config_entries.ENTRY_STATE_SETUP_ERROR,
)
entry.add_to_manager(manager)
assert manager.async_update_entry(entry) is False
assert manager.async_update_entry(entry, data={"second": True}) is True
assert manager.async_update_entry(entry, data={"second": True}) is False
assert (
manager.async_update_entry(entry, data={"second": True, "third": 456}) is True
)
assert (
manager.async_update_entry(entry, data={"second": True, "third": 456}) is False
)
assert manager.async_update_entry(entry, options={"second": True}) is True
assert manager.async_update_entry(entry, options={"second": True}) is False
assert (
manager.async_update_entry(entry, options={"second": True, "third": "123"})
is True
)
assert (
manager.async_update_entry(entry, options={"second": True, "third": "123"})
is False
)
assert (
manager.async_update_entry(entry, system_options={"disable_new_entities": True})
is True
)
assert (
manager.async_update_entry(entry, system_options={"disable_new_entities": True})
is False
)
assert (
manager.async_update_entry(
entry, system_options={"disable_new_entities": False}
)
is True
)
assert (
manager.async_update_entry(
entry, system_options={"disable_new_entities": False}
)
is False
)
assert manager.async_update_entry(entry, title="thetitle") is False
assert manager.async_update_entry(entry, title="newtitle") is True
assert manager.async_update_entry(entry, unique_id="abc123") is False
assert manager.async_update_entry(entry, unique_id="abc1234") is True