Skip to content

Commit 37e49c2

Browse files
committed
Adjust and add tests
1 parent 873d8a2 commit 37e49c2

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

homeassistant/config_entries.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1482,11 +1482,11 @@ async def async_finish_flow(
14821482
)
14831483

14841484
if existing_entry is not None and flow.handler != "mobile_app":
1485-
# `mobile_app` does this on purpose
1485+
# This causes the old entry to be removed and replaced when it
1486+
# should most likely update the previous entry and abort the flow
14861487
report_usage(
14871488
"creates a config entry when another entry with the same unique ID "
1488-
"exists, causing the old entry to be removed and replaced when it "
1489-
"should most likely update the previous entry and abort the flow",
1489+
"exists",
14901490
core_behavior=ReportBehavior.LOG,
14911491
core_integration_behavior=ReportBehavior.LOG,
14921492
custom_integration_behavior=ReportBehavior.IGNORE,

tests/test_config_entries.py

+60
Original file line numberDiff line numberDiff line change
@@ -7616,3 +7616,63 @@ async def test_add_description_placeholder_automatically_not_overwrites(
76167616
result = await hass.config_entries.flow.async_configure(flows[0]["flow_id"], None)
76177617
assert result["type"] == FlowResultType.FORM
76187618
assert result["description_placeholders"] == {"name": "Custom title"}
7619+
7620+
7621+
@pytest.mark.parametrize(
7622+
("domain", "expected_log"),
7623+
[
7624+
("some_integration", True),
7625+
("mobile_app", False),
7626+
],
7627+
)
7628+
async def test_create_entry_existing_unique_id(
7629+
hass: HomeAssistant,
7630+
domain: str,
7631+
expected_log: bool,
7632+
caplog: pytest.LogCaptureFixture,
7633+
) -> None:
7634+
"""Test to highlight unexpected behavior on create_entry."""
7635+
entry = MockConfigEntry(
7636+
title="From config flow",
7637+
domain=domain,
7638+
entry_id="01J915Q6T9F6G5V0QJX6HBC94T",
7639+
data={"host": "any", "port": 123},
7640+
unique_id="mock-unique-id",
7641+
)
7642+
entry.add_to_hass(hass)
7643+
7644+
assert len(hass.config_entries.async_entries(domain)) == 1
7645+
7646+
mock_setup_entry = AsyncMock(return_value=True)
7647+
7648+
mock_integration(hass, MockModule(domain, async_setup_entry=mock_setup_entry))
7649+
mock_platform(hass, f"{domain}.config_flow", None)
7650+
7651+
class TestFlow(config_entries.ConfigFlow):
7652+
"""Test flow."""
7653+
7654+
VERSION = 1
7655+
7656+
async def async_step_user(self, user_input=None):
7657+
"""Test user step."""
7658+
await self.async_set_unique_id("mock-unique-id")
7659+
return self.async_create_entry(title="mock-title", data={})
7660+
7661+
with (
7662+
mock_config_flow(domain, TestFlow),
7663+
patch.object(frame, "_REPORTED_INTEGRATIONS", set()),
7664+
):
7665+
result = await hass.config_entries.flow.async_init(
7666+
domain, context={"source": config_entries.SOURCE_USER}
7667+
)
7668+
await hass.async_block_till_done()
7669+
assert result["type"] is FlowResultType.CREATE_ENTRY
7670+
7671+
assert len(hass.config_entries.async_entries(domain)) == 1
7672+
7673+
log_text = (
7674+
f"Detected that integration '{domain}' creates a config entry "
7675+
"when another entry with the same unique ID exists. Please "
7676+
"create a bug report at https:"
7677+
)
7678+
assert (log_text in caplog.text) == expected_log

0 commit comments

Comments
 (0)