Files
supervisor/tests/homeassistant/test_websocket.py
Stefan Agner 122b73202b Unify Supervisor event message functions (#5831)
* Unify Supervisor event message functions

Unify functions which send WebSocket messages of type
"supervisor/event". This deduplicates code and hopefully avoids further
diversication in the future.

While at it, remove unused HomeAssistantWSNotSupported exception. It
seems the only place this exception is used got removed in #3317.

* Test message delivery during shutdown states
2025-04-23 10:40:25 +02:00

95 lines
2.9 KiB
Python

"""Test websocket."""
# pylint: disable=import-error
import asyncio
import logging
from unittest.mock import AsyncMock
from awesomeversion import AwesomeVersion
from supervisor.const import CoreState
from supervisor.coresys import CoreSys
from supervisor.homeassistant.const import WSEvent, WSType
async def test_send_command(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test websocket error on listen."""
await coresys.homeassistant.websocket.async_send_command({"type": "test"})
ha_ws_client.async_send_command.assert_called_with({"type": "test"})
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
ha_ws_client.async_send_command.assert_called_with(
{
"type": WSType.SUPERVISOR_EVENT,
"data": {
"event": WSEvent.SUPERVISOR_UPDATE,
"update_key": "test",
"data": {"lorem": "ipsum"},
},
}
)
async def test_send_command_old_core_version(
coresys: CoreSys, ha_ws_client: AsyncMock, caplog
):
"""Test websocket error on listen."""
caplog.set_level(logging.INFO)
ha_ws_client.ha_version = AwesomeVersion("1970.1.1")
await coresys.homeassistant.websocket.async_send_command(
{"type": "supervisor/event"}
)
assert (
"WebSocket command supervisor/event is not supported until core-2021.2.4"
in caplog.text
)
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
ha_ws_client.async_send_command.assert_not_called()
async def test_send_message_during_startup(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test websocket messages queue during startup."""
await coresys.homeassistant.websocket.load()
await coresys.core.set_state(CoreState.SETUP)
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
ha_ws_client.async_send_command.assert_not_called()
await coresys.core.set_state(CoreState.RUNNING)
await asyncio.sleep(0)
assert ha_ws_client.async_send_command.call_count == 2
assert ha_ws_client.async_send_command.call_args_list[0][0][0] == {
"type": WSType.SUPERVISOR_EVENT,
"data": {
"event": WSEvent.SUPERVISOR_UPDATE,
"update_key": "test",
"data": {"lorem": "ipsum"},
},
}
assert ha_ws_client.async_send_command.call_args_list[1][0][0] == {
"type": WSType.SUPERVISOR_EVENT,
"data": {
"event": WSEvent.SUPERVISOR_UPDATE,
"update_key": "info",
"data": {"state": "running"},
},
}
ha_ws_client.reset_mock()
await coresys.core.set_state(CoreState.SHUTDOWN)
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
ha_ws_client.async_send_command.assert_not_called()