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
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)
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 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)
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
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
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
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
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)
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)
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
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)
def test_one_intent_two_sentence(self, requests_mock): sentence = Sentence() sentence.add_string("Some test text") sentence2 = Sentence() sentence2.add_string("Some other test text") intent_definition = IntentDefinition("TestIntent") intent_definition.add_sentence(sentence) intent_definition.add_sentence(sentence2) 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]\nSome test text\nSome other test text" requests_mock.assert_called_with(self.SENTENCES_URL, expected_intent_in_call)