def test_topics(self): """Check get_ methods for topics""" siteId = "testSiteId" requestId = "testRequestId" intentName = "testIntent" wakewordId = "testWakeWord" # AudioFrame self.assertTrue(AudioFrame.is_topic(AudioFrame.topic(siteId=siteId))) self.assertEqual( AudioFrame.get_siteId(AudioFrame.topic(siteId=siteId)), siteId) # AudioPlayBytes self.assertTrue( AudioPlayBytes.is_topic( AudioPlayBytes.topic(siteId=siteId, requestId=requestId))) self.assertEqual( AudioPlayBytes.get_siteId( AudioPlayBytes.topic(siteId=siteId, requestId=requestId)), siteId, ) self.assertEqual( AudioPlayBytes.get_requestId( AudioPlayBytes.topic(siteId=siteId, requestId=requestId)), requestId, ) # AudioPlayFinished self.assertTrue( AudioPlayFinished.is_topic(AudioPlayFinished.topic(siteId=siteId))) self.assertEqual( AudioPlayFinished.get_siteId( AudioPlayFinished.topic(siteId=siteId)), siteId) # NluIntent self.assertTrue( NluIntent.is_topic(NluIntent.topic(intentName=intentName))) self.assertEqual( NluIntent.get_intentName(NluIntent.topic(intentName=intentName)), intentName) # HotwordDetected self.assertTrue( HotwordDetected.is_topic( HotwordDetected.topic(wakewordId=wakewordId))) self.assertEqual( HotwordDetected.get_wakewordId( HotwordDetected.topic(wakewordId=wakewordId)), wakewordId, )
async def recognize_intent(self, text: str) -> typing.Dict[str, typing.Any]: """Send an NLU query and wait for intent or not recognized""" nlu_id = str(uuid4()) query = NluQuery(id=nlu_id, input=text, siteId=self.siteId) def handle_intent(): while True: _, message = yield if isinstance( message, (NluIntent, NluIntentNotRecognized)) and (message.id == nlu_id): if isinstance(message, NluIntent): # Finish parsing message.intent = rhasspyhermes.intent.Intent( **message.intent) message.slots = [ rhasspyhermes.intent.Slot(**s) for s in message.slots ] return True, message messages = [query] topics = [ NluIntent.topic(intentName="#"), NluIntentNotRecognized.topic() ] # Expecting only a single result async for result in self.publish_wait(handle_intent(), messages, topics): return result
async def async_test_ws_intent(self): """Test api/events/intent endpoint""" # Start listening event_queue = asyncio.Queue() connected = asyncio.Event() receive_task = asyncio.ensure_future( self.async_ws_receive("events/intent", event_queue, connected)) await asyncio.wait_for(connected.wait(), timeout=5) # Send in a message nlu_intent = NluIntent( input="turn on the living room lamp", id=str(uuid4()), intent=Intent(intent_name="ChangeLightState", confidence_score=1), slots=[ Slot( entity="state", slot_name="state", value={"value": "on"}, confidence=1.0, raw_value="on", ), Slot( entity="name", slot_name="name", value={"value": "living room lamp"}, confidence=1.0, raw_value="living room lamp", ), ], site_id=self.site_id, session_id=self.session_id, ) self.client.publish( nlu_intent.topic(intent_name=nlu_intent.intent.intent_name), nlu_intent.payload(), ) # Wait for response event = json.loads(await asyncio.wait_for(event_queue.get(), timeout=5)) # Expected Rhasspy JSON format as a response _LOGGER.debug(nlu_intent) expected = nlu_intent.to_rhasspy_dict() # Extra info expected["siteId"] = self.site_id expected["sessionId"] = self.session_id expected["customData"] = None expected["wakewordId"] = None expected["lang"] = None self.assertEqual(event, expected) # Stop listening receive_task.cancel()
def on_connect(self, client, userdata, flags, rc): """Connected to MQTT broker.""" try: topics = [ DialogueStartSession.topic(), DialogueContinueSession.topic(), DialogueEndSession.topic(), TtsSayFinished.topic(), NluIntent.topic(intent_name="#"), NluIntentNotRecognized.topic(), AsrTextCaptured.topic(), ] + list(self.wakeword_topics.keys()) for topic in topics: self.client.subscribe(topic) _LOGGER.debug("Subscribed to %s", topic) except Exception: _LOGGER.exception("on_connect")
def _subscribe_callbacks(self): # Remove duplicate intent names intent_names = list(set(self._callbacks_intent.keys())) topics = [ NluIntent.topic(intent_name=intent_name) for intent_name in intent_names ] if self._callbacks_hotword: topics.append(HotwordDetected.topic()) if self._callbacks_intent_not_recognized: topics.append(NluIntentNotRecognized.topic()) topic_names = list(set(self._callbacks_topic.keys())) topics.extend(topic_names) topics.extend(self._additional_topic) self.subscribe_topics(*topics)
def __init__(self, mqtt_client, site_ids: typing.Optional[typing.List[str]] = None): super().__init__("TimeApp", mqtt_client, site_ids=site_ids) self.subscribe_topics(NluIntent.topic(intent_name="GetTime"))
def on_message(self, client, userdata, msg): """Received message from MQTT broker.""" try: _LOGGER.debug("Received %s byte(s) on %s", len(msg.payload), msg.topic) if msg.topic == DialogueStartSession.topic(): # Start session json_payload = json.loads(msg.payload) if not self._check_siteId(json_payload): return # Run in event loop (for TTS) asyncio.run_coroutine_threadsafe( self.handle_start(DialogueStartSession(**json_payload)), self.loop) elif msg.topic == DialogueContinueSession.topic(): # Continue session json_payload = json.loads(msg.payload) if not self._check_siteId(json_payload): return # Run in event loop (for TTS) asyncio.run_coroutine_threadsafe( self.handle_continue( DialogueContinueSession(**json_payload)), self.loop, ) elif msg.topic == DialogueEndSession.topic(): # End session json_payload = json.loads(msg.payload) if not self._check_siteId(json_payload): return # Run outside event loop self.handle_end(DialogueEndSession(**json_payload)) elif msg.topic == TtsSayFinished.topic(): # TTS finished json_payload = json.loads(msg.payload) if not self._check_sessionId(json_payload): return # Signal event loop self.loop.call_soon_threadsafe(self.say_finished_event.set) elif msg.topic == AsrTextCaptured.topic(): # Text captured json_payload = json.loads(msg.payload) if not self._check_sessionId(json_payload): return # Run outside event loop self.handle_text_captured(AsrTextCaptured(**json_payload)) elif msg.topic.startswith(NluIntent.topic(intent_name="")): # Intent recognized json_payload = json.loads(msg.payload) if not self._check_sessionId(json_payload): return self.handle_recognized(NluIntent(**json_payload)) elif msg.topic.startswith(NluIntentNotRecognized.topic()): # Intent recognized json_payload = json.loads(msg.payload) if not self._check_sessionId(json_payload): return # Run in event loop (for TTS) asyncio.run_coroutine_threadsafe( self.handle_not_recognized( NluIntentNotRecognized(**json_payload)), self.loop, ) elif msg.topic in self.wakeword_topics: json_payload = json.loads(msg.payload) if not self._check_siteId(json_payload): return wakeword_id = self.wakeword_topics[msg.topic] asyncio.run_coroutine_threadsafe( self.handle_wake(wakeword_id, HotwordDetected(**json_payload)), self.loop, ) except Exception: _LOGGER.exception("on_message")
def test_nlu_intent(): """Test NluIntent.""" assert NluIntent.is_topic(NluIntent.topic(intent_name=intent_name)) assert (NluIntent.get_intent_name( NluIntent.topic(intent_name=intent_name)) == intent_name)