def create_intent_handler_manager(speaker: Speaker): """- Creates an IntentHandlerManager object - Creates an instance of each class implementing the IntentHandler Abstract Base class - Subscribes every IntentHandler instance to the IntentHandlerManager """ intent_handler_manager = IntentHandlerManager(speaker) intent_handlers: List[IntentHandler] = [ LightsInRoomOnOffHandler(), RoomDimmer(), TeamspeakIntentHandler(), TimeIntentHandler(), WeatherIntentHandler(), MailHandler() ] async_voice_sources: List[AsyncVoiceSource] = [] timer_intent_handler = TimerIntenthandler() intent_handlers.append(timer_intent_handler) async_voice_sources.append(timer_intent_handler) for intent_handler in intent_handlers: intent_handler_manager.subscribe_intent_handler(intent_handler) trigger_manager = TriggerManager(speaker) for async_voice_source in async_voice_sources: async_voice_source.subscribe(trigger_manager) return intent_handler_manager
def test_get_all_intent_definitions(self): mock_intent_handler_1 = Mock() mock_intent_handler_1.intent_definitions = [IntentDefinition("test1", "Test text")] mock_intent_handler_2 = Mock() mock_intent_handler_2.intent_definitions = [IntentDefinition("test2", "Test text")] intent_manager = IntentHandlerManager() intent_manager.subscribe_intent_handler(mock_intent_handler_1) intent_manager.subscribe_intent_handler(mock_intent_handler_2) self.assertEqual(2, len(intent_manager.get_intent_definitions()))
def test_handle_intent(self): localtime = time.localtime() time_string = time.strftime("%H:%M", localtime) expected_response = "It is currently {} o'clock".format( time_string), False intent = Intent("GetTime", {}) intent_manager = IntentHandlerManager() time_intent_handler = TimeIntentHandler() intent_manager.subscribe_intent_handler(time_intent_handler) response = time_intent_handler.handle_intent(intent) self.assertEqual(expected_response, response)
def test_new_intent_without_response(self): mock_intent_handler = Mock() mock_intent_handler.intent_definitions = [IntentDefinition("Test", "Test text")] mock_intent_handler.handle_intent = MagicMock(return_value=(None, False)) intent_handler_manager = IntentHandlerManager() intent_receiver = RhasspyIntentReceiver() intent_receiver.attach(intent_handler_manager) intent_handler_manager.subscribe_intent_handler(mock_intent_handler) response = intent_receiver.handle_new_intent(self._intent_string) expected_response = '{"intent": "Test", "time_sec": 0.0, "response": ""}' self.assertEqual(expected_response, response)
def test_dispatch_intent(self): mock_intent_handler = Mock() mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")] mock_intent_handler.handle_intent = MagicMock() intent_manager = IntentHandlerManager() intent_manager.subscribe_intent_handler(mock_intent_handler) intent_manager.update(Intent('test', {})) mock_intent_handler.handle_intent.assert_called_once()
def test_speaker_is_called_when_present(self): mock_intent_handler = Mock() mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")] mock_intent_handler.handle_intent = Mock(return_value="Speak this text") speaker = Mock() intent_manager = IntentHandlerManager(speaker) intent_manager.subscribe_intent_handler(mock_intent_handler) intent_manager.update(Intent("test", {})) speaker.speak_text.assert_called_once()
def test_handle_exception_in_intent_handler(self): mock_intent_handler = Mock() mock_intent_handler.intent_definitions = [IntentDefinition("test", "Test text")] mock_intent_handler.handle_intent = Mock(side_effect=KeyError("Shit happened")) intent_manager = IntentHandlerManager() intent_manager.subscribe_intent_handler(mock_intent_handler) try: intent_manager.update(Intent("test", {})) except KeyError: self.fail("IntentHandlerManager.update() unexpectedly raised a KeyError")
def test_dispatch_unknown_intent(self): intent_manager = IntentHandlerManager() try: intent_manager.update(Intent("unknown", {})) except KeyError: self.fail("dispatch_intent raised KeyError unexpectedly")