Example #1
0
    def test_create_multiple_parameters(self, requests_mock):
        values1 = ["One", "Two", "Three", "Four", "Five", "Six"]
        values2 = ["One", "Two", "Three", "Four", "Five", "Seven"]

        parameter1 = SetParameter("Test1", True, values1)
        parameter2 = SetParameter("Test2", True, values2)
        sentence = Sentence()
        sentence.add_parameter(parameter1)
        sentence.add_parameter(parameter2)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        expected_json = {
            "Test1": ["One", "Two", "Three", "Four", "Five", "Six"],
            "Test2": ["One", "Two", "Three", "Four", "Five", "Seven"]
        }

        updater.update_rhasspy()

        calls = [
            mock.call(self.SLOTS_URL, json.dumps(expected_json)),
            mock.call(self.SENTENCES_URL,
                      "[TestIntent]\n$Test1{Test1} $Test2{Test2}")
        ]
        self.assertEqual(2, requests_mock.call_count)
        requests_mock.assert_has_calls(calls, any_order=True)
Example #2
0
    def test_create_parameter_string_without_return(self, requests_mock):
        values = ["One", "Two", "Three", "Four", "Five", "Six"]
        parameter = SetParameter("Test", possible_values=values)
        sentence = Sentence()
        sentence.add_parameter(parameter)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        expected_json = {
            "Test": ["One", "Two", "Three", "Four", "Five", "Six"]
        }

        updater.update_rhasspy()

        calls = [
            mock.call(self.SLOTS_URL, json.dumps(expected_json)),
            mock.call(self.SENTENCES_URL, "[TestIntent]\n$Test")
        ]
        self.assertEqual(2, requests_mock.call_count)
        requests_mock.assert_has_calls(calls, any_order=True)
Example #3
0
 def _create_intent_definition() -> IntentDefinition:
     start_stop_server_definition = IntentDefinition("StartStopTeamspeakServer")
     sentence = Sentence()
     sentence.add_parameter(SetParameter(TeamspeakIntentHandler.ACTION,
                                              True,
                                              possible_values=["Start", "Stop"]))
     sentence.add_string("teamspeak server")
     start_stop_server_definition.add_sentence(sentence)
     return start_stop_server_definition
Example #4
0
 def _create_one_string_sentence_intent_definition(intent_name: str, sentence_string: str) -> \
         IntentDefinition:
     """Creates the most basic of IntentDefinitions, those consisting of only one single
     string"""
     intent_definition = IntentDefinition(intent_name)
     sentence = Sentence()
     sentence.add_string(sentence_string)
     intent_definition.add_sentence(sentence)
     return intent_definition
Example #5
0
    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 _create_intent_definitions() -> IntentDefinition:
        get_mails = IntentDefinition("GetEmail",
                                     "Do I have [any] (mail|email)")
        email_sender = IntentDefinition(
            "GetEmailSender",
            "(And the next email|What about the next one|Who is the first (mail|email) from)"
        )
        next_email = IntentDefinition("EmailSubject", "What is the subject")
        read_email = IntentDefinition("ReadEmail",
                                      "Can you read (it|the email) to me")

        return [get_mails, email_sender, next_email, read_email]
Example #7
0
 def create_intent_definitions(self) -> List[IntentDefinition]:
     hold_notifications = IntentDefinition("HoldNotifications",
                                           "Block notifications")
     resume_notifications = IntentDefinition("ResumeNotifications",
                                             "Resume notifications")
     get_number_of_notifications = IntentDefinition(
         "GetNumberOfNotifications",
         "How many notifications are in the queue")
     first_notification = IntentDefinition(
         "GetFirstNotification", "Tell me the first notification")
     return [
         hold_notifications, resume_notifications,
         get_number_of_notifications, first_notification
     ]
