Improve ergonomics of FlowManager.async_show_progress (#107668)
* Improve ergonomics of FlowManager.async_show_progress * Don't include progress coroutine in web response * Unconditionally reset progress task when show_progress finished * Fix race * Tweak, add tests * Address review comments * Improve error handling * Allow progress jobs to return anything * Add comment * Remove unneeded check * Change API according to discussion * Adjust typing
This commit is contained in:
parent
00b40c964a
commit
24cd6a8a52
4 changed files with 221 additions and 28 deletions
|
@ -2,7 +2,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from contextlib import suppress
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from aiogithubapi import (
|
||||
|
@ -18,7 +17,7 @@ import voluptuous as vol
|
|||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.data_entry_flow import FlowResult, UnknownFlow
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import (
|
||||
SERVER_SOFTWARE,
|
||||
async_get_clientsession,
|
||||
|
@ -124,22 +123,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
assert self._device is not None
|
||||
assert self._login_device is not None
|
||||
|
||||
try:
|
||||
response = await self._device.activation(
|
||||
device_code=self._login_device.device_code
|
||||
)
|
||||
self._login = response.data
|
||||
|
||||
finally:
|
||||
|
||||
async def _progress():
|
||||
# If the user closes the dialog the flow will no longer exist and it will raise UnknownFlow
|
||||
with suppress(UnknownFlow):
|
||||
await self.hass.config_entries.flow.async_configure(
|
||||
flow_id=self.flow_id
|
||||
)
|
||||
|
||||
self.hass.async_create_task(_progress())
|
||||
response = await self._device.activation(
|
||||
device_code=self._login_device.device_code
|
||||
)
|
||||
self._login = response.data
|
||||
|
||||
if not self._device:
|
||||
self._device = GitHubDeviceAPI(
|
||||
|
@ -174,6 +161,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"url": OAUTH_USER_LOGIN,
|
||||
"code": self._login_device.user_code,
|
||||
},
|
||||
progress_task=self.login_task,
|
||||
)
|
||||
|
||||
async def async_step_repositories(
|
||||
|
@ -220,13 +208,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""Get the options flow for this handler."""
|
||||
return OptionsFlowHandler(config_entry)
|
||||
|
||||
@callback
|
||||
def async_remove(self) -> None:
|
||||
"""Handle remove handler callback."""
|
||||
if self.login_task and not self.login_task.done():
|
||||
# Clean up login task if it's still running
|
||||
self.login_task.cancel()
|
||||
|
||||
|
||||
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle a option flow for GitHub."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue