Make variables action not restricted to local scopes (#141114)

Make variables action in scripts not restricted to local scopes
This commit is contained in:
Artur Pragacz 2025-03-23 14:07:52 +01:00 committed by GitHub
parent ca10618dc7
commit 798ee60ae5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 18 deletions

View File

@ -966,12 +966,11 @@ class _ScriptRun:
## Variable actions ## ## Variable actions ##
async def _async_step_variables(self) -> None: async def _async_step_variables(self) -> None:
"""Define a local variable.""" """Assign values to variables."""
self._step_log("defining local variables") self._step_log("assigning variables")
for key, value in ( self._variables.update(
self._action[CONF_VARIABLES].async_simple_render(self._variables).items() self._action[CONF_VARIABLES].async_simple_render(self._variables)
): )
self._variables.define_local(key, value)
## External actions ## ## External actions ##

View File

@ -494,7 +494,7 @@ async def test_calling_service_response_data_in_scopes(hass: HomeAssistant) -> N
assert result.variables["my_response"] == expected_var assert result.variables["my_response"] == expected_var
expected_trace = { expected_trace = {
"0": [{"variables": {"my_response": expected_var}}], "0": [{"variables": {"my_response": expected_var, "state": "off"}}],
"0/parallel/0/sequence/0": [{"variables": {"state": "off"}}], "0/parallel/0/sequence/0": [{"variables": {"state": "off"}}],
"0/parallel/0/sequence/1": [ "0/parallel/0/sequence/1": [
{ {
@ -1797,7 +1797,7 @@ async def test_wait_in_sequence(hass: HomeAssistant) -> None:
assert result.variables["wait"] == expected_var assert result.variables["wait"] == expected_var
expected_trace = { expected_trace = {
"0": [{"variables": {"wait": expected_var}}], "0": [{"variables": {"wait": expected_var, "state": "off"}}],
"0/sequence/0": [{"variables": {"state": "off"}}], "0/sequence/0": [{"variables": {"state": "off"}}],
"0/sequence/1": [ "0/sequence/1": [
{ {
@ -1840,7 +1840,7 @@ async def test_wait_in_parallel(hass: HomeAssistant) -> None:
assert "wait" not in result.variables assert "wait" not in result.variables
expected_trace = { expected_trace = {
"0": [{}], "0": [{"variables": {"state": "off"}}],
"0/parallel/0/sequence/0": [{"variables": {"state": "off"}}], "0/parallel/0/sequence/0": [{"variables": {"state": "off"}}],
"0/parallel/0/sequence/1": [ "0/parallel/0/sequence/1": [
{ {
@ -5277,11 +5277,23 @@ async def test_set_variable(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test setting variables in scripts.""" """Test setting variables in scripts."""
alias = "variables step"
sequence = cv.SCRIPT_SCHEMA( sequence = cv.SCRIPT_SCHEMA(
[ [
{"alias": alias, "variables": {"variable": "value"}}, {"alias": "variables", "variables": {"x": 1, "y": 1}},
{"action": "test.script", "data": {"value": "{{ variable }}"}}, {
"alias": "scope",
"sequence": [
{"variables": {"y": 3, "z": 3}},
{
"action": "test.script",
"data": {"value": "x={{ x }}, y={{ y }}, z={{ z }}"},
},
],
},
{
"action": "test.script",
"data": {"value": "x={{ x }}, y={{ y }}, z={{ z }}"},
},
] ]
) )
script_obj = script.Script(hass, sequence, "test script", "test_domain") script_obj = script.Script(hass, sequence, "test script", "test_domain")
@ -5291,18 +5303,36 @@ async def test_set_variable(
await script_obj.async_run(context=Context()) await script_obj.async_run(context=Context())
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_calls[0].data["value"] == "value" assert len(mock_calls) == 2
assert f"Executing step {alias}" in caplog.text assert mock_calls[0].data["value"] == "x=1, y=3, z=3"
assert mock_calls[1].data["value"] == "x=1, y=3, z=3"
assert "Executing step variables" in caplog.text
expected_trace = { expected_trace = {
"0": [{"variables": {"variable": "value"}}], "0": [{"variables": {"x": 1, "y": 1}}],
"1": [ "1": [{"variables": {"y": 3, "z": 3}}],
"1/sequence/0": [{"variables": {"y": 3, "z": 3}}],
"1/sequence/1": [
{ {
"result": { "result": {
"params": { "params": {
"domain": "test", "domain": "test",
"service": "script", "service": "script",
"service_data": {"value": "value"}, "service_data": {"value": "x=1, y=3, z=3"},
"target": {},
},
"running_script": False,
},
}
],
"2": [
{
"result": {
"params": {
"domain": "test",
"service": "script",
"service_data": {"value": "x=1, y=3, z=3"},
"target": {}, "target": {},
}, },
"running_script": False, "running_script": False,
@ -5899,7 +5929,9 @@ async def test_stop_action_nested_response_variables(
"variables": {"var": var, "output": {"value": "Testing 123"}}, "variables": {"var": var, "output": {"value": "Testing 123"}},
} }
], ],
"1": [{"result": {"choice": choice}}], "1": [
{"result": {"choice": choice}, "variables": {"output": {"value": response}}}
],
"1/if": [{"result": {"result": if_result}}], "1/if": [{"result": {"result": if_result}}],
"1/if/condition/0": [{"result": {"result": var == 1, "entities": []}}], "1/if/condition/0": [{"result": {"result": var == 1, "entities": []}}],
f"1/{choice}/0": [{"variables": {"output": {"value": response}}}], f"1/{choice}/0": [{"variables": {"output": {"value": response}}}],