Expose more Websocket API constants (#42263)

This commit is contained in:
Paulus Schoutsen 2020-10-25 21:10:13 +01:00 committed by GitHub
parent c157a582b8
commit de12ac354a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 13 deletions

View file

@ -1,4 +1,10 @@
"""Test WebSocket Connection class."""
import asyncio
import logging
import voluptuous as vol
from homeassistant import exceptions
from homeassistant.components import websocket_api
from homeassistant.components.websocket_api import const
@ -20,3 +26,32 @@ async def test_send_big_result(hass, websocket_client):
assert msg["type"] == const.TYPE_RESULT
assert msg["success"]
assert msg["result"] == {"big": "result"}
async def test_exception_handling():
"""Test handling of exceptions."""
send_messages = []
conn = websocket_api.ActiveConnection(
logging.getLogger(__name__), None, send_messages.append, None, None
)
for (exc, code, err) in (
(exceptions.Unauthorized(), websocket_api.ERR_UNAUTHORIZED, "Unauthorized"),
(
vol.Invalid("Invalid something"),
websocket_api.ERR_INVALID_FORMAT,
"Invalid something. Got {'id': 5}",
),
(asyncio.TimeoutError(), websocket_api.ERR_TIMEOUT, "Timeout"),
(
exceptions.HomeAssistantError("Failed to do X"),
websocket_api.ERR_UNKNOWN_ERROR,
"Failed to do X",
),
(ValueError("Really bad"), websocket_api.ERR_UNKNOWN_ERROR, "Unknown error"),
):
send_messages.clear()
conn.async_handle_exception({"id": 5}, exc)
assert len(send_messages) == 1
assert send_messages[0]["error"]["code"] == code
assert send_messages[0]["error"]["message"] == err