-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleHUE.py
48 lines (36 loc) · 1.31 KB
/
SimpleHUE.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Simple Phillips Hue Client
"""
import requests
import json
class SimpleHUE:
base_url = ''
username = ''
def __init__(self, base_url, username):
self.base_url = base_url
self.username = username
if not self.base_url:
raise Exception('Hue Bridge IP not set')
if not self.username:
raise Exception('Username not set')
def get_lights(self):
response = requests.get(self.__request_url()).json()
return response
def turn_on(self, id):
body = {'on': True}
body = json.dumps(body)
response = requests.put(self.__request_url() + '/' + str(id) + '/state', data=body).json()
return response
def turn_of(self, id):
body = {'on': False}
body = json.dumps(body)
response = requests.put(self.__request_url() + '/' + str(id) + '/state', data=body).json()
return response
def set_color(self, id, sat, brightness, hue):
body = {'on': True, 'sat': sat, 'bri': brightness, 'hue': hue}
body = json.dumps(body)
response = requests.put(self.__request_url() + '/' + str(id) + '/state', data=body).json()
return response
def __request_url(self):
request_url = self.base_url + '/api/' + self.username + '/lights'
return request_url