class Skill_Hue: def __init__(self): try: config = SnipsConfigParser.read_configuration_file(CONFIG_INI) except: config = None hostname = None code = None if config and config.get('secret', None) is not None: if config.get('secret').get('hostname', None) is not None: hostname = config.get('secret').get('hostname') if hostname == "": hostname = None if config.get('secret').get(API_KEY, None) is not None: code = config.get('secret').get(API_KEY) if code == "": code = None elif os.path.isfile(CACHE_INI): try: cached_config = SnipsConfigParser.read_configuration_file( CACHE_INI) except: cached_config = None if cached_config and cached_config.get('secret', None) is not None: if cached_config.get('secret').get(API_KEY, None) is not None: code = cached_config.get('secret').get(API_KEY) if code == "": code = None if hostname is None or code is None: print('No configuration') self.snipshue = SnipsHue(hostname, code) hostname = self.snipshue.hostname code = self.snipshue.username self.update_config(CACHE_INI, config, hostname, code) self.queue = Queue.Queue() self.thread_handler = ThreadHandler() self.thread_handler.run(target=self.start_blocking) self.thread_handler.start_run_loop() def update_config(self, filename, data, hostname, code): if not os.path.exists(CACHE_INI_DIR): os.makedirs(CACHE_INI_DIR) if 'secret' not in data or data['secret'] is None: data['secret'] = {} data['secret']['hostname'] = hostname data['secret'][API_KEY] = code SnipsConfigParser.write_configuration_file(filename, data) def start_blocking(self, run_event): while run_event.is_set(): try: self.queue.get(False) except Queue.Empty: with Hermes(MQTT_ADDR) as h: h.subscribe_intents(self.callback).start() # section -> extraction of slot value def extract_house_rooms(self, intent_message): house_rooms = [] if intent_message.slots.house_room: for room in intent_message.slots.house_room.all(): print type(room.value) house_rooms.append(room.value) return house_rooms def extract_percentage(self, intent_message, default_percentage): percentage = default_percentage if intent_message.slots.percent: percentage = intent_message.slots.percent.first().value if percentage < 0: percentage = 0 if percentage > 100: percentage = 100 return percentage def extract_color(self, intent_message): color_code = None if intent_message.slots.color: color_code = intent_message.slots.color.first().value return color_code def extract_scene(self, intent_message): scene_code = None if intent_message.slots.scene: scene_code = intent_message.slots.scene.first().value return scene_code # section -> handlers of intents def callback(self, hermes, intent_message): print("[HUE] Received") # all the intents have a house_room slot, extract here rooms = self.extract_house_rooms(intent_message) intent_name = intent_message.intent.intent_name if ':' in intent_name: intent_name = intent_name.split(":")[1] if intent_name == 'turnOn': self.queue.put(self.turn_on(hermes, intent_message, rooms)) if intent_name == 'turnOff': self.queue.put(self.turn_off(hermes, intent_message, rooms)) if intent_name == 'setBrightness': self.queue.put(self.set_brightness(hermes, intent_message, rooms)) if intent_name == 'setColor': self.queue.put(self.set_color(hermes, intent_message, rooms)) if intent_name == 'setScene': self.queue.put(self.set_scene(hermes, intent_message, rooms)) if intent_name == 'shiftUp': self.queue.put(self.shift_up(hermes, intent_message, rooms)) if intent_name == 'shiftDown': self.queue.put(self.shift_down(hermes, intent_message, rooms)) def turn_on(self, hermes, intent_message, rooms): if len(rooms) > 0: for room in rooms: self.snipshue.light_on(room.lower()) else: self.snipshue.light_on_all() self.terminate_feedback(hermes, intent_message) def turn_off(self, hermes, intent_message, rooms): if len(rooms) > 0: for room in rooms: self.snipshue.light_off(room.lower()) else: self.snipshue.light_off_all() self.terminate_feedback(hermes, intent_message) def set_brightness(self, hermes, intent_message, rooms): percent = self.extract_percentage(intent_message, None) if percent is None: self.terminate_feedback(hermes, intent_message) return if len(rooms) > 0: for room in rooms: self.snipshue.light_brightness(percent, room.lower()) else: self.snipshue.light_brightness_all(percent) self.terminate_feedback(hermes, intent_message) def set_color(self, hermes, intent_message, rooms): color = self.extract_color(intent_message) if color is None: self.terminate_feedback(hermes, intent_message) return if len(rooms) > 0: for room in rooms: self.snipshue.light_color(color, room.lower()) else: self.snipshue.light_color_all(color) self.terminate_feedback(hermes, intent_message) def set_scene(self, hermes, intent_message, rooms): scene = self.extract_scene(intent_message) if scene is None: self.terminate_feedback(hermes, intent_message) return if len(rooms) > 0: for room in rooms: self.snipshue.light_scene(scene, room.lower()) else: self.snipshue.light_scene_all(scene) self.terminate_feedback(hermes, intent_message) def shift_up(self, hermes, intent_message, rooms): percent = self.extract_percentage(intent_message, 20) if len(rooms) > 0: for room in rooms: self.snipshue.light_up(percent, room.lower()) else: self.snipshue.light_up_all(percent) self.terminate_feedback(hermes, intent_message) def shift_down(self, hermes, intent_message, rooms): percent = self.extract_percentage(intent_message, 20) if len(rooms) > 0: for room in rooms: self.snipshue.light_down(percent, room.lower()) else: self.snipshue.light_down_all(percent) self.terminate_feedback(hermes, intent_message) # section -> feedback reply // future function def terminate_feedback(self, hermes, intent_message, mode='default'): if mode == 'default': hermes.publish_end_session(intent_message.session_id, "") else: # more design hermes.publish_end_session(intent_message.session_id, "")
class Skill: def __init__(self): try: config = SnipsConfigParser.read_configuration_file(CONFIG_INI) except: config = None hostname = None code = None if config and config.get('secret') is not None: if config.get('secret').get('hostname') is not None: hostname = config.get('secret').get('hostname') if hostname == "": hostname = None if config.get('secret').get(API_KEY) is not None: code = config.get('secret').get(API_KEY) if code == "": code = None if hostname is None or code is None: print('No configuration') self.snipshue = SnipsHue(hostname, code) hostname = self.snipshue.hostname code = self.snipshue.username self.update_config(CONFIG_INI, config, hostname, code) self.queue = Queue.Queue() self.thread_handler = ThreadHandler() self.thread_handler.run(target=self.start_blocking) self.thread_handler.start_run_loop() def update_config(self, filename, data, hostname, code): if not os.path.exists(CONFIG_INI_DIR): os.makedirs(CONFIG_INI_DIR) if 'secret' not in data or data['secret'] is None: data['secret'] = {} data['secret']['hostname'] = hostname data['secret'][API_KEY] = code SnipsConfigParser.write_configuration_file(filename, data) def start_blocking(self, run_event): while run_event.is_set(): try: self.queue.get(False) except Queue.Empty: with Hermes(MQTT_ADDR) as h: h.subscribe_intents(self.callback).start() def extract_house_rooms(self, intent_message): house_rooms = [] if intent_message.slots.house_room is not None: for room in intent_message.slots.house_room: house_rooms.append(room.slot_value.value.value) return house_rooms def extract_number(self, intent_message, default_number): number = default_number if intent_message.slots.intensity_number: number = intent_message.slots.intensity_number.first().value if intent_message.slots.intensity_percent: number = intent_message.slots.intensity_percent.first().value return number def extract_up_down(self, intent_message): res = "down" if intent_message.slots.up_down: res = intent_message.slots.up_down.first().value return res def extract_color(self, intent_message): res = None if intent_message.slots.color: res = intent_message.slots.color.first().value return res pass def callback(self, hermes, intent_message): rooms = self.extract_house_rooms(intent_message) if intent_message.intent.intent_name == 'lightsTurnOff': self.queue.put(self.lights_turn_off(hermes, intent_message, rooms)) if intent_message.intent.intent_name == 'lightsTurnOnSet': self.queue.put( self.lights_turn_on_set(hermes, intent_message, rooms)) if intent_message.intent.intent_name == 'lightsShift': self.queue.put(self.lights_shift(hermes, intent_message, rooms)) if intent_message.intent.intent_name == 'lightsSet': self.queue.put( self.lights_turn_on_set(hermes, intent_message, rooms)) if intent_message.intent.intent_name == 'snips-labs:lightsColor_FR_': self.queue.put( self.lights_turn_on_set(hermes, intent_message, rooms)) if intent_message.intent.intent_name == 'snips-labs:lightsColor_EN': self.queue.put( self.lights_turn_on_set(hermes, intent_message, rooms)) def lights_turn_off(self, hermes, intent_message, rooms): hermes.publish_end_session(intent_message.session_id, None) if len(rooms) > 0: for room in rooms: self.snipshue.light_off(room) else: self.snipshue.light_off(None) def lights_turn_on_set(self, hermes, intent_message, rooms): hermes.publish_end_session(intent_message.session_id, None) number = self.extract_number(intent_message, 100) color = self.extract_color(intent_message) if len(rooms) > 0: for room in rooms: self.snipshue.light_on_set(color, number, room) else: self.snipshue.light_on_set(color, number, None) def lights_shift(self, hermes, intent_message, rooms): hermes.publish_end_session(intent_message.session_id, None) number = self.extract_number(intent_message, 20) if "down" == self.extract_up_down(intent_message): self.lights_turn_down(number, rooms) else: self.lights_turn_up(number, rooms) def lights_turn_down(self, number, rooms): if len(rooms) > 0: for room in rooms: self.snipshue.light_down(number, room) else: self.snipshue.light_down(number, None) def lights_turn_up(self, number, rooms): if len(rooms) > 0: for room in rooms: self.snipshue.light_up(number, room) else: self.snipshue.light_up(number, None)