-
Notifications
You must be signed in to change notification settings - Fork 0
/
lighting.py
33 lines (25 loc) · 876 Bytes
/
lighting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import config
import requests
import sys
# Get the command-line params
action = sys.argv[1]
brightness_percentage = sys.argv[2]
# Establish constants
HA_URL_PATH = "/api/services/homeassistant/turn_" + action
# Build the full URL
url = config.HA_BASE_URL + HA_URL_PATH
# Create some payloads
on_payload = '{"entity_id": "' + config.HA_LIGHTS_GROUP + '", "transition": 1.5, "brightness_pct": ' + brightness_percentage + '}'
off_payload = '{"entity_id": "' + config.HA_LIGHTS_GROUP + '", "transition": 1.5}'
# Choose which payload to use
if action == 'on':
payload = on_payload
if action == 'off':
payload = off_payload
# Build HTTP headers
headers = {
'Authorization': 'Bearer ' + config.BEARER_TOKEN,
'content-type': 'application/json'
}
# Send over the request to Home Assistant
response = requests.request("POST", url, data=payload, headers=headers)