Fix FlowHandler show progress (#108586)

This commit is contained in:
Erik Montnemery 2024-01-21 22:40:48 +01:00 committed by GitHub
parent 0566ceca0f
commit e90b42d3d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 10 deletions

View File

@ -316,6 +316,9 @@ class FlowManager(abc.ABC):
result: FlowResult | None = None
while not result or result["type"] == FlowResultType.SHOW_PROGRESS_DONE:
result = await self._async_configure(flow_id, user_input)
flow = self._progress.get(flow_id)
if flow and flow.deprecated_show_progress:
break
return result
async def _async_configure(
@ -540,6 +543,7 @@ class FlowHandler:
__progress_task: asyncio.Task[Any] | None = None
__no_progress_task_reported = False
deprecated_show_progress = False
@property
def source(self) -> str | None:
@ -710,6 +714,9 @@ class FlowHandler:
report_issue,
)
if progress_task is None:
self.deprecated_show_progress = True
flow_result = FlowResult(
type=FlowResultType.SHOW_PROGRESS,
flow_id=self.flow_id,

View File

@ -511,29 +511,31 @@ async def test_show_progress_hidden_from_frontend(hass: HomeAssistant, manager)
"""Test show progress done is not sent to frontend."""
manager.hass = hass
async_show_progress_done_called = False
progress_task: asyncio.Task[None] | None = None
@manager.mock_reg_handler("test")
class TestFlow(data_entry_flow.FlowHandler):
VERSION = 5
data = None
progress_task: asyncio.Task[None] | None = None
async def async_step_init(self, user_input=None):
nonlocal progress_task
async def long_running_job() -> None:
return
if not self.progress_task:
self.progress_task = hass.async_create_task(long_running_job())
if self.progress_task.done():
if not progress_task:
progress_task = hass.async_create_task(long_running_job())
if progress_task.done():
nonlocal async_show_progress_done_called
async_show_progress_done_called = True
return self.async_show_progress_done(next_step_id="finish")
return self.async_show_progress(
step_id="init",
progress_action="task",
# Set to None to simulate flow manager has not yet called when
# frontend loads
progress_task=None,
# Set to a task which never finishes to simulate flow manager has not
# yet called when frontend loads
progress_task=hass.async_create_task(asyncio.Event().wait()),
)
async def async_step_finish(self, user_input=None):
@ -546,7 +548,7 @@ async def test_show_progress_hidden_from_frontend(hass: HomeAssistant, manager)
assert len(manager.async_progress_by_handler("test")) == 1
assert manager.async_get(result["flow_id"])["handler"] == "test"
await hass.async_block_till_done()
await progress_task
assert not async_show_progress_done_called
# Frontend refreshes the flow
@ -628,8 +630,14 @@ async def test_show_progress_legacy(hass: HomeAssistant, manager, caplog) -> Non
result = await manager.async_configure(
result["flow_id"], {"task_finished": 2, "title": "Hello"}
)
# Note: The SHOW_PROGRESS_DONE is hidden from frontend; FlowManager automatically
# calls the flow again
# Note: The SHOW_PROGRESS_DONE is not hidden from frontend when flows manage
# the progress tasks themselves
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS_DONE
# Frontend refreshes the flow
result = await manager.async_configure(
result["flow_id"], {"task_finished": 2, "title": "Hello"}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "Hello"