Skip to content

Commit 3dec0a3

Browse files
skalavalafabaff
authored andcommitted
added service call example (home-assistant#4606)
* added service call example * Minor changes
1 parent 99383a0 commit 3dec0a3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

source/_components/python_script.markdown

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ hass.bus.fire(name, { "wow": "from a Python script!" })
4444
}
4545
```
4646

47-
For examples, visit the [Scripts section](https://community.home-assistant.io/c/projects/scripts) in our forum.
47+
## {% linkable_title Calling Services %}
48+
49+
The following example shows how to call a service from `python_script`. This script takes two parameters: `entity_id` (required), `rgb_color` (optional) and calls `light.turn_on` service by setting the brightness value to `255`.
50+
51+
```python
52+
entity_id = data.get('entity_id')
53+
rgb_color = data.get('rgb_color', [255, 255, 255])
54+
if entity_id is not None:
55+
service_data = {'entity_id': entity_id, 'rgb_color': rgb_color, 'brightness': 255 }
56+
hass.services.call('light', 'turn_on', service_data, False)
57+
```
58+
The above `python_script` can be called using the following JSON as an input.
59+
60+
```json
61+
{"entity_id": "light.bedroom", "rgb_color": [255, 0, 0] }
62+
```
63+
64+
For more examples, visit the [Scripts section](https://community.home-assistant.io/c/projects/scripts) in our forum.

0 commit comments

Comments
 (0)