Add default variables to script helper (#39895)
This commit is contained in:
1 parent
b5005430be
commit
aa9dff572e
9 files changed
+190
-15
No files matched your search
@@ -1134,3 +1134,57 @@ async def test_logbook_humanify_automation_triggered_event(hass):
|
||||
assert event2["domain"] == "automation"
|
||||
assert event2["message"] == "has been triggered by source of trigger"
|
||||
assert event2["entity_id"] == "automation.bye"
|
||||
|
||||
|
||||
async def test_automation_variables(hass):
|
||||
"""Test automation variables."""
|
||||
calls = async_mock_service(hass, "test", "automation")
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"variables": {
|
||||
"test_var": "defined_in_config",
|
||||
"event_type": "{{ trigger.event.event_type }}",
|
||||
},
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"value": "{{ test_var }}",
|
||||
"event_type": "{{ event_type }}",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"variables": {
|
||||
"test_var": "defined_in_config",
|
||||
},
|
||||
"trigger": {"platform": "event", "event_type": "test_event_2"},
|
||||
"condition": {
|
||||
"condition": "template",
|
||||
"value_template": "{{ trigger.event.data.pass_condition }}",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["value"] == "defined_in_config"
|
||||
assert calls[0].data["event_type"] == "test_event"
|
||||
|
||||
hass.bus.async_fire("test_event_2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.bus.async_fire("test_event_2", {"pass_condition": True})
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
@@ -23,7 +23,7 @@ from homeassistant.loader import bind_hass
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import async_mock_service, get_test_home_assistant
|
||||
from tests.components.logbook.test_init import MockLazyEventPartialState
|
||||
|
||||
ENTITY_ID = "script.test"
|
||||
@@ -615,3 +615,69 @@ async def test_concurrent_script(hass, concurrently):
|
||||
|
||||
assert not script.is_on(hass, "script.script1")
|
||||
assert not script.is_on(hass, "script.script2")
|
||||
|
||||
|
||||
async def test_script_variables(hass):
|
||||
"""Test defining scripts."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"script1": {
|
||||
"variables": {
|
||||
"test_var": "from_config",
|
||||
"templated_config_var": "{{ var_from_service | default('config-default') }}",
|
||||
},
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {
|
||||
"value": "{{ test_var }}",
|
||||
"templated_config_var": "{{ templated_config_var }}",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
"script2": {
|
||||
"variables": {
|
||||
"test_var": "from_config",
|
||||
},
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {
|
||||
"value": "{{ test_var }}",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
mock_calls = async_mock_service(hass, "test", "script")
|
||||
|
||||
await hass.services.async_call(
|
||||
"script", "script1", {"var_from_service": "hello"}, blocking=True
|
||||
)
|
||||
|
||||
assert len(mock_calls) == 1
|
||||
assert mock_calls[0].data["value"] == "from_config"
|
||||
assert mock_calls[0].data["templated_config_var"] == "hello"
|
||||
|
||||
await hass.services.async_call(
|
||||
"script", "script1", {"test_var": "from_service"}, blocking=True
|
||||
)
|
||||
|
||||
assert len(mock_calls) == 2
|
||||
assert mock_calls[1].data["value"] == "from_service"
|
||||
assert mock_calls[1].data["templated_config_var"] == "config-default"
|
||||
|
||||
# Call script with vars but no templates in it
|
||||
await hass.services.async_call(
|
||||
"script", "script2", {"test_var": "from_service"}, blocking=True
|
||||
)
|
||||
|
||||
assert len(mock_calls) == 3
|
||||
assert mock_calls[2].data["value"] == "from_service"
|
||||
Reference in new issue
Block a user