1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2025-12-26 13:37:06 +00:00

Separate startup event from update check event (#4425)

* Separate startup event from update check event

* Add a queue for messages sent during startup
This commit is contained in:
Mike Degatano
2023-07-06 12:45:37 -04:00
committed by GitHub
parent 3b38047fd4
commit 96d5fc244e
6 changed files with 105 additions and 6 deletions

View File

@@ -1,8 +1,10 @@
"""Test Homeassistant module."""
import asyncio
from pathlib import Path
from unittest.mock import patch
from supervisor.const import CoreState
from supervisor.coresys import CoreSys
from supervisor.docker.interface import DockerInterface
from supervisor.homeassistant.secrets import HomeAssistantSecrets
@@ -10,6 +12,7 @@ from supervisor.homeassistant.secrets import HomeAssistantSecrets
async def test_load(coresys: CoreSys, tmp_supervisor_data: Path):
"""Test homeassistant module load."""
client = coresys.homeassistant.websocket._client # pylint: disable=protected-access
with open(tmp_supervisor_data / "homeassistant" / "secrets.yaml", "w") as secrets:
secrets.write("hello: world\n")
@@ -24,3 +27,11 @@ async def test_load(coresys: CoreSys, tmp_supervisor_data: Path):
attach.assert_called_once()
assert coresys.homeassistant.secrets.secrets == {"hello": "world"}
coresys.core.state = CoreState.SETUP
await coresys.homeassistant.websocket.async_send_message({"lorem": "ipsum"})
client.async_send_command.assert_not_called()
coresys.core.state = CoreState.RUNNING
await asyncio.sleep(0)
assert client.async_send_command.call_args_list[0][0][0] == {"lorem": "ipsum"}

View File

@@ -1,9 +1,11 @@
"""Test websocket."""
# pylint: disable=protected-access, import-error
import asyncio
import logging
from awesomeversion import AwesomeVersion
from supervisor.const import CoreState
from supervisor.coresys import CoreSys
from supervisor.homeassistant.const import WSEvent, WSType
@@ -48,3 +50,36 @@ async def test_send_command_old_core_version(coresys: CoreSys, caplog):
"test", {"lorem": "ipsum"}
)
client.async_send_command.assert_not_called()
async def test_send_message_during_startup(coresys: CoreSys):
"""Test websocket messages queue during startup."""
client = coresys.homeassistant.websocket._client
await coresys.homeassistant.websocket.load()
coresys.core.state = CoreState.SETUP
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
client.async_send_command.assert_not_called()
coresys.core.state = CoreState.RUNNING
await asyncio.sleep(0)
assert client.async_send_command.call_count == 2
assert 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 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"},
},
}