Allow returning a script variable from a script (#95346)

* Allow returning a script variable from a script

* Don't allow returning a template result

* Raise if response variable is undefined

* Add test

* Update homeassistant/helpers/script.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* Format code

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Erik Montnemery 2023-06-27 17:13:53 +02:00 committed by GitHub
parent e19b29d6ae
commit cb22fb16f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 19 deletions

View file

@ -46,7 +46,6 @@ from homeassistant.const import (
CONF_MODE,
CONF_PARALLEL,
CONF_REPEAT,
CONF_RESPONSE,
CONF_RESPONSE_VARIABLE,
CONF_SCENE,
CONF_SEQUENCE,
@ -1031,10 +1030,14 @@ class _ScriptRun:
raise _AbortScript(stop)
self._log("Stop script sequence: %s", stop)
if CONF_RESPONSE in self._action:
response = template.render_complex(
self._action[CONF_RESPONSE], self._variables
)
if CONF_RESPONSE_VARIABLE in self._action:
try:
response = self._variables[self._action[CONF_RESPONSE_VARIABLE]]
except KeyError as ex:
raise _AbortScript(
f"Response variable '{self._action[CONF_RESPONSE_VARIABLE]}' "
"is not defined"
) from ex
else:
response = None
raise _StopScript(stop, response)