From 4478f64002fc0239725b4bef75c1513099a96b98 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:35:04 +0200 Subject: [PATCH] Remove dead reconfigure code (#127398) * Remove dead reconfigure code * Adjust * Start cleaning up test * Prevent duplicate flows * Add missing string * Adjust two more tests * Only filter out reauth flows * Update strings.json * Update config_entries.py * Adjust tests * Remove all checks - but add comment in tests * Simplify PR --- homeassistant/config_entries.py | 46 --------------- tests/test_config_entries.py | 99 +++++++++++++++++---------------- 2 files changed, 50 insertions(+), 95 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index c4ead1bbf0d..84771509c95 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -326,7 +326,6 @@ class ConfigEntry(Generic[_DataT]): _on_unload: list[Callable[[], Coroutine[Any, Any, None] | None]] | None setup_lock: asyncio.Lock _reauth_lock: asyncio.Lock - _reconfigure_lock: asyncio.Lock _tasks: set[asyncio.Future[Any]] _background_tasks: set[asyncio.Future[Any]] _integration_for_domain: loader.Integration | None @@ -430,8 +429,6 @@ class ConfigEntry(Generic[_DataT]): _setter(self, "setup_lock", asyncio.Lock()) # Reauth lock to prevent concurrent reauth flows _setter(self, "_reauth_lock", asyncio.Lock()) - # Reconfigure lock to prevent concurrent reconfigure flows - _setter(self, "_reconfigure_lock", asyncio.Lock()) _setter(self, "_tasks", set()) _setter(self, "_background_tasks", set()) @@ -1094,49 +1091,6 @@ class ConfigEntry(Generic[_DataT]): translation_placeholders={"name": self.title}, ) - @callback - def async_start_reconfigure( - self, - hass: HomeAssistant, - context: ConfigFlowContext | None = None, - data: dict[str, Any] | None = None, - ) -> None: - """Start a reconfigure flow.""" - # We will check this again in the task when we hold the lock, - # but we also check it now to try to avoid creating the task. - if any(self.async_get_active_flows(hass, {SOURCE_RECONFIGURE, SOURCE_REAUTH})): - # Reconfigure or reauth flow already in progress for this entry - return - hass.async_create_task( - self._async_init_reconfigure(hass, context, data), - f"config entry reconfigure {self.title} {self.domain} {self.entry_id}", - ) - - async def _async_init_reconfigure( - self, - hass: HomeAssistant, - context: ConfigFlowContext | None = None, - data: dict[str, Any] | None = None, - ) -> None: - """Start a reconfigure flow.""" - async with self._reconfigure_lock: - if any( - self.async_get_active_flows(hass, {SOURCE_RECONFIGURE, SOURCE_REAUTH}) - ): - # Reconfigure or reauth flow already in progress for this entry - return - await hass.config_entries.flow.async_init( - self.domain, - context=ConfigFlowContext( - source=SOURCE_RECONFIGURE, - entry_id=self.entry_id, - title_placeholders={"name": self.title}, - unique_id=self.unique_id, - ) - | (context or {}), - data=self.data | (data or {}), - ) - @callback def async_get_active_flows( self, hass: HomeAssistant, sources: set[str] diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 997a6231b58..ed350f4d887 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -989,7 +989,6 @@ async def test_as_dict(snapshot: SnapshotAssertion) -> None: "_tries", "_setup_again_job", "_supports_options", - "_reconfigure_lock", "supports_reconfigure", } @@ -4764,38 +4763,67 @@ async def test_reconfigure( await manager.async_setup(entry.entry_id) await hass.async_block_till_done() - flow = hass.config_entries.flow - with patch.object(flow, "async_init", wraps=flow.async_init) as mock_init: - entry.async_start_reconfigure( - hass, - context={"extra_context": "some_extra_context"}, - data={"extra_data": 1234}, + def _async_start_reconfigure(config_entry: MockConfigEntry) -> None: + hass.async_create_task( + manager.flow.async_init( + config_entry.domain, + context={ + "source": config_entries.SOURCE_RECONFIGURE, + "entry_id": config_entry.entry_id, + }, + ), + f"config entry reconfigure {config_entry.title} " + f"{config_entry.domain} {config_entry.entry_id}", ) - await hass.async_block_till_done() + + flow = hass.config_entries.flow + _async_start_reconfigure(entry) + await hass.async_block_till_done() flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 assert flows[0]["context"]["entry_id"] == entry.entry_id assert flows[0]["context"]["source"] == config_entries.SOURCE_RECONFIGURE - assert flows[0]["context"]["title_placeholders"] == {"name": "test_title"} - assert flows[0]["context"]["extra_context"] == "some_extra_context" - - assert mock_init.call_args.kwargs["data"]["extra_data"] == 1234 assert entry.entry_id != entry2.entry_id - # Check that we can't start duplicate reconfigure flows - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) + # Check that we can start duplicate reconfigure flows + # (may need revisiting) + _async_start_reconfigure(entry) await hass.async_block_till_done() - assert len(hass.config_entries.flow.async_progress()) == 1 - - # Check that we can't start duplicate reconfigure flows when the context is different - entry.async_start_reconfigure(hass, {"diff": "diff"}) - await hass.async_block_till_done() - assert len(hass.config_entries.flow.async_progress()) == 1 + assert len(hass.config_entries.flow.async_progress()) == 2 # Check that we can start a reconfigure flow for a different entry - entry2.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) + _async_start_reconfigure(entry2) + await hass.async_block_till_done() + assert len(hass.config_entries.flow.async_progress()) == 3 + + # Abort all existing flows + for flow in hass.config_entries.flow.async_progress(): + hass.config_entries.flow.async_abort(flow["flow_id"]) + await hass.async_block_till_done() + + # Check that we can start duplicate reconfigure flows + # without blocking between flows + # (may need revisiting) + _async_start_reconfigure(entry) + _async_start_reconfigure(entry) + _async_start_reconfigure(entry) + _async_start_reconfigure(entry) + await hass.async_block_till_done() + assert len(hass.config_entries.flow.async_progress()) == 4 + + # Abort all existing flows + for flow in hass.config_entries.flow.async_progress(): + hass.config_entries.flow.async_abort(flow["flow_id"]) + await hass.async_block_till_done() + + # Check that we can start reconfigure flows with active reauth flow + # (may need revisiting) + entry.async_start_reauth(hass, {"extra_context": "some_extra_context"}) + await hass.async_block_till_done() + assert len(hass.config_entries.flow.async_progress()) == 1 + _async_start_reconfigure(entry) await hass.async_block_till_done() assert len(hass.config_entries.flow.async_progress()) == 2 @@ -4804,35 +4832,8 @@ async def test_reconfigure( hass.config_entries.flow.async_abort(flow["flow_id"]) await hass.async_block_till_done() - # Check that we can't start duplicate reconfigure flows - # without blocking between flows - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) - await hass.async_block_till_done() - assert len(hass.config_entries.flow.async_progress()) == 1 - - # Abort all existing flows - for flow in hass.config_entries.flow.async_progress(): - hass.config_entries.flow.async_abort(flow["flow_id"]) - await hass.async_block_till_done() - - # Check that we can't start reconfigure flows with active reauth flow - entry.async_start_reauth(hass, {"extra_context": "some_extra_context"}) - await hass.async_block_till_done() - assert len(hass.config_entries.flow.async_progress()) == 1 - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) - await hass.async_block_till_done() - assert len(hass.config_entries.flow.async_progress()) == 1 - - # Abort all existing flows - for flow in hass.config_entries.flow.async_progress(): - hass.config_entries.flow.async_abort(flow["flow_id"]) - await hass.async_block_till_done() - # Check that we can't start reauth flows with active reconfigure flow - entry.async_start_reconfigure(hass, {"extra_context": "some_extra_context"}) + _async_start_reconfigure(entry) await hass.async_block_till_done() assert len(hass.config_entries.flow.async_progress()) == 1 entry.async_start_reauth(hass, {"extra_context": "some_extra_context"})