Detect duplicate script objects in config validation (#70368)

This commit is contained in:
Franck Nijhof 2022-04-26 15:33:43 +02:00 committed by GitHub
parent 9a7f6393aa
commit cad2ba4d44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -35,6 +35,7 @@ from .const import (
CONF_REQUIRED, CONF_REQUIRED,
CONF_TRACE, CONF_TRACE,
DOMAIN, DOMAIN,
LOGGER,
) )
from .helpers import async_get_blueprints from .helpers import async_get_blueprints
@ -110,6 +111,9 @@ async def async_validate_config(hass, config):
scripts = {} scripts = {}
for _, p_config in config_per_platform(config, DOMAIN): for _, p_config in config_per_platform(config, DOMAIN):
for object_id, cfg in p_config.items(): for object_id, cfg in p_config.items():
if object_id in scripts:
LOGGER.warning("Duplicate script detected with name: '%s'", object_id)
continue
cfg = await _try_async_validate_config_item(hass, object_id, cfg, config) cfg = await _try_async_validate_config_item(hass, object_id, cfg, config)
if cfg is not None: if cfg is not None:
scripts[object_id] = cfg scripts[object_id] = cfg

View file

@ -917,3 +917,27 @@ async def test_recursive_script_indirect(hass, script_mode, warning_msg, caplog)
await asyncio.wait_for(service_called.wait(), 1) await asyncio.wait_for(service_called.wait(), 1)
assert warning_msg in caplog.text assert warning_msg in caplog.text
async def test_setup_with_duplicate_scripts(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup with duplicate configs."""
assert await async_setup_component(
hass,
"script",
{
"script one": {
"duplicate": {
"sequence": [],
},
},
"script two": {
"duplicate": {
"sequence": [],
},
},
},
)
assert "Duplicate script detected with name: 'duplicate'" in caplog.text
assert len(hass.states.async_entity_ids("script")) == 1