Improve calls to async_show_progress in homeassistant_hardware (#107789)

pull/108016/head
Erik Montnemery 2024-01-14 10:41:44 +01:00 committed by GitHub
parent bca629ed31
commit 3895defff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 124 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from abc import ABC, abstractmethod
import asyncio
from collections.abc import Awaitable
import dataclasses
import logging
from typing import Any, Protocol
@ -339,14 +338,6 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
"""Return the correct flow manager."""
return self.hass.config_entries.options
async def _resume_flow_when_done(self, awaitable: Awaitable) -> None:
try:
await awaitable
finally:
self.hass.async_create_task(
self.flow_manager.async_configure(flow_id=self.flow_id)
)
async def _async_get_addon_info(self, addon_manager: AddonManager) -> AddonInfo:
"""Return and cache Silicon Labs Multiprotocol add-on info."""
try:
@ -411,18 +402,20 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Install Silicon Labs Multiprotocol add-on."""
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
if not self.install_task:
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
self.install_task = self.hass.async_create_task(
self._resume_flow_when_done(
multipan_manager.async_install_addon_waiting()
),
multipan_manager.async_install_addon_waiting(),
"SiLabs Multiprotocol addon install",
)
if not self.install_task.done():
return self.async_show_progress(
step_id="install_addon",
progress_action="install_addon",
description_placeholders={"addon_name": multipan_manager.addon_name},
progress_task=self.install_task,
)
try:
@ -518,27 +511,29 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Start Silicon Labs Multiprotocol add-on."""
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
if not self.start_task:
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
self.start_task = self.hass.async_create_task(
self._resume_flow_when_done(
multipan_manager.async_start_addon_waiting()
)
multipan_manager.async_start_addon_waiting()
)
if not self.start_task.done():
return self.async_show_progress(
step_id="start_addon",
progress_action="start_addon",
description_placeholders={"addon_name": multipan_manager.addon_name},
progress_task=self.start_task,
)
try:
await self.start_task
except (AddonError, AbortFlow) as err:
self.start_task = None
_LOGGER.error(err)
return self.async_show_progress_done(next_step_id="start_failed")
finally:
self.start_task = None
self.start_task = None
return self.async_show_progress_done(next_step_id="finish_addon_setup")
async def async_step_start_failed(
@ -715,15 +710,16 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
if not self.install_task:
self.install_task = self.hass.async_create_task(
self._resume_flow_when_done(
flasher_manager.async_install_addon_waiting()
),
flasher_manager.async_install_addon_waiting(),
"SiLabs Flasher addon install",
)
if not self.install_task.done():
return self.async_show_progress(
step_id="install_flasher_addon",
progress_action="install_addon",
description_placeholders={"addon_name": flasher_manager.addon_name},
progress_task=self.install_task,
)
try:
@ -800,19 +796,20 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Uninstall Silicon Labs Multiprotocol add-on."""
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
if not self.stop_task:
multipan_manager = await get_multiprotocol_addon_manager(self.hass)
self.stop_task = self.hass.async_create_task(
self._resume_flow_when_done(
multipan_manager.async_uninstall_addon_waiting()
),
multipan_manager.async_uninstall_addon_waiting(),
"SiLabs Multiprotocol addon uninstall",
)
if not self.stop_task.done():
return self.async_show_progress(
step_id="uninstall_multiprotocol_addon",
progress_action="uninstall_multiprotocol_addon",
description_placeholders={"addon_name": multipan_manager.addon_name},
progress_task=self.stop_task,
)
try:
@ -826,9 +823,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Start Silicon Labs Flasher add-on."""
flasher_manager = get_flasher_addon_manager(self.hass)
if not self.start_task:
flasher_manager = get_flasher_addon_manager(self.hass)
async def start_and_wait_until_done() -> None:
await flasher_manager.async_start_addon_waiting()
@ -837,13 +834,14 @@ class OptionsFlowHandler(config_entries.OptionsFlow, ABC):
AddonState.NOT_RUNNING
)
self.start_task = self.hass.async_create_task(
self._resume_flow_when_done(start_and_wait_until_done())
)
self.start_task = self.hass.async_create_task(start_and_wait_until_done())
if not self.start_task.done():
return self.async_show_progress(
step_id="start_flasher_addon",
progress_action="start_flasher_addon",
description_placeholders={"addon_name": flasher_manager.addon_name},
progress_task=self.start_task,
)
try:

View File

@ -242,9 +242,7 @@ async def test_option_flow_install_multi_pan_addon(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -263,9 +261,7 @@ async def test_option_flow_install_multi_pan_addon(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -321,9 +317,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
multipan_manager = await silabs_multiprotocol_addon.get_multiprotocol_addon_manager(
@ -362,9 +356,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
}
assert zha_config_entry.title == "Test Multiprotocol"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -420,9 +412,7 @@ async def test_option_flow_install_multi_pan_addon_zha_other_radio(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
addon_info.return_value["hostname"] = "core-silabs-multiprotocol"
@ -442,9 +432,7 @@ async def test_option_flow_install_multi_pan_addon_zha_other_radio(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -700,19 +688,15 @@ async def test_option_flow_addon_installed_same_device_uninstall(
assert result["step_id"] == "install_flasher_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_flasher_addon"
await hass.async_block_till_done()
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "uninstall_multiprotocol_addon"
assert result["progress_action"] == "uninstall_multiprotocol_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
uninstall_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "start_flasher_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS
@ -720,10 +704,8 @@ async def test_option_flow_addon_installed_same_device_uninstall(
assert result["progress_action"] == "start_flasher_addon"
assert result["description_placeholders"] == {"addon_name": "Silicon Labs Flasher"}
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_flasher")
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "flashing_complete"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.CREATE_ENTRY
@ -878,10 +860,8 @@ async def test_option_flow_addon_installed_same_device_flasher_already_installed
assert result["step_id"] == "uninstall_multiprotocol_addon"
assert result["progress_action"] == "uninstall_multiprotocol_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
uninstall_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "start_flasher_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS
@ -894,10 +874,8 @@ async def test_option_flow_addon_installed_same_device_flasher_already_installed
"available": True,
"state": "not_running",
}
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
install_addon.assert_not_called()
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "flashing_complete"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.CREATE_ENTRY
@ -964,9 +942,7 @@ async def test_option_flow_flasher_install_failure(
assert result["step_id"] == "install_flasher_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "install_failed"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_flasher")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1019,10 +995,8 @@ async def test_option_flow_flasher_addon_flash_failure(
start_addon.side_effect = HassioAPIError("Boom")
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
uninstall_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "start_flasher_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS
@ -1030,10 +1004,7 @@ async def test_option_flow_flasher_addon_flash_failure(
assert result["progress_action"] == "start_flasher_addon"
assert result["description_placeholders"] == {"addon_name": "Silicon Labs Flasher"}
addon_store_info.return_value["installed"] = True
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "flasher_failed"
await hass.async_block_till_done()
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.ABORT
@ -1158,10 +1129,8 @@ async def test_option_flow_uninstall_migration_finish_failure(
result["flow_id"], {silabs_multiprotocol_addon.CONF_DISABLE_MULTI_PAN: True}
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
await hass.async_block_till_done()
uninstall_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "start_flasher_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS
@ -1169,9 +1138,7 @@ async def test_option_flow_uninstall_migration_finish_failure(
assert result["progress_action"] == "start_flasher_addon"
assert result["description_placeholders"] == {"addon_name": "Silicon Labs Flasher"}
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "flashing_complete"
await hass.async_block_till_done()
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.ABORT
@ -1242,9 +1209,7 @@ async def test_option_flow_install_multi_pan_addon_install_fails(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "install_failed"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1287,9 +1252,7 @@ async def test_option_flow_install_multi_pan_addon_start_fails(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1308,9 +1271,7 @@ async def test_option_flow_install_multi_pan_addon_start_fails(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "start_failed"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1353,9 +1314,7 @@ async def test_option_flow_install_multi_pan_addon_set_options_fails(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1432,9 +1391,7 @@ async def test_option_flow_install_multi_pan_addon_zha_migration_fails_step_1(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1490,9 +1447,7 @@ async def test_option_flow_install_multi_pan_addon_zha_migration_fails_step_2(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -1511,9 +1466,7 @@ async def test_option_flow_install_multi_pan_addon_zha_migration_fails_step_2(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])

View File

@ -210,9 +210,7 @@ async def test_option_flow_install_multi_pan_addon(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -231,9 +229,7 @@ async def test_option_flow_install_multi_pan_addon(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -313,9 +309,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -343,9 +337,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
"radio_type": "ezsp",
}
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])

View File

@ -143,9 +143,7 @@ async def test_option_flow_install_multi_pan_addon(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -164,9 +162,7 @@ async def test_option_flow_install_multi_pan_addon(
},
)
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -225,9 +221,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
assert result["step_id"] == "install_addon"
assert result["progress_action"] == "install_addon"
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "configure_addon"
await hass.async_block_till_done()
install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])
@ -255,9 +249,7 @@ async def test_option_flow_install_multi_pan_addon_zha(
"radio_type": "ezsp",
}
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.SHOW_PROGRESS_DONE
assert result["step_id"] == "finish_addon_setup"
await hass.async_block_till_done()
start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol")
result = await hass.config_entries.options.async_configure(result["flow_id"])