def get(self, resource={'which': 'all'}): """ @summary: Get all lights, get new lights, or get a specific light as determined by the resource object. """ rest_obj = RestObject() services = { 'all': { 'service': 'lights' }, 'new': { 'service': 'lights/new' } } if (isinstance(resource['which'], int)): resource['id'] = resource['which'] resource['which'] = 'one' if (resource['which'] == 'one'): services['one'] = { 'service': 'lights/{id}'.format(id=resource['id']) } service = services[resource['which']]['service'] path = 'api/{username}/{service}'.format(username=self.user['name'], service=service) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.get(url) if service == 'lights': lights = [] for (k, v) in response.items(): v['id'] = int(k) lights.append(v) response = lights return dict(resource=response)
def get(self, resource={'which':'all'}): """ @summary: Get all lights, get new lights, or get a specific light as determined by the resource object. """ rest_obj = RestObject() services = { 'all':{'service':'lights'}, 'new':{'service':'lights/new'} } if (isinstance(resource['which'], int)): resource['id'] = resource['which'] resource['which'] = 'one' if (resource['which'] == 'one'): services['one'] = {'service':'lights/{id}'.format(id=resource['id'])} service = services[resource['which']]['service'] path = 'api/{username}/{service}'.format(username=self.user['name'], service=service) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.get(url) if service == 'lights': lights = [] for (k, v) in response.items(): v['id'] = int(k) lights.append(v) response = lights return dict(resource=response)
def findNewLights(self): """ @summary: Search for new lights. """ rest_obj = RestObject() path = 'api/{username}/lights'.format(username=self.user['name']) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.post(url) return dict(resource=response)
def createUser(self, user): """ @summary: Create an user on the Hue Bridge. @param: user -> String with username to create. @return: Info about operation. """ rest_obj = RestObject() url = 'http://{bridge_ip}/api'.format(bridge_ip=self.bridge['ip']) resource = {'devicetype': user, 'username': user} response = rest_obj.post(url, resource) return dict(resource=response)
def deleteUser(self, user): """ @summary: Delete an user from Hue Bridge. @param: user -> String with username to delete. @return: Info about operation. """ rest_obj = RestObject() service = 'config/whitelist/{id}'.format(id=user) path = 'api/{username}/{service}'.format(username=self.user['name'], service=service) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.delete(url) return dict(resource=response)
def getNumLights(self): """ @summary: Get num of Lights connected. @return: Num of lights. """ rest_obj = RestObject() path = 'api/{username}/lights'.format(username=self.user['name']) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.get(url) lights = [] for (k, v) in response.items(): v['id'] = int(k) lights.append(v) return len(lights)
def isConnected(self): """ @summary: See if Hue is connected. @return: Boolean which determines if there is connection stablished, and string with connection message. """ rest_obj = RestObject() path = 'api/{username}'.format(username=self.user['name']) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.get(url) response = dict(resource=response) if 'lights' in response['resource']: return True elif 'error' in response['resource'][0]: error = response['resource'][0]['error'] if error['type'] == 1: return False
def update(self, resource): """ @summary: Rename lights, or set a light's state, as determined by the\ resource object. """ rest_obj = RestObject() if (resource['data'].has_key('attr')): service = 'lights/{id}'.format(id=resource['which']) data = resource['data']['attr'] elif (resource['data'].has_key('state')): service = 'lights/{id}/state'.format(id=resource['which']) data = resource['data']['state'] else: raise Exception('Unknown data type.') path = 'api/{username}/{service}'.format(username=self.user['name'], service=service) url = 'http://{bridge_ip}/{path}'.format(bridge_ip=self.bridge['ip'], path=path) response = rest_obj.put(url, data) return dict(resource=response)