Add WS command integration/wait

This commit is contained in:
Erik
2025-04-02 08:34:21 +02:00
parent 253293c986
commit bc08d657d4
2 changed files with 57 additions and 1 deletions

View File

@@ -59,7 +59,11 @@ from homeassistant.loader import (
async_get_integration_descriptions, async_get_integration_descriptions,
async_get_integrations, async_get_integrations,
) )
from homeassistant.setup import async_get_loaded_integrations, async_get_setup_timings from homeassistant.setup import (
DATA_SETUP_DONE,
async_get_loaded_integrations,
async_get_setup_timings,
)
from homeassistant.util.json import format_unserializable_data from homeassistant.util.json import format_unserializable_data
from . import const, decorators, messages from . import const, decorators, messages
@@ -98,6 +102,7 @@ def async_register_commands(
async_reg(hass, handle_subscribe_entities) async_reg(hass, handle_subscribe_entities)
async_reg(hass, handle_supported_features) async_reg(hass, handle_supported_features)
async_reg(hass, handle_integration_descriptions) async_reg(hass, handle_integration_descriptions)
async_reg(hass, handle_integration_wait)
def pong_message(iden: int) -> dict[str, Any]: def pong_message(iden: int) -> dict[str, Any]:
@@ -923,3 +928,27 @@ async def handle_integration_descriptions(
) -> None: ) -> None:
"""Get metadata for all brands and integrations.""" """Get metadata for all brands and integrations."""
connection.send_result(msg["id"], await async_get_integration_descriptions(hass)) connection.send_result(msg["id"], await async_get_integration_descriptions(hass))
@decorators.websocket_command(
{
vol.Required("type"): "integration/wait",
vol.Required("domain"): str,
}
)
@decorators.async_response
async def handle_integration_wait(
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
) -> None:
"""Handle wait for integration command."""
domain = msg["domain"]
setup_done = hass.data.get(DATA_SETUP_DONE, {})
# Wait for the integration to be set up
if setup_future := setup_done.get(domain):
await setup_future
connection.send_result(
msg["id"], {"integration_loaded": domain in hass.config.components}
)

View File

@@ -2824,3 +2824,30 @@ async def test_subscribe_entities_chained_state_change(
await websocket_client.close() await websocket_client.close()
await hass.async_block_till_done() await hass.async_block_till_done()
@pytest.mark.parametrize(
("domain", "result"),
[
("config", {"integration_loaded": True}),
("non_existing_domain", {"integration_loaded": False}),
],
)
async def test_wait_integration(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
domain: str,
result: dict[str, Any],
) -> None:
"""Test we can get wait for an integration to load."""
assert await async_setup_component(hass, "config", {})
ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id({"type": "integration/wait", "domain": domain})
response = await ws_client.receive_json()
assert response == {
"id": ANY,
"result": result,
"success": True,
"type": "result",
}