Raise HassioAPIError when error is returned (#49418)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Joakim Sørensen 2021-04-19 17:23:43 +02:00 committed by GitHub
parent b8001b951b
commit a5806b59f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -21,6 +21,7 @@ ATTR_UUID = "uuid"
ATTR_WS_EVENT = "event"
ATTR_ENDPOINT = "endpoint"
ATTR_METHOD = "method"
ATTR_RESULT = "result"
ATTR_TIMEOUT = "timeout"

View File

@ -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(

View File

@ -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"