Improve WS command validate_config (#118864)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Robert Resch <robert@resch.dev>
pull/118886/head^2
Erik Montnemery 2024-06-05 18:53:44 +02:00 committed by GitHub
parent 6efc3b95a4
commit 8099ea8817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View File

@ -862,7 +862,10 @@ async def handle_validate_config(
try: try:
await validator(hass, schema(msg[key])) await validator(hass, schema(msg[key]))
except vol.Invalid as err: except (
vol.Invalid,
HomeAssistantError,
) as err:
result[key] = {"valid": False, "error": str(err)} result[key] = {"valid": False, "error": str(err)}
else: else:
result[key] = {"valid": True, "error": None} result[key] = {"valid": True, "error": None}

View File

@ -3,6 +3,7 @@
import asyncio import asyncio
from copy import deepcopy from copy import deepcopy
import logging import logging
from typing import Any
from unittest.mock import ANY, AsyncMock, Mock, patch from unittest.mock import ANY, AsyncMock, Mock, patch
import pytest import pytest
@ -2529,13 +2530,14 @@ async def test_integration_setup_info(
], ],
) )
async def test_validate_config_works( 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: ) -> None:
"""Test config validation.""" """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() msg = await websocket_client.receive_json()
assert msg["id"] == 7
assert msg["type"] == const.TYPE_RESULT assert msg["type"] == const.TYPE_RESULT
assert msg["success"] assert msg["success"]
assert msg["result"] == {key: {"valid": True, "error": None}} assert msg["result"] == {key: {"valid": True, "error": None}}
@ -2544,11 +2546,13 @@ async def test_validate_config_works(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("key", "config", "error"), ("key", "config", "error"),
[ [
# Raises vol.Invalid
( (
"trigger", "trigger",
{"platform": "non_existing", "event_type": "hello"}, {"platform": "non_existing", "event_type": "hello"},
"Invalid platform 'non_existing' specified", "Invalid platform 'non_existing' specified",
), ),
# Raises vol.Invalid
( (
"condition", "condition",
{ {
@ -2562,6 +2566,20 @@ async def test_validate_config_works(
"@ data[0]" "@ 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", "action",
{"non_existing": "domain_test.test_service"}, {"non_existing": "domain_test.test_service"},
@ -2570,13 +2588,15 @@ async def test_validate_config_works(
], ],
) )
async def test_validate_config_invalid( async def test_validate_config_invalid(
websocket_client: MockHAClientWebSocket, key, config, error websocket_client: MockHAClientWebSocket,
key: str,
config: dict[str, Any],
error: str,
) -> None: ) -> None:
"""Test config validation.""" """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() msg = await websocket_client.receive_json()
assert msg["id"] == 7
assert msg["type"] == const.TYPE_RESULT assert msg["type"] == const.TYPE_RESULT
assert msg["success"] assert msg["success"]
assert msg["result"] == {key: {"valid": False, "error": error}} assert msg["result"] == {key: {"valid": False, "error": error}}