Skip to content

Commit

Permalink
Improve WS command validate_config (home-assistant#118864)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <[email protected]>
Co-authored-by: Robert Resch <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 6efc3b9 commit 8099ea8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
5 changes: 4 additions & 1 deletion homeassistant/components/websocket_api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
32 changes: 26 additions & 6 deletions tests/components/websocket_api/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}}
Expand All @@ -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",
{
Expand All @@ -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"},
Expand All @@ -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}}
Expand Down

0 comments on commit 8099ea8

Please sign in to comment.