2018-10-01 09:21:00 +00:00
|
|
|
"""Fixtures for websocket tests."""
|
|
|
|
import pytest
|
|
|
|
|
2018-10-01 14:09:31 +00:00
|
|
|
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
|
2019-12-09 11:30:23 +00:00
|
|
|
from homeassistant.components.websocket_api.http import URL
|
|
|
|
from homeassistant.setup import async_setup_component
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2018-11-27 09:12:31 +00:00
|
|
|
def websocket_client(hass, hass_ws_client, hass_access_token):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Create a websocket client."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return hass.loop.run_until_complete(hass_ws_client(hass, hass_access_token))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def no_auth_websocket_client(hass, loop, aiohttp_client):
|
|
|
|
"""Websocket connection that requires authentication."""
|
2019-10-14 21:56:45 +00:00
|
|
|
assert loop.run_until_complete(async_setup_component(hass, "websocket_api", {}))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
client = loop.run_until_complete(aiohttp_client(hass.http.app))
|
2018-10-01 14:09:31 +00:00
|
|
|
ws = loop.run_until_complete(client.ws_connect(URL))
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
auth_ok = loop.run_until_complete(ws.receive_json())
|
2019-07-31 19:25:30 +00:00
|
|
|
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
yield ws
|
|
|
|
|
|
|
|
if not ws.closed:
|
|
|
|
loop.run_until_complete(ws.close())
|