Skip to content

Commit

Permalink
Added bulbs object, changed a lot of things
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanaël Lécaudé authored and Nathanaël Lécaudé committed Nov 5, 2012
1 parent dc3c623 commit 33c2141
Showing 1 changed file with 117 additions and 15 deletions.
132 changes: 117 additions & 15 deletions hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,99 @@

# Original protocol hacking by rsmck : http://rsmck.co.uk/hue

class Hue:
class Bulb(object):
def __init__(self, bridge, light_id):
self.bridge = bridge
self.light_id = light_id

self._name = None
self._on = None
self._brightness = None
self._hue = None
self._saturation = None
self._xy = None
self._colortemp = None


@property
def name(self):
self._name = self.bridge.get_state(self.light_id)['name']
return self._name

@name.setter
def name(self, value):
self._name = value
self.bridge.set_state(self.light_id, "name", self._name)

@property
def on(self):
self._on = self.bridge.get_state(self.light_id)['state']['on']
return self._on

@on.setter
def on(self, value):
self._on = value
self.bridge.set_light_state(self.light_id, "on", self._on)

@property
def brightness(self):
self._brightness = self.bridge.get_state(self.light_id)['state']['bri']
return self._brightness

@brightness.setter
def brightness(self, value):
self._brightness = value
self.bridge.set_light_state(self.light_id, "bri", self._brightness)

@property
def hue(self):
self._hue = self.bridge.get_state(self.light_id)['state']['hue']
return self._hue

@hue.setter
def hue(self, value):
self._hue = value
self.bridge.set_light_state(self.light_id, "hue", self._hue)

@property
def saturation(self):
self._saturation = self.bridge.get_state(self.light_id)['state']['sat']
return self._saturation

@saturation.setter
def saturation(self, value):
self._saturation = value
self.bridge.set_light_state(self.light_id, "sat", self._saturation)

@property
def xy(self):
self._xy = self.bridge.get_state(self.light_id)['state']['xy']
return self._xy

@xy.setter
def xy(self, value):
self._xy = value
self.bridge.set_light_state(self.light_id, "xy", self._xy)

@property
def colortemp(self):
self._colortemp = self.bridge.get_state(self.light_id)['state']['ct']
return self._colortemp

@colortemp.setter
def colortemp(self, value):
self._colortemp = value
self.bridge.set_light_state(self.light_id, "ct", self._colortemp)

class Bridge(object):
def __init__(self, bridge_ip):
self.config_file = os.path.join(os.getenv("HOME"),'.python_hue')
self.bridge_ip = bridge_ip
self.bridge_api_url = 'http://' + self.bridge_ip + '/api/'
self.bulbs = {}
self.username = None

self.connect()

def register_app(self):
registration_request = {"username": "python_hue", "devicetype": "python_hue"}
Expand All @@ -37,31 +124,46 @@ def connect(self):
with open(self.config_file) as f:
self.username = json.loads(f.read())[self.bridge_ip]['username']
print 'Using username: ' + self.username
print 'Getting bulb information...'
self.refresh_bulbs()

except Exception as e:
print 'Error opening config file, will attempt bridge registration'
self.register_app()

def refresh_bulbs(self):
bulbs = json.loads(urllib2.urlopen(self.bridge_api_url + self.username + '/lights/').read())
for bulb in bulbs:
self.bulbs[int(bulb)] = Bulb(self, int(bulb))
self.bulbs[bulbs[bulb]['name']] = self.bulbs[int(bulb)]


# With no arguments, prints the whole bridge information, with a lamp_id, prints the lamp information.
def get_info(self, lamp_id = None):
if lamp_id != None:
u = urllib2.urlopen(self.bridge_api_url + self.username + '/lights/' + str(lamp_id))
# Sets the global parameters of the light (the name mainly)
def get_state(self, light_id = None):
if light_id != None:
u = urllib2.urlopen(self.bridge_api_url + self.username + '/lights/' + str(light_id))
else:
u = urllib2.urlopen(self.bridge_api_url + self.username)
for line in u.readlines():
print json.dumps(json.loads(line), indent=4)
return json.loads(line)

# lamp_id can be a single lamp or an array or lamps
def set_state(self, light_id, parameter, value):
data = {parameter : value}
connection = httplib.HTTPConnection(self.bridge_ip + ':80')
connection.request('PUT', '/api/' + self.username + '/lights/'+ str(light_id), json.dumps(data))
result = connection.getresponse()
print result.read()

# light_id can be a single lamp or an array or lamps
# parameters: 'on' : True|False , 'bri' : 0-254, 'sat' : 0-254, 'ct': 154-500
def set_state(self, lamp_id, parameter, value):
def set_light_state(self, light_id, parameter, value):
data = {parameter : value}
connection = httplib.HTTPConnection(self.bridge_ip + ':80')
lamp_id_array = lamp_id
if type(lamp_id) == int:
lamp_id_array = [lamp_id]
for lamp in lamp_id_array:
connection.request('PUT', '/api/' + self.username + '/lights/'+ str(lamp) + '/state', json.dumps(data))
light_id_array = light_id
if type(light_id) == int:
light_id_array = [light_id]
for light in light_id_array:
connection.request('PUT', '/api/' + self.username + '/lights/'+ str(light) + '/state', json.dumps(data))
result = connection.getresponse()
print result.read()



0 comments on commit 33c2141

Please sign in to comment.