Use mock_config_flow helper in config_entries tests (#126251)

This commit is contained in:
Erik Montnemery 2024-09-19 13:57:27 +02:00 committed by GitHub
parent 31adb048f1
commit 7ba9d1fe65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -675,7 +675,7 @@ async def test_add_entry_calls_setup_entry(
"""Test user step."""
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
)
@ -866,7 +866,7 @@ async def test_saving_and_loading(
await self.async_set_unique_id("unique")
return self.async_create_entry(title="Test Title", data={"token": "abcd"})
with patch.dict(config_entries.HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_USER}
)
@ -1059,23 +1059,20 @@ async def test_discovery_notification(
mock_integration(hass, MockModule("test"))
mock_platform(hass, "test.config_flow", None)
with patch.dict(config_entries.HANDLERS):
class TestFlow(config_entries.ConfigFlow):
"""Test flow."""
class TestFlow(config_entries.ConfigFlow, domain="test"):
"""Test flow."""
VERSION = 5
VERSION = 5
async def async_step_discovery(self, discovery_info):
"""Test discovery step."""
return self.async_show_form(step_id="discovery_confirm")
async def async_step_discovery(self, discovery_info):
"""Test discovery step."""
return self.async_show_form(step_id="discovery_confirm")
async def async_step_discovery_confirm(self, discovery_info):
"""Test discovery confirm step."""
return self.async_create_entry(
title="Test Title", data={"token": "abcd"}
)
async def async_step_discovery_confirm(self, discovery_info):
"""Test discovery confirm step."""
return self.async_create_entry(title="Test Title", data={"token": "abcd"})
with mock_config_flow("test", TestFlow):
notifications = async_get_persistent_notifications(hass)
assert "config_entry_discovery" not in notifications
@ -1113,29 +1110,28 @@ async def test_reauth_notification(hass: HomeAssistant) -> None:
mock_integration(hass, MockModule("test"))
mock_platform(hass, "test.config_flow", None)
with patch.dict(config_entries.HANDLERS):
class TestFlow(config_entries.ConfigFlow):
"""Test flow."""
class TestFlow(config_entries.ConfigFlow, domain="test"):
"""Test flow."""
VERSION = 5
VERSION = 5
async def async_step_user(self, user_input):
"""Test user step."""
return self.async_show_form(step_id="user_confirm")
async def async_step_user(self, user_input):
"""Test user step."""
return self.async_show_form(step_id="user_confirm")
async def async_step_user_confirm(self, user_input):
"""Test user confirm step."""
return self.async_show_form(step_id="user_confirm")
async def async_step_user_confirm(self, user_input):
"""Test user confirm step."""
return self.async_show_form(step_id="user_confirm")
async def async_step_reauth(self, user_input):
"""Test reauth step."""
return self.async_show_form(step_id="reauth_confirm")
async def async_step_reauth(self, user_input):
"""Test reauth step."""
return self.async_show_form(step_id="reauth_confirm")
async def async_step_reauth_confirm(self, user_input):
"""Test reauth confirm step."""
return self.async_abort(reason="test")
async def async_step_reauth_confirm(self, user_input):
"""Test reauth confirm step."""
return self.async_abort(reason="test")
with mock_config_flow("test", TestFlow):
# Start user flow to assert that reconfigure notification doesn't fire
await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_USER}
@ -1235,7 +1231,7 @@ async def test_discovery_notification_not_created(hass: HomeAssistant) -> None:
"""Test discovery step."""
return self.async_abort(reason="test")
with patch.dict(config_entries.HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_DISCOVERY}
)
@ -1570,7 +1566,7 @@ async def test_create_entry_options(
options={"example": user_input["option"]},
)
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
assert await async_setup_component(hass, "comp", {})
await hass.async_block_till_done()
@ -2317,7 +2313,7 @@ async def test_unique_id_persisted(
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}):
with mock_config_flow("comp", TestFlow):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
)
@ -2368,7 +2364,7 @@ async def test_unique_id_existing_entry(
return self.async_create_entry(title="mock-title", data={"via": "flow"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
)
@ -2414,7 +2410,7 @@ async def test_entry_id_existing_entry(
with (
pytest.raises(HomeAssistantError),
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ulid_util.ulid_now",
return_value=collide_entry_id,
@ -2457,7 +2453,7 @@ async def test_unique_id_update_existing_entry_without_reload(
)
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2507,7 +2503,7 @@ async def test_unique_id_update_existing_entry_with_reload(
)
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2527,7 +2523,7 @@ async def test_unique_id_update_existing_entry_with_reload(
updates["host"] = "2.2.2.2"
entry._async_set_state(hass, config_entries.ConfigEntryState.NOT_LOADED, None)
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2584,7 +2580,7 @@ async def test_unique_id_from_discovery_in_setup_retry(
# Verify we do not reload from a user source
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2600,7 +2596,7 @@ async def test_unique_id_from_discovery_in_setup_retry(
# Verify do reload from a discovery source
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2652,7 +2648,7 @@ async def test_unique_id_not_update_existing_entry(
)
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2686,7 +2682,7 @@ async def test_unique_id_in_progress(
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}):
with mock_config_flow("comp", TestFlow):
# Create one to be in progress
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
@ -2726,7 +2722,7 @@ async def test_finish_flow_aborts_progress(
return self.async_create_entry(title="yo", data={})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# Create one to be in progress
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
@ -2761,7 +2757,7 @@ async def test_unique_id_ignore(
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}):
with mock_config_flow("comp", TestFlow):
# Create one to be in progress
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
@ -2825,7 +2821,7 @@ async def test_manual_add_overrides_ignored_entry(
raise NotImplementedError
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -2869,7 +2865,7 @@ async def test_manual_add_overrides_ignored_entry_singleton(
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
)
@ -2910,7 +2906,7 @@ async def test_async_current_entries_does_not_skip_ignore_non_user(
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_IMPORT}
)
@ -2947,7 +2943,7 @@ async def test_async_current_entries_explicit_skip_ignore(
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_IMPORT}
)
@ -2988,7 +2984,7 @@ async def test_async_current_entries_explicit_include_ignore(
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_IMPORT}
)
@ -3016,7 +3012,7 @@ async def test_unignore_step_form(
await self.async_set_unique_id(unique_id)
return self.async_show_form(step_id="discovery")
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp",
context={"source": config_entries.SOURCE_IGNORE},
@ -3059,7 +3055,7 @@ async def test_unignore_create_entry(
await self.async_set_unique_id(unique_id)
return self.async_create_entry(title="yo", data={})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp",
context={"source": config_entries.SOURCE_IGNORE},
@ -3099,7 +3095,7 @@ async def test_unignore_default_impl(
VERSION = 1
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp",
context={"source": config_entries.SOURCE_IGNORE},
@ -3151,7 +3147,7 @@ async def test_partial_flows_hidden(
async def async_step_someform(self, user_input=None):
raise NotImplementedError
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# Start a config entry flow and wait for it to be blocked
init_task = asyncio.ensure_future(
manager.flow.async_init(
@ -3217,7 +3213,7 @@ async def test_async_setup_init_entry(
"""Test import step creating entry."""
return self.async_create_entry(title="title", data={})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
assert await async_setup_component(hass, "comp", {})
await hass.async_block_till_done()
@ -3278,7 +3274,7 @@ async def test_async_setup_init_entry_completes_before_loaded_event_fires(
# This test must not use hass.async_block_till_done()
# as its explicitly testing what happens without it
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
assert await async_setup_component(hass, "comp", {})
assert len(async_setup_entry.mock_calls) == 1
assert load_events[0].event_type == EVENT_COMPONENT_LOADED
@ -3334,7 +3330,7 @@ async def test_async_setup_update_entry(hass: HomeAssistant) -> None:
)
return self.async_abort(reason="yo")
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
assert await async_setup_component(hass, "comp", {})
entries = hass.config_entries.async_entries("comp")
@ -3383,7 +3379,7 @@ async def test_flow_with_default_discovery(
return self.async_create_entry(title="yo", data={})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# Create one to be in progress
result = await manager.flow.async_init(
"comp", context={"source": discovery_source[0]}, data=discovery_source[1]
@ -3433,7 +3429,7 @@ async def test_flow_with_default_discovery_with_unique_id(
async def async_step_mock(self, user_input=None):
raise NotImplementedError
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
)
@ -3460,7 +3456,7 @@ async def test_default_discovery_abort_existing_entries(
VERSION = 1
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
)
@ -3489,7 +3485,7 @@ async def test_default_discovery_in_progress(
async def async_step_mock(self, user_input=None):
raise NotImplementedError
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
result = await manager.flow.async_init(
"comp",
context={"source": config_entries.SOURCE_DISCOVERY},
@ -3529,7 +3525,7 @@ async def test_default_discovery_abort_on_new_unique_flow(
async def async_step_mock(self, user_input=None):
raise NotImplementedError
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# First discovery with default, no unique ID
result2 = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
@ -3576,7 +3572,7 @@ async def test_default_discovery_abort_on_user_flow_complete(
async def async_step_mock(self, user_input=None):
raise NotImplementedError
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# First discovery with default, no unique ID
flow1 = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
@ -3640,7 +3636,7 @@ async def test_flow_same_device_multiple_sources(
return self.async_show_form(step_id="link")
return self.async_create_entry(title="title", data={"token": "supersecret"})
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
with mock_config_flow("comp", TestFlow):
# Create one to be in progress
flow1 = manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_ZEROCONF}
@ -4159,7 +4155,7 @@ async def test_async_abort_entries_match(
self._async_abort_entries_match(matchers)
return self.async_abort(reason="no_match")
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
result = await manager.flow.async_init(
"comp", context={"source": config_entries.SOURCE_USER}
)
@ -4455,7 +4451,7 @@ async def test_unique_id_update_while_setup_in_progress(
)
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as async_reload,
@ -5023,7 +5019,7 @@ async def test_update_entry_and_reload(
**kwargs,
)
with patch.dict(config_entries.HANDLERS, {"comp": MockFlowHandler}):
with mock_config_flow("comp", MockFlowHandler):
task = await manager.flow.async_init("comp", context={"source": "reauth"})
await hass.async_block_till_done()
@ -5305,7 +5301,7 @@ async def test_avoid_adding_second_config_entry_on_single_config_entry(
"homeassistant.loader.async_get_integration",
return_value=integration,
),
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
):
# Start a flow
result = await manager.flow.async_init(
@ -5364,7 +5360,7 @@ async def test_in_progress_get_canceled_when_entry_is_created(
return self.async_show_form(step_id="user")
with (
patch.dict(config_entries.HANDLERS, {"comp": TestFlow}),
mock_config_flow("comp", TestFlow),
patch(
"homeassistant.loader.async_get_integration",
return_value=integration,