Skip to content

Commit

Permalink
Apply suggestion failures fail supervisor repair (#113372)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 authored and frenck committed Mar 14, 2024
1 parent 297c7c1 commit 45ef5a3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
5 changes: 1 addition & 4 deletions homeassistant/components/hassio/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ async def async_update_core(
@bind_hass
@_api_bool
async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> dict:
"""Apply a suggestion from supervisor's resolution center.
The caller of the function should handle HassioAPIError.
"""
"""Apply a suggestion from supervisor's resolution center."""
hassio: HassIO = hass.data[DOMAIN]
command = f"/resolution/suggestion/{suggestion_uuid}"
return await hassio.send_command(command, timeout=None)
Expand Down
11 changes: 4 additions & 7 deletions homeassistant/components/hassio/repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
PLACEHOLDER_KEY_REFERENCE,
SupervisorIssueContext,
)
from .handler import HassioAPIError, async_apply_suggestion
from .handler import async_apply_suggestion
from .issues import Issue, Suggestion

SUGGESTION_CONFIRMATION_REQUIRED = {"system_execute_reboot"}
Expand Down Expand Up @@ -109,12 +109,9 @@ async def _async_step_apply_suggestion(
if not confirmed and suggestion.key in SUGGESTION_CONFIRMATION_REQUIRED:
return self._async_form_for_suggestion(suggestion)

try:
await async_apply_suggestion(self.hass, suggestion.uuid)
except HassioAPIError:
return self.async_abort(reason="apply_suggestion_fail")

return self.async_create_entry(data={})
if await async_apply_suggestion(self.hass, suggestion.uuid):
return self.async_create_entry(data={})
return self.async_abort(reason="apply_suggestion_fail")

@staticmethod
def _async_step(
Expand Down
3 changes: 2 additions & 1 deletion tests/components/hassio/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def mock_resolution_info(
unsupported: list[str] | None = None,
unhealthy: list[str] | None = None,
issues: list[dict[str, str]] | None = None,
suggestion_result: str = "ok",
):
"""Mock resolution/info endpoint with unsupported/unhealthy reasons and/or issues."""
aioclient_mock.get(
Expand Down Expand Up @@ -76,7 +77,7 @@ def mock_resolution_info(
for suggestion in suggestions:
aioclient_mock.post(
f"http://127.0.0.1/resolution/suggestion/{suggestion['uuid']}",
json={"result": "ok"},
json={"result": suggestion_result},
)


Expand Down
72 changes: 72 additions & 0 deletions tests/components/hassio/test_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,78 @@ async def test_supervisor_issue_repair_flow_skip_confirmation(
)


async def test_mount_failed_repair_flow_error(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
hass_client: ClientSessionGenerator,
issue_registry: ir.IssueRegistry,
all_setup_requests,
) -> None:
"""Test repair flow fails when repair fails to apply."""
mock_resolution_info(
aioclient_mock,
issues=[
{
"uuid": "1234",
"type": "mount_failed",
"context": "mount",
"reference": "backup_share",
"suggestions": [
{
"uuid": "1235",
"type": "execute_reload",
"context": "mount",
"reference": "backup_share",
},
{
"uuid": "1236",
"type": "execute_remove",
"context": "mount",
"reference": "backup_share",
},
],
},
],
suggestion_result=False,
)

assert await async_setup_component(hass, "hassio", {})

repair_issue = issue_registry.async_get_issue(domain="hassio", issue_id="1234")
assert repair_issue

client = await hass_client()

resp = await client.post(
"/api/repairs/issues/fix",
json={"handler": "hassio", "issue_id": repair_issue.issue_id},
)

assert resp.status == HTTPStatus.OK
data = await resp.json()
flow_id = data["flow_id"]

resp = await client.post(
f"/api/repairs/issues/fix/{flow_id}",
json={"next_step_id": "mount_execute_reload"},
)

assert resp.status == HTTPStatus.OK
data = await resp.json()

flow_id = data["flow_id"]
assert data == {
"type": "abort",
"flow_id": flow_id,
"handler": "hassio",
"reason": "apply_suggestion_fail",
"result": None,
"description_placeholders": None,
}

assert issue_registry.async_get_issue(domain="hassio", issue_id="1234")


async def test_mount_failed_repair_flow(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
Expand Down

0 comments on commit 45ef5a3

Please sign in to comment.