mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Reduce boilerplate to abort for matching config entries (#50186)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
@@ -2755,3 +2755,60 @@ async def test_setup_retrying_during_shutdown(hass):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_call.return_value.mock_calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"matchers, reason",
|
||||
[
|
||||
({}, "already_configured"),
|
||||
({"host": "3.3.3.3"}, "no_match"),
|
||||
({"host": "3.4.5.6"}, "already_configured"),
|
||||
({"host": "3.4.5.6", "ip": "3.4.5.6"}, "no_match"),
|
||||
({"host": "3.4.5.6", "ip": "1.2.3.4"}, "already_configured"),
|
||||
({"host": "3.4.5.6", "ip": "1.2.3.4", "port": 23}, "already_configured"),
|
||||
({"ip": "9.9.9.9"}, "already_configured"),
|
||||
({"ip": "7.7.7.7"}, "no_match"), # ignored
|
||||
],
|
||||
)
|
||||
async def test__async_abort_entries_match(hass, manager, matchers, reason):
|
||||
"""Test aborting if matching config entries exist."""
|
||||
MockConfigEntry(
|
||||
domain="comp", data={"ip": "1.2.3.4", "host": "4.5.6.7", "port": 23}
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain="comp", data={"ip": "9.9.9.9", "host": "4.5.6.7", "port": 23}
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain="comp", data={"ip": "1.2.3.4", "host": "3.4.5.6", "port": 23}
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain="comp",
|
||||
source=config_entries.SOURCE_IGNORE,
|
||||
data={"ip": "7.7.7.7", "host": "4.5.6.7", "port": 23},
|
||||
).add_to_hass(hass)
|
||||
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
mock_setup_entry = AsyncMock(return_value=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):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Test user step."""
|
||||
self._async_abort_entries_match(matchers)
|
||||
return self.async_abort(reason="no_match")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
||||
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"] == reason
|
||||
|
||||
Reference in New Issue
Block a user