diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index 417a62a1a8c..435d42349fd 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -21,6 +21,7 @@ ATTR_UUID = "uuid" ATTR_WS_EVENT = "event" ATTR_ENDPOINT = "endpoint" ATTR_METHOD = "method" +ATTR_RESULT = "result" ATTR_TIMEOUT = "timeout" diff --git a/homeassistant/components/hassio/websocket_api.py b/homeassistant/components/hassio/websocket_api.py index 387aa926489..dfc2b7dc01d 100644 --- a/homeassistant/components/hassio/websocket_api.py +++ b/homeassistant/components/hassio/websocket_api.py @@ -16,6 +16,7 @@ from .const import ( ATTR_DATA, ATTR_ENDPOINT, ATTR_METHOD, + ATTR_RESULT, ATTR_TIMEOUT, ATTR_WS_EVENT, DOMAIN, @@ -94,7 +95,6 @@ async def websocket_supervisor_api( ): """Websocket handler to call Supervisor API.""" supervisor: HassIO = hass.data[DOMAIN] - result = False try: result = await supervisor.send_command( msg[ATTR_ENDPOINT], @@ -102,6 +102,9 @@ async def websocket_supervisor_api( timeout=msg.get(ATTR_TIMEOUT, 10), payload=msg.get(ATTR_DATA, {}), ) + + if result.get(ATTR_RESULT) == "error": + raise hass.components.hassio.HassioAPIError(result.get("message")) except hass.components.hassio.HassioAPIError as err: _LOGGER.error("Failed to to call %s - %s", msg[ATTR_ENDPOINT], err) connection.send_error( diff --git a/tests/components/hassio/test_websocket_api.py b/tests/components/hassio/test_websocket_api.py index dcf6b64d9e2..5278d2cbb91 100644 --- a/tests/components/hassio/test_websocket_api.py +++ b/tests/components/hassio/test_websocket_api.py @@ -88,3 +88,27 @@ async def test_websocket_supervisor_api( msg = await websocket_client.receive_json() assert msg["result"]["version_latest"] == "1.0.0" + + +async def test_websocket_supervisor_api_error( + hassio_env, hass: HomeAssistant, hass_ws_client, aioclient_mock +): + """Test Supervisor websocket api error.""" + assert await async_setup_component(hass, "hassio", {}) + websocket_client = await hass_ws_client(hass) + aioclient_mock.get( + "http://127.0.0.1/ping", + json={"result": "error", "message": "example error"}, + ) + + await websocket_client.send_json( + { + WS_ID: 1, + WS_TYPE: WS_TYPE_API, + ATTR_ENDPOINT: "/ping", + ATTR_METHOD: "get", + } + ) + + msg = await websocket_client.receive_json() + assert msg["error"]["message"] == "example error"