def poll(self): """ Poll Hue for new lights/existing lights' statuses """ if self.hub is None: return True api = self._get_api() if not api: return False lights = api['lights'] manifest = self.config.get('manifest', {}) for lamp_id, data in lights.items(): address = id_2_addr(data['uniqueid']) name = data['name'] if address not in self.nodes: self.nodes[address] = HueColorLight(self, address, name, lamp_id, manifest) (color_x, color_y) = [round(val, 4) for val in data['state']['xy']] brightness = round(data['state']['bri'] / 255. * 100., 4) brightness = brightness if data['state']['on'] else 0 self.nodes[address].set_driver('GV1', color_x) self.nodes[address].set_driver('GV2', color_y) self.nodes[address].set_driver('ST', brightness) return True
def poll(self): """ Poll Hue for new lights/existing lights' statuses """ if self.hub is None: return True api = self._get_api() if not api: return False lights = api['lights'] manifest = self.config.get('manifest', {}) for lamp_id, data in lights.items(): address = id_2_addr(data['uniqueid']) name = data['name'] if address not in self.nodes: self.nodes[address] = HueColorLight( self, address, name, lamp_id, manifest) (color_x, color_y) = [round(val, 4) for val in data['state']['xy']] brightness = round(data['state']['bri'] / 255. * 100., 4) brightness = brightness if data['state']['on'] else 0 self.nodes[address].set_driver('GV1', color_x) self.nodes[address].set_driver('GV2', color_y) self.nodes[address].set_driver('ST', brightness) return True
def query_node(self, lkp_address): """ find specific node in api. """ api = self._get_api() if not api: return False lights = api['lights'] for data in lights.values(): address = id_2_addr(data['uniqueid']) if address == lkp_address: (color_x, color_y) = [round(val, 4) for val in data['state']['xy']] brightness = round(data['state']['bri'] / 255. * 100., 4) brightness = brightness if data['state']['on'] else 0 return (color_x, color_y, brightness)
def discover(self, command=None): LOGGER.info('Discovering Nest Products...') if self.auth_token is None: return False if not self.getState(): return False self.discovery = True ''' Copy initial data if REST Streaming is not active yet ''' if self.data is None: self.data = self.api_data if 'structures' not in self.api_data: LOGGER.error('Nest API did not return any structures') self.discovery = False return False structures = self.api_data['structures'] LOGGER.info("Found {} structure(s)".format(len(structures))) for struct_id, struct in structures.items(): address = id_2_addr(struct_id) LOGGER.info("Id: {}, Name: {}".format(address, struct['name'])) if address not in self.nodes: self.addNode(Structure(self, self.address, address, struct['name'], struct_id, struct)) if 'thermostats' in self.api_data['devices']: thermostats = self.api_data['devices']['thermostats'] LOGGER.info("Found {} thermostat(s)".format(len(thermostats))) for tstat_id, tstat in thermostats.items(): address = id_2_addr(tstat_id) LOGGER.info("Id: {}, Name: {}".format(address, tstat['name_long'])) if address not in self.nodes: if tstat['temperature_scale'] == 'F': self.addNode(Thermostat(self, self.address, address, tstat['name'], tstat_id, tstat), update=self.update_nodes) else: self.addNode(ThermostatC(self, self.address, address, tstat['name'], tstat_id, tstat), update=self.update_nodes) if 'smoke_co_alarms' in self.api_data['devices']: smokedets = self.api_data['devices']['smoke_co_alarms'] LOGGER.info("Found {} smoke detector(s)".format(len(smokedets))) for smkdet_id, smkdet in smokedets.items(): address = id_2_addr(smkdet_id) LOGGER.info("Id: {}, Name: {}".format(address, smkdet['name_long'])) if address not in self.nodes: self.addNode(Protect(self, self.address, address, smkdet['name'], smkdet_id, smkdet), update=self.update_nodes) if 'cameras' in self.api_data['devices']: cams = self.api_data['devices']['cameras'] LOGGER.info("Found {} camera(s)".format(len(cams))) for cam_id, camera in cams.items(): address = id_2_addr(cam_id) LOGGER.info("Id: {}, Name: {}".format(address, camera['name_long'])) if address not in self.nodes: self.addNode(Camera(self, self.address, address, camera['name'], cam_id, camera), update=self.update_nodes) self.discovery = False self.update_nodes = False return True
def _discover(self, hub_idx): """ Poll Hue for new lights/existing lights' statuses """ if self.hub[hub_idx] is None or self.discovery == True: return True self.discovery = True LOGGER.info('Hub {} Starting Hue discovery...'.format(hub_idx)) self.lights[hub_idx] = self._get_lights(hub_idx) if not self.lights[hub_idx]: LOGGER.error( 'Hub {} Discover: Failed to read Lights from the Hue Bridge'. format(hub_idx)) self.discovery = False return False LOGGER.info( 'Hub {} {} bulbs found. Checking status and adding to ISY if necessary.' .format(hub_idx, len(self.lights[hub_idx]))) for lamp_id, data in self.lights[hub_idx].items(): address = id_2_addr(data['uniqueid']) name = data['name'] if not address in self.nodes: if data['type'] == "Extended color light": LOGGER.info( 'Hub {} Found Extended Color Bulb: {}({})'.format( hub_idx, name, address)) self.addNode( HueEColorLight(self, self.address, address, name, lamp_id, data, hub_idx)) elif data['type'] == "Color light": LOGGER.info('Hub {} Found Color Bulb: {}({})'.format( hub_idx, name, address)) self.addNode( HueColorLight(self, self.address, address, name, lamp_id, data, hub_idx)) elif data['type'] == "Color temperature light": LOGGER.info( 'Hub {} Found White Ambiance Bulb: {}({})'.format( hub_idx, name, address)) self.addNode( HueWhiteLight(self, self.address, address, name, lamp_id, data, hub_idx)) elif data['type'] == "Dimmable light": LOGGER.info('Hub {} Found Dimmable Bulb: {}({})'.format( hub_idx, name, address)) self.addNode( HueDimmLight(self, self.address, address, name, lamp_id, data, hub_idx)) else: LOGGER.info( 'Hub {} Found Unsupported {} Bulb: {}({})'.format( hub_idx, data['type'], name, address)) self.scenes[hub_idx] = self._get_scenes(hub_idx) if not self.scenes[hub_idx]: LOGGER.error( 'Hub {} Discover: Failed to read Scenes from the Hue Bridge'. format(hub_idx)) self.groups[hub_idx] = self._get_groups(hub_idx) if not self.groups[hub_idx]: LOGGER.error( 'Hub {} Discover: Failed to read Groups from the Hue Bridge'. format(hub_idx)) self.discovery = False return False LOGGER.info( 'Hub {} {} groups found. Checking status and adding to ISY if necessary.' .format(hub_idx, len(self.groups[hub_idx]))) for group_id, data in self.groups[hub_idx].items(): scene_idx = 0 if len(self.hub) > 1: address = 'huegrp' + hub_idx.split('.')[-1] + group_id else: address = 'huegrp' + group_id if group_id == '0': name = 'All Lights' else: name = data['name'] if 'lights' in data and len(data['lights']) > 0: if not address in self.nodes: LOGGER.info("Hub {} Found {} {} with {} light(s)".format( hub_idx, data['type'], name, len(data['lights']))) self.addNode( HueGroup(self, self.address, address, name, group_id, data, hub_idx)) if self.scenes[hub_idx]: for scene_id, scene_data in self.scenes[hub_idx].items( ): if 'group' in scene_data: if scene_data['group'] == group_id: self.scene_lookup.append({ "hub": hub_idx, "group": int(group_id), "idx": scene_idx, "id": scene_id, "name": scene_data['name'] }) LOGGER.info( f"Hub {hub_idx} {data['type']} {name} {scene_data['type']} {scene_idx}:{scene_id}:{scene_data['name']}" ) scene_idx += 1 else: if address in self.nodes: LOGGER.info( "Hub {} {} {} does not have any lights in it, removing a node" .format(hub_idx, data['type'], name)) self.delNode(address) LOGGER.info('Hub {} Discovery complete'.format(hub_idx)) self.discovery = False return True