Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dann Bohn committed Jan 9, 2016
1 parent fdb8f4f commit ff32e4d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv/
*.pyc
example.py
65 changes: 65 additions & 0 deletions hue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import requests
import json


class HueException(Exception):
pass


class Hue(object):
def __init__(self, **kwargs):
self.url = kwargs.pop('url', None)
self.api_key = kwargs.pop('api_key', None)

def process_errors(self, resp):
errors = [m['error']['description'] for m in resp if 'error' in m]
if errors:
raise HueException("\n".join(errors))

def get_light_id(self, **kwargs):
light_found = False
light_name = kwargs.pop('light_name', None)
url = "%s/api/%s/lights" % (self.url, self.api_key)
r = requests.get(url)
if r.status_code == 200:
lights = json.loads(r.text)
for light in lights:
try:
name = lights[light].get('name')
if name == light_name:
light_found = True
return light
except TypeError:
raise ValueError(light['error']['description'])
else:
raise ValueError('API threw %s') % (r.status_code)

if not light_found:
raise ValueError('light not found')

def change_state(self, **kwargs):
light_id = kwargs.pop('light_id', None)
on = kwargs.pop('on', False)
url = "%s/api/%s/lights/%s/state" % (self.url, self.api_key, light_id)
payload = {"on": on}
r = requests.put(url, data=json.dumps(payload))
if r.status_code != 200:
raise HueException(
"Recieved %s status code from url %s ") % (r.status_code, url)
self.process_errors(json.loads(r.text))
success = [s['success'] for s in json.loads(r.text) if 'success' in s]
return success

def off(self, **kwargs):
light_id = kwargs.pop('light_id', None)
if not light_id:
light_name = kwargs.pop('light_name', None)
light_id = self.get_light_id(light_name=light_name)
self.change_state(on=False, light_id=light_id)

def on(self, **kwargs):
light_id = kwargs.pop('light_id', None)
if not light_id:
light_name = kwargs.pop('light_name', None)
light_id = self.get_light_id(light_name=light_name)
self.change_state(on=True, light_id=light_id)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.9.1

0 comments on commit ff32e4d

Please sign in to comment.