From 42691b783eb5038bed439a3f1bb450258ab73799 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sat, 12 Oct 2019 21:28:47 +0200 Subject: [PATCH] Handle empty service in script action gracefully (#27467) * Handle empty service in script action gracefully * Add test --- homeassistant/helpers/config_validation.py | 1 + tests/components/automation/test_init.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 8598b50f140..7ca5a7e86f9 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -386,6 +386,7 @@ def remove_falsy(value: List[T]) -> List[T]: def service(value): """Validate service.""" # Services use same format as entities so we can use same helper. + value = string(value).lower() if valid_entity_id(value): return value raise vol.Invalid("Service {} does not match format .".format(value)) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 6acb40cec88..a0573ce7c1b 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -842,6 +842,25 @@ async def test_automation_with_error_in_script(hass, caplog): assert "Service not found" in caplog.text +async def test_automation_with_error_in_script_2(hass, caplog): + """Test automation with an error in script.""" + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "alias": "hello", + "trigger": {"platform": "event", "event_type": "test_event"}, + "action": {"service": None, "entity_id": "hello.world"}, + } + }, + ) + + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert "string value is None" in caplog.text + + async def test_automation_restore_last_triggered_with_initial_state(hass): """Ensure last_triggered is restored, even when initial state is set.""" time = dt_util.utcnow()