mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Raise HassioAPIError when error is returned (#49418)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
b8001b951b
commit
a5806b59f2
@ -21,6 +21,7 @@ ATTR_UUID = "uuid"
|
||||
ATTR_WS_EVENT = "event"
|
||||
ATTR_ENDPOINT = "endpoint"
|
||||
ATTR_METHOD = "method"
|
||||
ATTR_RESULT = "result"
|
||||
ATTR_TIMEOUT = "timeout"
|
||||
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user