Example #8
0
    def test_two_slots_same_name_same_values(self, requests_mock):
        slot_values = ["One", "Two", "Three", "Four", "Five", "Six"]
        parameter1 = SetParameter("Test", True, possible_values=slot_values)
        sentence1 = Sentence()
        sentence1.add_parameter(parameter1)
        intent_definition1 = IntentDefinition("TestIntent1")
        intent_definition1.add_sentence(sentence1)

        parameter2 = SetParameter("Test", True, possible_values=slot_values)
        sentence2 = Sentence()
        sentence2.add_parameter(parameter2)
        intent_definition2 = IntentDefinition("TestIntent2")
        intent_definition2.add_sentence(sentence2)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition1, intent_definition2
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        expected_json = {
            "Test": ["One", "Two", "Three", "Four", "Five", "Six"]
        }

        updater.update_rhasspy()

        expected_intents = "[TestIntent1]\n$Test{Test}\n\n[TestIntent2]\n$Test{Test}"
        calls = [
            mock.call(self.SLOTS_URL, json.dumps(expected_json)),
            mock.call(self.SENTENCES_URL, expected_intents)
        ]
        self.assertEqual(2, requests_mock.call_count)
        requests_mock.assert_has_calls(calls, any_order=True)
    def _create_speaker_select_intent_definition():
        intent_definition = IntentDefinition("SelectSpeaker")

        speaker_parameter = SetParameter(
            SpeechManager.SPEAKER, True,
            [SpeechManager.SONOS, SpeechManager.RASPBERRY])

        sentence_builder = SentenceBuilder()
        sentence_builder.add_string("Talk to me on (the)").add_parameter(
            speaker_parameter)

        sentence = sentence_builder.build()

        intent_definition.add_sentence(sentence)
        return intent_definition
Example #10
0
    def test_optional_string(self, requests_mock):
        requests_mock.return_value.status_code = 200

        sentence = Sentence()
        sentence.add_string("Some test text", True)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]

        updater = RhasspyUpdater(intent_definition_source_mock)
        updater.update_rhasspy()

        requests_mock.assert_called_once()
        requests_mock.assert_called_with(self.SENTENCES_URL,
                                         "[TestIntent]\n[Some test text]")
    def _create_get_time_intent_definition(self) -> IntentDefinition:
        set_timer = IntentDefinition("SetTimer")

        numbers_parameter = NumberRangeParameter(self.AMOUNT,
                                                 True,
                                                 lower=0,
                                                 upper=60)
        time_unit_parameter = SetParameter(
            self.TIMEUNIT, True, possible_values=[self.SECONDS, self.MINUTES])

        sentence_builder = SentenceBuilder()
        sentence_builder.add_string("Set a timer for") \
            .add_parameter(numbers_parameter) \
            .add_parameter(time_unit_parameter) \
            .add_string("from now", True)

        sentence = sentence_builder.build()
        set_timer.add_sentence(sentence)
        return set_timer
Example #12
0
    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()
Example #13
0
    def _get_room_on_off_intent_definition(self) -> IntentDefinition:
        room_on_off = IntentDefinition("TurnLightsInRoomOnOff")

        room_names = [room.name for room in self._hue_manager.groups]
        room_parameter = SetParameter(self.ROOM,
                                      True,
                                      possible_values=room_names)

        on_off_parameter = SetParameter(self.ONOFF,
                                        True,
                                        possible_values=[self.ON, self.OFF])

        sentence_builder = SentenceBuilder()
        sentence_builder.add_string("Turn the lights in the")\
            .add_parameter(room_parameter)\
            .add_parameter(on_off_parameter)

        sentence = sentence_builder.build()
        room_on_off.add_sentence(sentence)
        return room_on_off
Example #14
0
    def test_number_range_parameter_without_step(self, requests_mock):
        parameter = NumberRangeParameter("Test", True, -5, 31)
        sentence = Sentence()
        sentence.add_parameter(parameter)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        requests_mock.assert_called_once()
        expected_intent_in_call = "[TestIntent]\n(-5..31){Test!int}"
        requests_mock.assert_has_calls(
            [mock.call(self.SENTENCES_URL, expected_intent_in_call)],
            any_order=True)
Example #15
0
    def test_create_option_string_without_return(self, requests_mock):
        values = ["One", "Two", "Three", "Four", "Five"]
        parameter = SetParameter("Test", possible_values=values)
        sentence = Sentence()
        sentence.add_parameter(parameter)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        requests_mock.assert_called_once()
        expected_intent_in_call = "[TestIntent]\n(One | Two | Three | Four | Five)"
        requests_mock.assert_called_with(self.SENTENCES_URL,
                                         expected_intent_in_call)
Example #16
0
    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()
Example #17
0
    def test_create_option_string_with_return(self, requests_mock):
        parameter = SetParameter("Test", True,
                                 ["One", "Two", "Three", "Four", "Five"])
        sentence = Sentence()
        sentence.add_parameter(parameter)
        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        requests_mock.assert_called_once()
        expected_intent_in_call = "[TestIntent]\n(One | Two | Three | Four | Five){Test}"
        requests_mock.assert_has_calls(
            [mock.call(self.SENTENCES_URL, expected_intent_in_call)],
            any_order=True)
