Config flow progress in percent (#142737)

* Config flow progress in percent

* PR comments
This commit is contained in:
Petar Petrov 2025-04-14 11:24:01 +03:00 committed by GitHub
parent 422bcecec1
commit 9239ace1c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -40,6 +40,7 @@ class FlowResultType(StrEnum):
# Event that is fired when a flow is progressed via external or progress source.
EVENT_DATA_ENTRY_FLOW_PROGRESSED = "data_entry_flow_progressed"
EVENT_DATA_ENTRY_FLOW_PROGRESS_UPDATE = "data_entry_flow_progress_update"
FLOW_NOT_COMPLETE_STEPS = {
FlowResultType.FORM,
@ -829,6 +830,14 @@ class FlowHandler(Generic[_FlowContextT, _FlowResultT, _HandlerT]):
flow_result["step_id"] = step_id
return flow_result
@callback
def async_update_progress(self, progress: float) -> None:
"""Update the progress of a flow. `progress` must be between 0 and 1."""
self.hass.bus.async_fire_internal(
EVENT_DATA_ENTRY_FLOW_PROGRESS_UPDATE,
{"handler": self.handler, "flow_id": self.flow_id, "progress": progress},
)
@callback
def async_show_progress_done(self, *, next_step_id: str) -> _FlowResultT:
"""Mark the progress done."""

View File

@ -464,6 +464,9 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
"""Test show progress logic."""
manager.hass = hass
events = []
progress_update_events = async_capture_events(
hass, data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESS_UPDATE
)
task_one_evt = asyncio.Event()
task_two_evt = asyncio.Event()
event_received_evt = asyncio.Event()
@ -486,7 +489,9 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
await task_one_evt.wait()
async def long_running_job_two() -> None:
self.async_update_progress(0.25)
await task_two_evt.wait()
self.async_update_progress(0.75)
self.data = {"title": "Hello"}
uncompleted_task: asyncio.Task[None] | None = None
@ -545,6 +550,12 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
result = await manager.async_configure(result["flow_id"])
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
assert result["progress_action"] == "task_two"
assert len(progress_update_events) == 1
assert progress_update_events[0].data == {
"handler": "test",
"flow_id": result["flow_id"],
"progress": 0.25,
}
# Set task two done and wait for event
task_two_evt.set()
@ -556,6 +567,12 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
"flow_id": result["flow_id"],
"refresh": True,
}
assert len(progress_update_events) == 2
assert progress_update_events[1].data == {
"handler": "test",
"flow_id": result["flow_id"],
"progress": 0.75,
}
# Frontend refreshes the flow
result = await manager.async_configure(result["flow_id"])