mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Config flow progress in percent (#142737)
* Config flow progress in percent * PR comments
This commit is contained in:
parent
422bcecec1
commit
9239ace1c8
@ -40,6 +40,7 @@ class FlowResultType(StrEnum):
|
|||||||
|
|
||||||
# Event that is fired when a flow is progressed via external or progress source.
|
# 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_PROGRESSED = "data_entry_flow_progressed"
|
||||||
|
EVENT_DATA_ENTRY_FLOW_PROGRESS_UPDATE = "data_entry_flow_progress_update"
|
||||||
|
|
||||||
FLOW_NOT_COMPLETE_STEPS = {
|
FLOW_NOT_COMPLETE_STEPS = {
|
||||||
FlowResultType.FORM,
|
FlowResultType.FORM,
|
||||||
@ -829,6 +830,14 @@ class FlowHandler(Generic[_FlowContextT, _FlowResultT, _HandlerT]):
|
|||||||
flow_result["step_id"] = step_id
|
flow_result["step_id"] = step_id
|
||||||
return flow_result
|
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
|
@callback
|
||||||
def async_show_progress_done(self, *, next_step_id: str) -> _FlowResultT:
|
def async_show_progress_done(self, *, next_step_id: str) -> _FlowResultT:
|
||||||
"""Mark the progress done."""
|
"""Mark the progress done."""
|
||||||
|
@ -464,6 +464,9 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
|
|||||||
"""Test show progress logic."""
|
"""Test show progress logic."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
events = []
|
events = []
|
||||||
|
progress_update_events = async_capture_events(
|
||||||
|
hass, data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESS_UPDATE
|
||||||
|
)
|
||||||
task_one_evt = asyncio.Event()
|
task_one_evt = asyncio.Event()
|
||||||
task_two_evt = asyncio.Event()
|
task_two_evt = asyncio.Event()
|
||||||
event_received_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()
|
await task_one_evt.wait()
|
||||||
|
|
||||||
async def long_running_job_two() -> None:
|
async def long_running_job_two() -> None:
|
||||||
|
self.async_update_progress(0.25)
|
||||||
await task_two_evt.wait()
|
await task_two_evt.wait()
|
||||||
|
self.async_update_progress(0.75)
|
||||||
self.data = {"title": "Hello"}
|
self.data = {"title": "Hello"}
|
||||||
|
|
||||||
uncompleted_task: asyncio.Task[None] | None = None
|
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"])
|
result = await manager.async_configure(result["flow_id"])
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
||||||
assert result["progress_action"] == "task_two"
|
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
|
# Set task two done and wait for event
|
||||||
task_two_evt.set()
|
task_two_evt.set()
|
||||||
@ -556,6 +567,12 @@ async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> N
|
|||||||
"flow_id": result["flow_id"],
|
"flow_id": result["flow_id"],
|
||||||
"refresh": True,
|
"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
|
# Frontend refreshes the flow
|
||||||
result = await manager.async_configure(result["flow_id"])
|
result = await manager.async_configure(result["flow_id"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user