Raise repair issues when scripts can't be set up (#122087)

This commit is contained in:
Erik Montnemery 2024-07-18 08:34:41 +02:00 committed by GitHub
parent e2276458ed
commit 0927dd9090
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 189 additions and 23 deletions

View file

@ -3,7 +3,7 @@
import asyncio
from datetime import timedelta
from typing import Any
from unittest.mock import Mock, patch
from unittest.mock import ANY, Mock, patch
import pytest
@ -47,11 +47,13 @@ import homeassistant.util.dt as dt_util
from tests.common import (
MockConfigEntry,
MockUser,
async_fire_time_changed,
async_mock_service,
mock_restore_cache,
)
from tests.components.logbook.common import MockRow, mock_humanify
from tests.components.repairs import get_repairs
from tests.typing import WebSocketGenerator
ENTITY_ID = "script.test"
@ -252,13 +254,14 @@ async def test_bad_config_validation_critical(
@pytest.mark.parametrize(
("object_id", "broken_config", "problem", "details"),
("object_id", "broken_config", "problem", "details", "issue"),
[
(
"bad_script",
{},
"could not be validated",
"required key not provided @ data['sequence']",
"validation_failed_schema",
),
(
"bad_script",
@ -270,18 +273,22 @@ async def test_bad_config_validation_critical(
"state": "blah",
},
},
"failed to setup actions",
"failed to setup sequence",
"Unknown entity registry entry abcdabcdabcdabcdabcdabcdabcdabcd.",
"validation_failed_sequence",
),
],
)
async def test_bad_config_validation(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
hass_admin_user: MockUser,
object_id,
broken_config,
problem,
details,
issue,
) -> None:
"""Test bad script configuration which can be detected during validation."""
assert await async_setup_component(
@ -301,11 +308,22 @@ async def test_bad_config_validation(
},
)
# Check we get the expected error message
# Check we get the expected error message and issue
assert (
f"Script with alias 'bad_script' {problem} and has been disabled: {details}"
in caplog.text
)
issues = await get_repairs(hass, hass_ws_client)
assert len(issues) == 1
assert issues[0]["issue_id"] == f"script.bad_script_{issue}"
assert issues[0]["translation_key"] == issue
assert issues[0]["translation_placeholders"] == {
"edit": "/config/script/edit/bad_script",
"entity_id": "script.bad_script",
"error": ANY,
"name": "bad_script",
}
assert issues[0]["translation_placeholders"]["error"].startswith(details)
# Make sure both scripts are setup
assert set(hass.states.async_entity_ids("script")) == {
@ -315,6 +333,31 @@ async def test_bad_config_validation(
# The script failing validation should be unavailable
assert hass.states.get("script.bad_script").state == STATE_UNAVAILABLE
# Reloading the automation with fixed config should clear the issue
with patch(
"homeassistant.config.load_yaml_config_file",
autospec=True,
return_value={
script.DOMAIN: {
object_id: {
"alias": "bad_script",
"sequence": {
"service": "test.automation",
"entity_id": "hello.world",
},
},
}
},
):
await hass.services.async_call(
script.DOMAIN,
SERVICE_RELOAD,
context=Context(user_id=hass_admin_user.id),
blocking=True,
)
issues = await get_repairs(hass, hass_ws_client)
assert len(issues) == 0
@pytest.mark.parametrize("running", ["no", "same", "different"])
async def test_reload_service(hass: HomeAssistant, running) -> None:
@ -1563,9 +1606,7 @@ async def test_script_service_changed_entity_id(
assert calls[1].data["entity_id"] == "script.custom_entity_id_2"
async def test_blueprint_automation(
hass: HomeAssistant, calls: list[ServiceCall]
) -> None:
async def test_blueprint_script(hass: HomeAssistant, calls: list[ServiceCall]) -> None:
"""Test blueprint script."""
assert await async_setup_component(
hass,
@ -1623,6 +1664,7 @@ async def test_blueprint_automation(
)
async def test_blueprint_script_bad_config(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
blueprint_inputs,
problem,
@ -1646,9 +1688,24 @@ async def test_blueprint_script_bad_config(
assert problem in caplog.text
assert details in caplog.text
issues = await get_repairs(hass, hass_ws_client)
assert len(issues) == 1
issue = "validation_failed_blueprint"
assert issues[0]["issue_id"] == f"script.test_script_{issue}"
assert issues[0]["translation_key"] == issue
assert issues[0]["translation_placeholders"] == {
"edit": "/config/script/edit/test_script",
"entity_id": "script.test_script",
"error": ANY,
"name": "test_script",
}
assert issues[0]["translation_placeholders"]["error"].startswith(details)
async def test_blueprint_script_fails_substitution(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test blueprint script with bad inputs."""
with patch(
@ -1677,6 +1734,18 @@ async def test_blueprint_script_fails_substitution(
in caplog.text
)
issues = await get_repairs(hass, hass_ws_client)
assert len(issues) == 1
issue = "validation_failed_blueprint"
assert issues[0]["issue_id"] == f"script.test_script_{issue}"
assert issues[0]["translation_key"] == issue
assert issues[0]["translation_placeholders"] == {
"edit": "/config/script/edit/test_script",
"entity_id": "script.test_script",
"error": "No substitution found for input blah",
"name": "test_script",
}
@pytest.mark.parametrize("response", [{"value": 5}, '{"value": 5}'])
async def test_responses(hass: HomeAssistant, response: Any) -> None: