diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index e159880c8bc090..f66930c8d0049f 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -862,7 +862,10 @@ async def handle_validate_config( try: await validator(hass, schema(msg[key])) - except vol.Invalid as err: + except ( + vol.Invalid, + HomeAssistantError, + ) as err: result[key] = {"valid": False, "error": str(err)} else: result[key] = {"valid": True, "error": None} diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index 655d8adf1ea1d8..a51e51b81b09a7 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -3,6 +3,7 @@ import asyncio from copy import deepcopy import logging +from typing import Any from unittest.mock import ANY, AsyncMock, Mock, patch import pytest @@ -2529,13 +2530,14 @@ async def test_integration_setup_info( ], ) async def test_validate_config_works( - websocket_client: MockHAClientWebSocket, key, config + websocket_client: MockHAClientWebSocket, + key: str, + config: dict[str, Any] | list[dict[str, Any]], ) -> None: """Test config validation.""" - await websocket_client.send_json({"id": 7, "type": "validate_config", key: config}) + await websocket_client.send_json_auto_id({"type": "validate_config", key: config}) msg = await websocket_client.receive_json() - assert msg["id"] == 7 assert msg["type"] == const.TYPE_RESULT assert msg["success"] assert msg["result"] == {key: {"valid": True, "error": None}} @@ -2544,11 +2546,13 @@ async def test_validate_config_works( @pytest.mark.parametrize( ("key", "config", "error"), [ + # Raises vol.Invalid ( "trigger", {"platform": "non_existing", "event_type": "hello"}, "Invalid platform 'non_existing' specified", ), + # Raises vol.Invalid ( "condition", { @@ -2562,6 +2566,20 @@ async def test_validate_config_works( "@ data[0]" ), ), + # Raises HomeAssistantError + ( + "condition", + { + "above": 50, + "condition": "device", + "device_id": "a51a57e5af051eb403d56eb9e6fd691c", + "domain": "sensor", + "entity_id": "7d18a157b7c00adbf2982ea7de0d0362", + "type": "is_carbon_dioxide", + }, + "Unknown device 'a51a57e5af051eb403d56eb9e6fd691c'", + ), + # Raises vol.Invalid ( "action", {"non_existing": "domain_test.test_service"}, @@ -2570,13 +2588,15 @@ async def test_validate_config_works( ], ) async def test_validate_config_invalid( - websocket_client: MockHAClientWebSocket, key, config, error + websocket_client: MockHAClientWebSocket, + key: str, + config: dict[str, Any], + error: str, ) -> None: """Test config validation.""" - await websocket_client.send_json({"id": 7, "type": "validate_config", key: config}) + await websocket_client.send_json_auto_id({"type": "validate_config", key: config}) msg = await websocket_client.receive_json() - assert msg["id"] == 7 assert msg["type"] == const.TYPE_RESULT assert msg["success"] assert msg["result"] == {key: {"valid": False, "error": error}}