mirror of
https://github.com/home-assistant/core.git
synced 2025-11-07 09:59:30 +00:00
Set scripts which fail validation unavailable (#95381)
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
"""Tests for config/script."""
|
||||
from http import HTTPStatus
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import config
|
||||
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import yaml
|
||||
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
@@ -67,7 +70,12 @@ async def test_update_script_config(
|
||||
data=json.dumps({"alias": "Moon updated", "sequence": []}),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == ["script.moon"]
|
||||
assert sorted(hass.states.async_entity_ids("script")) == [
|
||||
"script.moon",
|
||||
"script.sun",
|
||||
]
|
||||
assert hass.states.get("script.moon").state == STATE_OFF
|
||||
assert hass.states.get("script.sun").state == STATE_UNAVAILABLE
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
@@ -79,11 +87,39 @@ async def test_update_script_config(
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_config", ({},))
|
||||
@pytest.mark.parametrize(
|
||||
("updated_config", "validation_error"),
|
||||
[
|
||||
({}, "required key not provided @ data['sequence']"),
|
||||
(
|
||||
{
|
||||
"sequence": {
|
||||
"condition": "state",
|
||||
# The UUID will fail being resolved to en entity_id
|
||||
"entity_id": "abcdabcdabcdabcdabcdabcdabcdabcd",
|
||||
"state": "blah",
|
||||
}
|
||||
},
|
||||
"Unknown entity registry entry abcdabcdabcdabcdabcdabcdabcdabcd",
|
||||
),
|
||||
(
|
||||
{
|
||||
"use_blueprint": {
|
||||
"path": "test_service.yaml",
|
||||
"input": {},
|
||||
},
|
||||
},
|
||||
"Missing input service_to_call",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_update_script_config_with_error(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
hass_config_store,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
updated_config: Any,
|
||||
validation_error: str,
|
||||
) -> None:
|
||||
"""Test updating script config with errors."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
@@ -98,14 +134,68 @@ async def test_update_script_config_with_error(
|
||||
|
||||
resp = await client.post(
|
||||
"/api/config/script/config/moon",
|
||||
data=json.dumps({}),
|
||||
data=json.dumps(updated_config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == []
|
||||
|
||||
assert resp.status != HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
validation_error = "required key not provided @ data['sequence']"
|
||||
assert result == {"message": f"Message malformed: {validation_error}"}
|
||||
# Assert the validation error is not logged
|
||||
assert validation_error not in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_config", ({},))
|
||||
@pytest.mark.parametrize(
|
||||
("updated_config", "validation_error"),
|
||||
[
|
||||
(
|
||||
{
|
||||
"use_blueprint": {
|
||||
"path": "test_service.yaml",
|
||||
"input": {
|
||||
"service_to_call": "test.automation",
|
||||
},
|
||||
},
|
||||
},
|
||||
"No substitution found for input blah",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_update_script_config_with_blueprint_substitution_error(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
hass_config_store,
|
||||
# setup_automation,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
updated_config: Any,
|
||||
validation_error: str,
|
||||
) -> None:
|
||||
"""Test updating script config with errors."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
await async_setup_component(hass, "config", {})
|
||||
|
||||
assert sorted(hass.states.async_entity_ids("script")) == []
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
orig_data = {"sun": {}, "moon": {}}
|
||||
hass_config_store["scripts.yaml"] = orig_data
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blueprint.models.BlueprintInputs.async_substitute",
|
||||
side_effect=yaml.UndefinedSubstitution("blah"),
|
||||
):
|
||||
resp = await client.post(
|
||||
"/api/config/script/config/moon",
|
||||
data=json.dumps(updated_config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == []
|
||||
|
||||
assert resp.status != HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
assert result == {"message": f"Message malformed: {validation_error}"}
|
||||
# Assert the validation error is not logged
|
||||
assert validation_error not in caplog.text
|
||||
@@ -131,7 +221,12 @@ async def test_update_remove_key_script_config(
|
||||
data=json.dumps({"sequence": []}),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == ["script.moon"]
|
||||
assert sorted(hass.states.async_entity_ids("script")) == [
|
||||
"script.moon",
|
||||
"script.sun",
|
||||
]
|
||||
assert hass.states.get("script.moon").state == STATE_OFF
|
||||
assert hass.states.get("script.sun").state == STATE_UNAVAILABLE
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
|
||||
Reference in New Issue
Block a user