Add TT WS API (#28599)

* Add TT WS API

* Add a test

* Correctly convert TT errrors
This commit is contained in:
Paulus Schoutsen 2019-11-08 01:06:16 -08:00 committed by Bram Kragten
parent b2071b81c1
commit bd54ff3c02
7 changed files with 93 additions and 20 deletions

View file

@ -7,6 +7,7 @@ import pytest
from jose import jwt
from hass_nabucasa.auth import Unauthenticated, UnknownError
from hass_nabucasa.const import STATE_CONNECTED
from hass_nabucasa import thingtalk
from homeassistant.core import State
from homeassistant.auth.providers import trusted_networks as tn_auth
@ -871,3 +872,55 @@ async def test_enable_alexa_state_report_fail(
assert not response["success"]
assert response["error"]["code"] == "alexa_relink"
async def test_thingtalk_convert(hass, hass_ws_client, setup_api):
"""Test that we can convert a query."""
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.cloud.http_api.thingtalk.async_convert",
return_value=mock_coro({"hello": "world"}),
):
await client.send_json(
{"id": 5, "type": "cloud/thingtalk/convert", "query": "some-data"}
)
response = await client.receive_json()
assert response["success"]
assert response["result"] == {"hello": "world"}
async def test_thingtalk_convert_timeout(hass, hass_ws_client, setup_api):
"""Test that we can convert a query."""
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.cloud.http_api.thingtalk.async_convert",
side_effect=asyncio.TimeoutError,
):
await client.send_json(
{"id": 5, "type": "cloud/thingtalk/convert", "query": "some-data"}
)
response = await client.receive_json()
assert not response["success"]
assert response["error"]["code"] == "timeout"
async def test_thingtalk_convert_internal(hass, hass_ws_client, setup_api):
"""Test that we can convert a query."""
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.cloud.http_api.thingtalk.async_convert",
side_effect=thingtalk.ThingTalkConversionError("Did not understand"),
):
await client.send_json(
{"id": 5, "type": "cloud/thingtalk/convert", "query": "some-data"}
)
response = await client.receive_json()
assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["message"] == "Did not understand"