Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt config flow to changes in HA Core 2024.2.0 #3582

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions custom_components/hacs/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
)
from .utils.logger import LOGGER

MINIMUM_HA_VERSION_SHOW_PROGRESS_TASK = "2024.2.0"

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant

Expand All @@ -52,6 +54,7 @@ class HacsFlowHandler(ConfigFlow, domain=DOMAIN):
_registration: GitHubLoginDeviceModel | None = None
_activation: GitHubLoginOauthModel | None = None
_reauth: bool = False
_use_progress_task: bool = False

def __init__(self) -> None:
"""Initialize."""
Expand All @@ -60,6 +63,8 @@ def __init__(self) -> None:

async def async_step_user(self, user_input):
"""Handle a flow initialized by the user."""
self._use_progress_task = AwesomeVersion(HAVERSION) >= MINIMUM_HA_VERSION_SHOW_PROGRESS_TASK

self._errors = {}
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
Expand All @@ -80,7 +85,12 @@ async def async_step_user(self, user_input):

@callback
def async_remove(self):
"""Cleanup."""
"""Cleanup.

Needed in old Home Assistant versions which don't support show progress tasks.
"""
if self._use_progress_task:
return
if self.activation_task and not self.activation_task.done():
self.activation_task.cancel()

Expand All @@ -97,7 +107,8 @@ async def _progress():
with suppress(UnknownFlow):
await self.hass.config_entries.flow.async_configure(flow_id=self.flow_id)

self.hass.async_create_task(_progress())
if not self._use_progress_task:
self.hass.async_create_task(_progress())

if not self.device:
integration = await async_get_integration(self.hass, DOMAIN)
Expand All @@ -122,14 +133,17 @@ async def _progress():
return self.async_show_progress_done(next_step_id="could_not_register")
return self.async_show_progress_done(next_step_id="device_done")

return self.async_show_progress(
step_id="device",
progress_action="wait_for_device",
description_placeholders={
show_progress_kwargs = {
"step_id": "device",
"progress_action": "wait_for_device",
"description_placeholders": {
"url": OAUTH_USER_LOGIN,
"code": self._registration.user_code,
},
)
}
if self._use_progress_task:
show_progress_kwargs["progress_task"] = self.activation_task
return self.async_show_progress(**show_progress_kwargs)

async def _show_config_form(self, user_input):
"""Show the configuration form to edit location data."""
Expand Down
10 changes: 4 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,8 @@ def response_mocker() -> ResponseMocker:
async def setup_integration(hass: HomeAssistant, check_report_issue: None) -> None:
## Assert the string to ensure the format did not change
if AwesomeVersion(HA_VERSION) >= "2023.11.0":
# Issues may be created because hacs accesses hass.components, hass.helpers and
# calls async_show_progress without passing a progress task
assert len(_async_suggest_report_issue_mock_call_tracker) in [0, 1, 2, 3]
# Issues may be created because hacs accesses hass.components and hass.helpers
assert len(_async_suggest_report_issue_mock_call_tracker) in [0, 1, 2]
_async_suggest_report_issue_mock_call_tracker.clear()
assert (
loader.async_suggest_report_issue(
Expand Down Expand Up @@ -361,9 +360,8 @@ async def setup_integration(hass: HomeAssistant, check_report_issue: None) -> No
async def check_report_issue() -> None:
"""Finish things up."""
yield
# Issues may be created because hacs accesses hass.components, hass.helpers and
# calls async_show_progress without passing a progress task
allowed = [0, 1, 2, 3] if AwesomeVersion(HA_VERSION) > "2023.6.0" else [0]
# Issues may be created because hacs accesses hass.components and hass.helpers
allowed = [0, 1, 2] if AwesomeVersion(HA_VERSION) > "2023.6.0" else [0]
if (times := len(_async_suggest_report_issue_mock_call_tracker)) not in allowed:
raise AssertionError(
f"homeassistant.loader.async_suggest_report_issue has been called {times} times"
Expand Down