def test_hotword_detected(): """Test HotwordDetected.""" assert HotwordDetected.is_topic(HotwordDetected.topic(wakeword_id=wakeword_id)) assert ( HotwordDetected.get_wakeword_id(HotwordDetected.topic(wakeword_id=wakeword_id)) == wakeword_id )
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, )
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, client, siteIds: typing.Optional[typing.List[str]] = None, wakewordIds: typing.Optional[typing.List[str]] = None, loop=None, ): self.client = client self.siteIds = siteIds or [] self.loop = loop or asyncio.get_event_loop() self.session: typing.Optional[SessionInfo] = None self.session_queue = deque() self.wakeword_topics = { HotwordDetected.topic(wakewordId=w): w for w in wakewordIds or [] } # Set when TtsSayFinished comes back self.say_finished_event = asyncio.Event() self.say_finished_timeout: float = 10
async def async_test_ws_wake(self): """Test api/events/wake endpoint""" # Start listening event_queue = asyncio.Queue() connected = asyncio.Event() receive_task = asyncio.ensure_future( self.async_ws_receive("events/wake", event_queue, connected) ) await asyncio.wait_for(connected.wait(), timeout=5) # Send in a message detected = HotwordDetected(model_id=str(uuid4()), site_id=self.site_id) wakeword_id = str(uuid4()) self.client.publish(detected.topic(wakeword_id=wakeword_id), detected.payload()) # Wait for response event = json.loads(await asyncio.wait_for(event_queue.get(), timeout=5)) self.assertEqual(wakeword_id, event.get("wakewordId", "")) self.assertEqual(detected.site_id, event.get("siteId", "")) # Stop listening receive_task.cancel()