Add unique ID to config entries (#29806)
* Add unique ID to config entries * Unload existing entries with same unique ID if flow with unique ID is finished * Remove unused exception * Fix typing * silence pylint * Fix tests * Add unique ID to Hue * Address typing comment * Tweaks to comments * lint
This commit is contained in:
1 parent
87ca61ddd7
commit
d851cb6f9e
10 files changed
+305
-46
No files matched your search
@@ -671,6 +671,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||
options={},
|
||||
system_options={},
|
||||
connection_class=config_entries.CONN_CLASS_UNKNOWN,
|
||||
unique_id=None,
|
||||
):
|
||||
"""Initialize a mock config entry."""
|
||||
kwargs = {
|
||||
@@ -682,6 +683,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||
"version": version,
|
||||
"title": title,
|
||||
"connection_class": connection_class,
|
||||
"unique_id": unique_id,
|
||||
}
|
||||
if source is not None:
|
||||
kwargs["source"] = source
|
||||
|
||||
@@ -19,6 +19,7 @@ async def test_flow_works(hass, aioclient_mock):
|
||||
|
||||
flow = config_flow.HueFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {}
|
||||
await flow.async_step_init()
|
||||
|
||||
with patch("aiohue.Bridge") as mock_bridge:
|
||||
@@ -349,28 +350,33 @@ async def test_creating_entry_removes_entries_for_same_host_or_bridge(hass):
|
||||
accessible via a single IP. So when we create a new entry, we'll remove
|
||||
all existing entries that either have same IP or same bridge_id.
|
||||
"""
|
||||
MockConfigEntry(
|
||||
domain="hue", data={"host": "0.0.0.0", "bridge_id": "id-1234"}
|
||||
).add_to_hass(hass)
|
||||
orig_entry = MockConfigEntry(
|
||||
domain="hue",
|
||||
data={"host": "0.0.0.0", "bridge_id": "id-1234"},
|
||||
unique_id="id-1234",
|
||||
)
|
||||
orig_entry.add_to_hass(hass)
|
||||
|
||||
MockConfigEntry(
|
||||
domain="hue", data={"host": "1.2.3.4", "bridge_id": "id-1234"}
|
||||
domain="hue",
|
||||
data={"host": "1.2.3.4", "bridge_id": "id-5678"},
|
||||
unique_id="id-5678",
|
||||
).add_to_hass(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries("hue")) == 2
|
||||
|
||||
flow = config_flow.HueFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {}
|
||||
|
||||
bridge = Mock()
|
||||
bridge.username = "username-abc"
|
||||
bridge.config.bridgeid = "id-1234"
|
||||
bridge.config.name = "Mock Bridge"
|
||||
bridge.host = "0.0.0.0"
|
||||
|
||||
with patch.object(config_flow, "get_bridge", return_value=mock_coro(bridge)):
|
||||
result = await flow.async_step_import({"host": "0.0.0.0"})
|
||||
with patch.object(
|
||||
config_flow, "_find_username_from_config", return_value="mock-user"
|
||||
), patch.object(config_flow, "get_bridge", return_value=mock_coro(bridge)):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"hue", data={"host": "2.2.2.2"}, context={"source": "import"}
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "Mock Bridge"
|
||||
@@ -379,9 +385,11 @@ async def test_creating_entry_removes_entries_for_same_host_or_bridge(hass):
|
||||
"bridge_id": "id-1234",
|
||||
"username": "username-abc",
|
||||
}
|
||||
# We did not process the result of this entry but already removed the old
|
||||
# ones. So we should have 0 entries.
|
||||
assert len(hass.config_entries.async_entries("hue")) == 0
|
||||
entries = hass.config_entries.async_entries("hue")
|
||||
assert len(entries) == 2
|
||||
new_entry = entries[-1]
|
||||
assert orig_entry.entry_id != new_entry.entry_id
|
||||
assert new_entry.unique_id == "id-1234"
|
||||
|
||||
|
||||
async def test_bridge_homekit(hass):
|
||||
@@ -398,6 +406,7 @@ async def test_bridge_homekit(hass):
|
||||
"host": "0.0.0.0",
|
||||
"serial": "1234",
|
||||
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
|
||||
"properties": {"id": "aa:bb:cc:dd:ee:ff"},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -175,3 +175,19 @@ async def test_unload_entry(hass):
|
||||
assert await hue.async_unload_entry(hass, entry)
|
||||
assert len(mock_bridge.return_value.async_reset.mock_calls) == 1
|
||||
assert hass.data[hue.DOMAIN] == {}
|
||||
|
||||
|
||||
async def test_setting_unique_id(hass):
|
||||
"""Test we set unique ID if not set yet."""
|
||||
entry = MockConfigEntry(domain=hue.DOMAIN, data={"host": "0.0.0.0"})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(hue, "HueBridge") as mock_bridge, patch(
|
||||
"homeassistant.helpers.device_registry.async_get_registry",
|
||||
return_value=mock_coro(Mock()),
|
||||
):
|
||||
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_bridge.return_value.api.config = Mock(bridgeid="mock-id")
|
||||
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
|
||||
|
||||
assert entry.unique_id == "mock-id"
|
||||
@@ -1001,3 +1001,110 @@ async def test_reload_entry_entity_registry_works(hass):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_unload_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_unqiue_id_persisted(hass, manager):
|
||||
"""Test that a unique ID is stored in the config entry."""
|
||||
mock_setup_entry = MagicMock(return_value=mock_coro(True))
|
||||
|
||||
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
await self.async_set_unique_id("mock-unique-id")
|
||||
return self.async_create_entry(title="mock-title", data={})
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
p_hass, p_entry = mock_setup_entry.mock_calls[0][1]
|
||||
|
||||
assert p_hass is hass
|
||||
assert p_entry.unique_id == "mock-unique-id"
|
||||
|
||||
|
||||
async def test_unique_id_existing_entry(hass, manager):
|
||||
"""Test that we remove an entry if there already is an entry with unique ID."""
|
||||
hass.config.components.add("comp")
|
||||
MockConfigEntry(
|
||||
domain="comp",
|
||||
state=config_entries.ENTRY_STATE_LOADED,
|
||||
unique_id="mock-unique-id",
|
||||
).add_to_hass(hass)
|
||||
|
||||
async_setup_entry = MagicMock(side_effect=lambda _, _2: mock_coro(True))
|
||||
async_unload_entry = MagicMock(side_effect=lambda _, _2: mock_coro(True))
|
||||
async_remove_entry = MagicMock(side_effect=lambda _, _2: mock_coro(True))
|
||||
|
||||
mock_integration(
|
||||
hass,
|
||||
MockModule(
|
||||
"comp",
|
||||
async_setup_entry=async_setup_entry,
|
||||
async_unload_entry=async_unload_entry,
|
||||
async_remove_entry=async_remove_entry,
|
||||
),
|
||||
)
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
existing_entry = await self.async_set_unique_id("mock-unique-id")
|
||||
|
||||
assert existing_entry is not None
|
||||
|
||||
return self.async_create_entry(title="mock-title", data={"via": "flow"})
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
result = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
entries = hass.config_entries.async_entries("comp")
|
||||
assert len(entries) == 1
|
||||
assert entries[0].data == {"via": "flow"}
|
||||
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
assert len(async_unload_entry.mock_calls) == 1
|
||||
assert len(async_remove_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_unique_id_in_progress(hass, manager):
|
||||
"""Test that we abort if there is already a flow in progress with same unique id."""
|
||||
mock_integration(hass, MockModule("comp"))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
await self.async_set_unique_id("mock-unique-id")
|
||||
return self.async_show_form(step_id="discovery")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
# Create one to be in progress
|
||||
result = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
# Will be canceled
|
||||
result2 = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result2["reason"] == "already_in_progress"
|
||||
@@ -94,7 +94,7 @@ async def test_configure_two_steps(manager):
|
||||
|
||||
|
||||
async def test_show_form(manager):
|
||||
"""Test that abort removes the flow from progress."""
|
||||
"""Test that we can show a form."""
|
||||
schema = vol.Schema({vol.Required("username"): str, vol.Required("password"): str})
|
||||
|
||||
@manager.mock_reg_handler("test")
|
||||
@@ -271,3 +271,17 @@ async def test_external_step(hass, manager):
|
||||
result = await manager.async_configure(result["flow_id"])
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "Hello"
|
||||
|
||||
|
||||
async def test_abort_flow_exception(manager):
|
||||
"""Test that the AbortFlow exception works."""
|
||||
|
||||
@manager.mock_reg_handler("test")
|
||||
class TestFlow(data_entry_flow.FlowHandler):
|
||||
async def async_step_init(self, user_input=None):
|
||||
raise data_entry_flow.AbortFlow("mock-reason", {"placeholder": "yo"})
|
||||
|
||||
form = await manager.async_init("test")
|
||||
assert form["type"] == "abort"
|
||||
assert form["reason"] == "mock-reason"
|
||||
assert form["description_placeholders"] == {"placeholder": "yo"}
|
||||
Reference in new issue
Block a user