def start_blocking(self): 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) #with open('sounds/jokes/badumts_extreme.wav', 'rb') as f: # self.hermes.register_sound(RegisterSoundMessage("test", f.read())) #todo dieser Aufruf führt zu segmentation fault fehler 139 self.hermes.subscribe_intents(self.master_intent_callback).start() with Hermes(mqtt_options=mqtt_opts) as h: h.subscribe_intents(self.master_intent_callback).start()
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_ROOM_OF_DEVICE_DEFAULT' in environ: self.conf['secret']['room_of_device_default'] = environ.get( 'OPENHAB_ROOM_OF_DEVICE_DEFAULT') 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 test_subscribe_intent_correctly_registers_two_callbacks_for_same_intent( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): def user_callback_1(hermes, intentMessage): pass def user_callback_2(hermes, intentMessage): pass ffi = FFI() hermes_client = mock.Mock() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) ffi.dialogue.register_subscribe_intent_handler(DUMMY_INTENT_NAME, user_callback_1, hermes_client) ffi.dialogue.register_subscribe_intent_handler(DUMMY_INTENT_NAME, user_callback_2, hermes_client) assert len(ffi.dialogue._c_callback_subscribe_intent) == 2 hermes_protocol_handler_new_mqtt.assert_called_once( ) # connection is established hermes_protocol_handler_dialogue_facade.assert_called_once( ) # connection is established assert ffi_utils.hermes_dialogue_subscribe_intent_json.call_count == 2
def test_establishing_successful_connection( hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) hermes_protocol_handler_new_mqtt.assert_called_once() hermes_protocol_handler_dialogue_facade.assert_called_once()
def test_publish_start_session_with_notification_success_json( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) start_session_message_with_notification = {"test": "test"} ffi.dialogue.publish_start_session(start_session_message_with_notification) ffi_utils.hermes_dialogue_publish_start_session_json.assert_called_once()
def test_publish_end_session_success_json( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) end_session_message = {"session_id": "session_id", "text": "ok"} ffi.dialogue.publish_end_session(end_session_message) ffi_utils.hermes_dialogue_publish_end_session_json.assert_called_once()
def test_release_connection_sucessful(hermes_protocol_handler_new_mqtt, hermes_drop_dialogue_facade, hermes_protocol_handler_dialogue_facade): ffi = FFI() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) ffi.release_connection() hermes_protocol_handler_new_mqtt.assert_called_once() hermes_protocol_handler_dialogue_facade.assert_called_once() hermes_drop_dialogue_facade.assert_called_once()
def test_release_connection_calls_api_subsets( hermes_protocol_handler_new_mqtt): ffi = FFI() mqtt_opts = MqttOptions() # Here, you have to mock every API subset of Hermes Protocol mocked_dialogue_ffi = mock.Mock() ffi.dialogue = mocked_dialogue_ffi ffi.release_connection() ffi.dialogue.release_facade.assert_called_once()
def test_publish_end_session_success(ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI(use_json_api=False) mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) end_session_message = EndSessionMessage( "session_id", "I end the session with this text") ffi.dialogue.publish_end_session(end_session_message) ffi_utils.hermes_dialogue_publish_end_session.assert_called_once()
def test_publish_continue_session_success( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI(use_json_api=False) mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) continue_session_message = ContinueSessionMessage("session_id", "text", "intent_filter", "custom_data", False) ffi.dialogue.publish_continue_session(continue_session_message) ffi_utils.hermes_dialogue_publish_continue_session.assert_called_once()
def test_publish_start_session_with_notification_success( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): ffi = FFI(use_json_api=False) mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) session_init = SessionInitNotification("hello world!") start_session_message_with_notification = StartSessionMessage( session_init, custom_data=None, site_id=None) ffi.dialogue.publish_start_session(start_session_message_with_notification) ffi_utils.hermes_dialogue_publish_start_session.assert_called_once()
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 create_hermes(): """ Create instance of hermes with mqtt options from snips toml file """ broker_address = "localhost:1883" username = None password = None if (snips_toml is not None): if ("mqtt" in snips_toml["snips-common"].keys()): broker_address = snips_toml["snips-common"]["mqtt"] if ("mqtt_username" in snips_toml["snips-common"].keys()): username = snips_toml["snips-common"]["mqtt_username"] if ("mqtt_password" in snips_toml["snips-common"].keys()): password = snips_toml["snips-common"]["mqtt_password"] mqtt_opts = MqttOptions(username=username, password=password, broker_address=broker_address) hermes = Hermes(mqtt_options=mqtt_opts) return hermes
def test_subscribe_session_ended_correctly_registers_callback( ffi_utils, hermes_protocol_handler_new_mqtt, hermes_protocol_handler_dialogue_facade): def user_callback(hermes, intentMessage): pass ffi = FFI() mqtt_opts = MqttOptions() ffi.establish_connection(mqtt_opts) hermes_client = mock.Mock() ffi.dialogue.register_session_ended_handler(user_callback, hermes_client) assert ffi.dialogue._c_callback_subscribe_session_ended is not None hermes_protocol_handler_new_mqtt.assert_called_once( ) # connection is established hermes_protocol_handler_dialogue_facade.assert_called_once( ) # connection is established ffi_utils.hermes_dialogue_subscribe_session_ended_json.assert_called_once()
class ActionConversion(snips_common.ActionWrapper): reactions = { exceptions.UnknownUnit: "Désolée, je ne sais pas convertir les {}", exceptions.ConversionError: "Désolée, {}", } def action(self): slots = self.intent_message.slots quantity = slots.quantity.first().value source_unit = slots.source_unit.first().value dest_unit = slots.dest_unit.first().value if len(slots.dest_unit) else None print( 'quantity', quantity, 'source_unit', source_unit, 'dest_unit', dest_unit ) message = converter.convert(quantity, source_unit, dest_unit) self.end_session(message) if __name__ == "__main__": mqtt_opts = MqttOptions() with Hermes(mqtt_options=mqtt_opts) as hermes: hermes.subscribe_intent( "borsltd:askUnit", ActionConversion.callback ).start()
def test_initialization_with_options(): mqtt_opts = MqttOptions() h = Hermes(mqtt_options=mqtt_opts) assert h.mqtt_options.broker_address == "localhost:1883"