mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-23 01:06:52 +00:00
Add python_script response documentation (#28477)
* Update python_script.markdown * Update python_script.markdown * Fix missing bracket
This commit is contained in:
parent
837d89c5bb
commit
6192c0bd10
@ -19,6 +19,7 @@ This integration allows you to write Python scripts that are exposed as services
|
||||
| `time` | The stdlib `time` available as limited access.
|
||||
| `datetime` | The stdlib `datetime` available as limited access.
|
||||
| `dt_util` | The ` homeassistant.util.dt` module.
|
||||
| `output` | An empty dictionary. Add items to return data as [`response_variable`](/docs/scripts/service-calls#use-templates-to-handle-response-data).
|
||||
|
||||
Other imports like `min`, `max` are available as builtins. See the [python_script](https://github.com/home-assistant/core/blob/dev/homeassistant/components/python_script/__init__.py) source code for up-to-date information on the available objects inside the script.
|
||||
|
||||
@ -136,6 +137,29 @@ service_data = {"type": "daily", "entity_id": ["weather.YOUR_HOME", "weather.YOU
|
||||
current_forecast = hass.services.call("weather", "get_forecasts", service_data, blocking=True, return_response=True)
|
||||
```
|
||||
|
||||
## Returning data
|
||||
|
||||
Python script itself can respond with data. Just add items to the `output` variable in your `python_script` and the whole dictionary will be returned. These can be used in automations to act upon the command results using [`response_variable`](/docs/scripts/service-calls#use-templates-to-handle-response-data).
|
||||
|
||||
```python
|
||||
# hello_world.py
|
||||
output["hello"] = f"hello {data.get('name', 'world')}"
|
||||
```
|
||||
|
||||
The above `python_script` can be called using the following YAML and return a result to later steps.
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
- service: python_script.hello_world
|
||||
response_variable: python_script_output
|
||||
- service: notify.mobile_app_iphone
|
||||
data:
|
||||
message: "{{ python_script_output['hello'] }}"
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Documenting your Python scripts
|
||||
|
||||
You can add names and descriptions for your Python scripts that will be shown in the frontend. To do so, simply create a `services.yaml` file in your `<config>/python_scripts` folder. Using the above Python script as an example, the `services.yaml` file would look like:
|
||||
|
Loading…
x
Reference in New Issue
Block a user