mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-04-30 00:07:35 +00:00
1.8 KiB
1.8 KiB
layout | title | description | date | sidebar | comments | sharing | footer | logo | ha_category | ha_release |
---|---|---|---|---|---|---|---|---|---|---|
page | Python Scripts | Instructions how to setup Python scripts within Home Assistant. | 2017-06-15 19:59 | true | false | true | true | home-assistant.png | Automation | 0.47 |
This component allows you to write Python scripts that are exposed as services in Home Assistant. Each Python file created in the <config>/python_scripts/
folder will be exposed as a service. The content is not cached so you can easily develop: edit file, save changes, call service. The scripts are run in a sandboxed environment. The following variables are available in the sandbox:
Name | Description |
---|---|
hass |
The Home Assistant object. Access is only allowed to call services, set/remove states and fire events. API reference |
data |
The data passed to the Python Script service call. |
logger |
A logger to allow you to log messages: logger.info() , logger.warning() , logger.error() . API reference |
{% linkable_title Writing your first script %}
- Add to
configuration.yaml
:python_script:
- Create folder
<config>/python_scripts
- Create a file
hello_world.py
in the folder and give it this content:
name = data.get('name', 'world')
logger.info("Hello {}".format(name))
hass.bus.fire(name, { "wow": "from a Python script!" })
- Start Home Assistant
- Call service
python_script/hello_world
with parameters
{
"name": "you"
}
For python_script:
examples visit the Scripts section in our forum.