diff --git a/supervisor/resolution/module.py b/supervisor/resolution/module.py index 08ad36193..441c5538e 100644 --- a/supervisor/resolution/module.py +++ b/supervisor/resolution/module.py @@ -87,7 +87,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes): # Event on issue creation self.sys_homeassistant.websocket.supervisor_event( - WSEvent.ISSUE_CHANGED, attr.asdict(issue) + WSEvent.ISSUE_CHANGED, self._make_issue_message(issue) ) @property @@ -112,7 +112,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes): # Event on suggestion added to issue for issue in self.issues_for_suggestion(suggestion): self.sys_homeassistant.websocket.supervisor_event( - WSEvent.ISSUE_CHANGED, attr.asdict(issue) + WSEvent.ISSUE_CHANGED, self._make_issue_message(issue) ) @property @@ -145,6 +145,15 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes): attr.asdict(HealthChanged(False, self.unhealthy)), ) + def _make_issue_message(self, issue: Issue) -> dict[str, Any]: + """Make issue into message for core.""" + return attr.asdict(issue) | { + "suggestions": [ + attr.asdict(suggestion) + for suggestion in self.suggestions_for_issue(issue) + ] + } + def get_suggestion(self, uuid: str) -> Suggestion: """Return suggestion with uuid.""" for suggestion in self._suggestions: @@ -215,7 +224,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes): # Event on suggestion removed from issues for issue in self.issues_for_suggestion(suggestion): self.sys_homeassistant.websocket.supervisor_event( - WSEvent.ISSUE_CHANGED, attr.asdict(issue) + WSEvent.ISSUE_CHANGED, self._make_issue_message(issue) ) def dismiss_issue(self, issue: Issue) -> None: diff --git a/tests/resolution/test_resolution_manager.py b/tests/resolution/test_resolution_manager.py index 160c59457..6d523d7df 100644 --- a/tests/resolution/test_resolution_manager.py +++ b/tests/resolution/test_resolution_manager.py @@ -3,7 +3,6 @@ import asyncio from typing import Any from unittest.mock import AsyncMock, patch -import attr import pytest from supervisor.coresys import CoreSys @@ -211,8 +210,22 @@ async def test_events_on_issue_changes(coresys: CoreSys): assert len(coresys.resolution.suggestions) == 1 issue = coresys.resolution.issues[0] suggestion = coresys.resolution.suggestions[0] + issue_expected = { + "type": "corrupt_repository", + "context": "store", + "reference": "test_repo", + "uuid": issue.uuid, + } + suggestion_expected = { + "type": "execute_reset", + "context": "store", + "reference": "test_repo", + "uuid": suggestion.uuid, + } send_message.assert_called_once_with( - _supervisor_event_message("issue_changed", attr.asdict(issue)) + _supervisor_event_message( + "issue_changed", issue_expected | {"suggestions": [suggestion_expected]} + ) ) # Adding a suggestion that fixes the issue changes it @@ -221,16 +234,28 @@ async def test_events_on_issue_changes(coresys: CoreSys): SuggestionType.EXECUTE_REMOVE, ContextType.STORE, "test_repo" ) await asyncio.sleep(0) - send_message.assert_called_once_with( - _supervisor_event_message("issue_changed", attr.asdict(issue)) - ) + send_message.assert_called_once() + sent_data = send_message.call_args.args[0] + assert sent_data["type"] == "supervisor/event" + assert sent_data["data"]["event"] == "issue_changed" + assert sent_data["data"]["data"].items() >= issue_expected.items() + assert len(sent_data["data"]["data"]["suggestions"]) == 2 + assert suggestion_expected in sent_data["data"]["data"]["suggestions"] + assert { + "type": "execute_remove", + "context": "store", + "reference": "test_repo", + "uuid": execute_remove.uuid, + } in sent_data["data"]["data"]["suggestions"] # Removing a suggestion that fixes the issue changes it again send_message.reset_mock() coresys.resolution.dismiss_suggestion(execute_remove) await asyncio.sleep(0) send_message.assert_called_once_with( - _supervisor_event_message("issue_changed", attr.asdict(issue)) + _supervisor_event_message( + "issue_changed", issue_expected | {"suggestions": [suggestion_expected]} + ) ) # Applying a suggestion should only fire an issue removed event @@ -240,7 +265,7 @@ async def test_events_on_issue_changes(coresys: CoreSys): await asyncio.sleep(0) send_message.assert_called_once_with( - _supervisor_event_message("issue_removed", attr.asdict(issue)) + _supervisor_event_message("issue_removed", issue_expected) )