Example #18
0
    def test_empty_intent_definition(self, requests_mock):
        intent_definition = IntentDefinition("")

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]

        updater = RhasspyUpdater(intent_definition_source_mock)
        updater.update_rhasspy()

        self.assertEqual(0, requests_mock.call_count)
Example #19
0
    def get_dim_room_intent_definition(self):
        dim_room = IntentDefinition("DimRoom")

        room_names = [room.name for room in self._hue_manager.groups]
        room_parameter = SetParameter(self.ROOM,
                                      True,
                                      possible_values=room_names)
        up_down_parameter = SetParameter(self.UP_DOWN,
                                         True,
                                         possible_values=[self.UP, self.DOWN])
        in_decrease_parameter = SetParameter(
            self.IN_DECREASE,
            True,
            possible_values=[self.INCREASE, self.DECREASE])

        sentence_builder = SentenceBuilder()
        sentence_builder.add_string("Dim the lights in the")\
            .add_parameter(room_parameter)\
            .add_parameter(up_down_parameter)
        sentence = sentence_builder.build()

        sentence_builder2 = SentenceBuilder()
        sentence_builder2.add_parameter(in_decrease_parameter)\
            .add_string("the brightness in the")\
            .add_parameter(room_parameter)

        sentence2 = sentence_builder2.build()
        dim_room.add_sentence(sentence)
        dim_room.add_sentence(sentence2)

        return dim_room
Example #20
0
    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")
Example #21
0
    def test_variable(self, requests_mock):
        named_days = ["today", "tomorrow", "the day after tomorrow"]
        weekday_names = ["Monday", "Tuesday", "Wednesday"]
        month_names = ["January", "February", "March", "April", "May", "June"]

        forecast = IntentDefinition("GetWeatherForecast")
        named_days_sentence = SentenceBuilder().add_string("on", True, True) \
            .add_parameter(SetParameter("day", possible_values=named_days + weekday_names))\
            .build()
        date_sentence = SentenceBuilder().add_parameter(NumberRangeParameter("date", lower=0, upper=31)) \
            .add_parameter(SetParameter("month", possible_values=month_names))\
            .build()
        in_days_sentence = SentenceBuilder().add_string("in") \
            .add_parameter(NumberRangeParameter("days", lower=0, upper=7)) \
            .add_string("days")\
            .build()

        day_variable = Variable(
            "day", [named_days_sentence, date_sentence, in_days_sentence])

        forecast.add_variable(day_variable)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            forecast
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        self.assertEqual(2, requests_mock.call_count)
        expected_intent_in_call = "[GetWeatherForecast]\nday = ([on:] $day|(0..31) $month|in (0..7) days)\n\n"
        expected_slots_in_call = """{"day": ["today", "tomorrow", "the day after tomorrow", "Monday", "Tuesday", "Wednesday"], "month": ["January", "February", "March", "April", "May", "June"]}"""
        requests_mock.assert_has_calls([
            mock.call(self.SLOTS_URL, expected_slots_in_call),
            mock.call(self.SENTENCES_URL, expected_intent_in_call)
        ],
                                       any_order=True)
    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)
Example #23
0
    def test_create_string_only_simple_single_sentence(self, requests_mock):
        intent_definition = IntentDefinition("TestIntent",
                                             "Some simple test text")

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition
        ]

        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        requests_mock.assert_called_once()
        requests_mock.assert_called_with(
            self.SENTENCES_URL, "[TestIntent]\nSome simple test text")
Example #24
0
    def test_two_intents_one_sentence(self, requests_mock):
        sentence = Sentence()
        sentence.add_string("Some test text")

        intent_definition = IntentDefinition("TestIntent")
        intent_definition.add_sentence(sentence)

        sentence2 = Sentence()
        sentence2.add_string("Some other test text")
        intent_definition2 = IntentDefinition("TestIntent2")
        intent_definition2.add_sentence(sentence2)

        intent_definition_source_mock = Mock()
        intent_definition_source_mock.get_intent_definitions.return_value = [
            intent_definition, intent_definition2
        ]
        updater = RhasspyUpdater(intent_definition_source_mock)

        updater.update_rhasspy()

        requests_mock.assert_called_once()
        expected_intents = "[TestIntent]\nSome test text\n\n[TestIntent2]\nSome other test text"
        requests_mock.assert_called_with(self.SENTENCES_URL, expected_intents)
Example #25
0
 def __init__(self):
     super().__init__()
     self._intent_definitions = [IntentDefinition("")]