class HermesSnipsComponent(SnipsComponent): """A Snips component using the Hermes Python library. Attributes: snips (:class:`.SnipsConfig`): The Snips configuration. hermes (:class:`hermes_python.hermes.Hermes`): The Hermes object. """ def _connect(self): """Connect with the MQTT broker referenced in the snips configuration file. """ mqtt_options = self.snips.mqtt self.hermes = Hermes(mqtt_options=MqttOptions( mqtt_options.broker_address, mqtt_options.auth.username, mqtt_options.auth.password, mqtt_options.tls.hostname, mqtt_options.tls.ca_file, mqtt_options.tls.ca_path, mqtt_options.tls.client_key, mqtt_options.tls.client_cert, mqtt_options.tls.disable_root_store)) self.hermes.connect() self._register_callbacks() def _start(self): """Start the event loop to the Hermes object so the component starts listening to events and the callback methods are called. """ self.hermes.loop_forever() def _register_callbacks(self): """Subscribe to the Hermes events we're interested in. Each method with an attribute set by a decorator is registered as a callback for the corresponding event. """ for name in dir(self): callable_name = getattr(self, name) # If we have given the method a subscribe_method attribute by one # of the decorators. if hasattr(callable_name, 'subscribe_method'): subscribe_method = getattr(callable_name, 'subscribe_method') # If we have given the method a subscribe_parameter attribute # by one of the decorators. if hasattr(callable_name, 'subscribe_parameter'): subscribe_parameter = getattr(callable_name, 'subscribe_parameter') # Register callable_name as a callback with # subscribe_method and subscribe_parameter as a parameter. getattr(self.hermes, subscribe_method)(subscribe_parameter, callable_name) else: # Register callable_name as a callback with # subscribe_method. getattr(self.hermes, subscribe_method)(callable_name)
class Assistant: def __init__(self): self.intents = {} self.last_message = None snips_config = toml.load('/etc/snips.toml') mqtt_username = None mqtt_password = None mqtt_broker_address = "localhost:1883" if 'mqtt' in snips_config['snips-common'].keys(): mqtt_broker_address = snips_config['snips-common']['mqtt'] if 'mqtt_username' in snips_config['snips-common'].keys(): mqtt_username = snips_config['snips-common']['mqtt_username'] if 'mqtt_password' in snips_config['snips-common'].keys(): mqtt_password = snips_config['snips-common']['mqtt_password'] mqtt_opts = MqttOptions(username=mqtt_username, password=mqtt_password, broker_address=mqtt_broker_address) self.hermes = Hermes(mqtt_options=mqtt_opts) self.conf = read_configuration_file() if 'OPENHAB_SERVER_URL' in environ: self.conf['secret']['openhab_server_url'] = environ.get( 'OPENHAB_SERVER_URL') if 'OPENHAB_DEFAULT_ROOM' in environ: self.conf['secret']['default_room'] = environ.get( 'OPENHAB_DEFAULT_ROOM') if 'OPENHAB_SITEID2ROOM_MAPPING' in environ: self.conf['secret']['siteid2room_mapping'] = environ.get( 'OPENHAB_SITEID2ROOM_MAPPING') if 'OPENHAB_SOUND_FEEDBACK' in environ: self.conf['secret']['sound_feedback'] = environ.get( 'OPENHAB_SOUND_FEEDBACK') self.sound_feedback = self.conf['secret']["sound_feedback"] == "on" def add_callback(self, intent_name, callback): self.intents[intent_name] = callback def register_sound(self, sound_name, sound_data): self.hermes.register_sound(RegisterSoundMessage( sound_name, sound_data)) def callback(self, intent_message): intent_name = intent_message.intent.intent_name if intent_name in self.intents: success, message = self.intents[intent_name](self, intent_message, self.conf) self.last_message = message if self.sound_feedback: if success is None: self.hermes.publish_end_session(intent_message.session_id, message) elif success: self.hermes.publish_end_session(intent_message.session_id, "[[sound:success]]") else: # TODO: negative sound self.hermes.publish_end_session(intent_message.session_id, message) else: self.hermes.publish_end_session(intent_message.session_id, message) def inject(self, entities): self.hermes.request_injection( InjectionRequestMessage([AddFromVanillaInjectionRequest(entities) ])) def __enter__(self): self.hermes.connect() return self def __exit__(self, exception_type, exception_val, trace): if not exception_type: self.hermes.disconnect() return self return False def start(self): def helper_callback(hermes, intent_message): self.callback(intent_message) with open(path.join(path.dirname(__file__), 'success.wav'), 'rb') as f: self.register_sound("success", bytearray(f.read())) self.hermes.subscribe_intents(helper_callback) self.hermes.start()