Set scripts which fail validation unavailable (#95381)

This commit is contained in:
Erik Montnemery 2023-06-27 18:24:34 +02:00 committed by GitHub
parent 17ac1a6d32
commit 1fec407a24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 357 additions and 39 deletions

View file

@ -16,6 +16,7 @@ from homeassistant.const import (
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_UNAVAILABLE,
)
from homeassistant.core import (
Context,
@ -158,14 +159,32 @@ invalid_configs = [
]
@pytest.mark.parametrize("value", invalid_configs)
async def test_setup_with_invalid_configs(hass: HomeAssistant, value) -> None:
@pytest.mark.parametrize(
("config", "nbr_script_entities"),
[
({"test": {}}, 1),
# Invalid slug, entity can't be set up
({"test hello world": {"sequence": [{"event": "bla"}]}}, 0),
(
{
"test": {
"sequence": {
"event": "test_event",
"service": "homeassistant.turn_on",
}
}
},
1,
),
],
)
async def test_setup_with_invalid_configs(
hass: HomeAssistant, config, nbr_script_entities
) -> None:
"""Test setup with invalid configs."""
assert await async_setup_component(
hass, "script", {"script": value}
), f"Script loaded with wrong config {value}"
assert await async_setup_component(hass, "script", {"script": config})
assert len(hass.states.async_entity_ids("script")) == 0
assert len(hass.states.async_entity_ids("script")) == nbr_script_entities
@pytest.mark.parametrize(
@ -177,6 +196,47 @@ async def test_setup_with_invalid_configs(hass: HomeAssistant, value) -> None:
"has invalid object id",
"invalid slug Bad Script",
),
),
)
async def test_bad_config_validation_critical(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
object_id,
broken_config,
problem,
details,
) -> None:
"""Test bad script configuration which can be detected during validation."""
assert await async_setup_component(
hass,
script.DOMAIN,
{
script.DOMAIN: {
object_id: {"alias": "bad_script", **broken_config},
"good_script": {
"alias": "good_script",
"sequence": {
"service": "test.automation",
"entity_id": "hello.world",
},
},
}
},
)
# Check we get the expected error message
assert (
f"Script with alias 'bad_script' {problem} and has been disabled: {details}"
in caplog.text
)
# Make sure one bad script does not prevent other scripts from setting up
assert hass.states.async_entity_ids("script") == ["script.good_script"]
@pytest.mark.parametrize(
("object_id", "broken_config", "problem", "details"),
(
(
"bad_script",
{},
@ -230,8 +290,13 @@ async def test_bad_config_validation(
in caplog.text
)
# Make sure one bad script does not prevent other scripts from setting up
assert hass.states.async_entity_ids("script") == ["script.good_script"]
# Make sure both scripts are setup
assert set(hass.states.async_entity_ids("script")) == {
"script.bad_script",
"script.good_script",
}
# The script failing validation should be unavailable
assert hass.states.get("script.bad_script").state == STATE_UNAVAILABLE
@pytest.mark.parametrize("running", ["no", "same", "different"])
@ -614,6 +679,25 @@ async def test_extraction_functions_unknown_script(hass: HomeAssistant) -> None:
assert script.entities_in_script(hass, "script.unknown") == []
async def test_extraction_functions_unavailable_script(hass: HomeAssistant) -> None:
"""Test extraction functions for an unknown automation."""
entity_id = "script.test1"
assert await async_setup_component(
hass,
DOMAIN,
{DOMAIN: {"test1": {}}},
)
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
assert script.scripts_with_area(hass, "area-in-both") == []
assert script.areas_in_script(hass, entity_id) == []
assert script.scripts_with_blueprint(hass, "blabla.yaml") == []
assert script.blueprint_in_script(hass, entity_id) is None
assert script.scripts_with_device(hass, "device-in-both") == []
assert script.devices_in_script(hass, entity_id) == []
assert script.scripts_with_entity(hass, "light.in_both") == []
assert script.entities_in_script(hass, entity_id) == []
async def test_extraction_functions(hass: HomeAssistant) -> None:
"""Test extraction functions."""
assert await async_setup_component(