Beispiel #1
0
 def on_connect(self, client, userdata, flags, rc):
     """Connected to MQTT broker."""
     try:
         topics = [TtsSay.topic()]
         for topic in topics:
             self.client.subscribe(topic)
             _LOGGER.debug("Subscribed to %s", topic)
     except Exception:
         _LOGGER.exception("on_connect")
Beispiel #2
0
    def test_http_mqtt_text_to_speech(self):
        """Test text-to-speech HTTP endpoint"""
        text = "This is a test."
        self.client.subscribe(TtsSay.topic())
        self.client.subscribe(AudioPlayBytes.topic(site_id=self.site_id))
        self.client.subscribe(TtsSayFinished.topic())

        response = requests.post(
            self.api_url("text-to-speech"),
            data=text,
            params={
                "siteId": self.site_id,
                "sessionId": self.session_id
            },
        )
        self.check_status(response)

        wav_data = response.content
        self.assertGreater(len(wav_data), 0)

        # Check tts/say
        tts_say_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(TtsSay.is_topic(tts_say_msg.topic))

        tts_say = TtsSay.from_dict(json.loads(tts_say_msg.payload))
        self.assertEqual(tts_say.site_id, self.site_id)
        self.assertEqual(tts_say.session_id, self.session_id)
        self.assertEqual(tts_say.text, text)

        # Check audioServer/playBytes
        play_bytes_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(AudioPlayBytes.is_topic(play_bytes_msg.topic))
        self.assertEqual(AudioPlayBytes.get_site_id(play_bytes_msg.topic),
                         self.site_id)
        self.assertEqual(play_bytes_msg.payload, wav_data)

        # Check tts/sayFinished
        tts_finished_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(TtsSayFinished.is_topic(tts_finished_msg.topic))

        tts_finished = TtsSayFinished.from_dict(
            json.loads(tts_finished_msg.payload))
        self.assertEqual(tts_finished.session_id, self.session_id)

        # Ask for repeat
        response = requests.post(self.api_url("text-to-speech"),
                                 params={"repeat": "true"})
        self.check_status(response)
        self.assertEqual(wav_data, response.content)
Beispiel #3
0
    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 == TtsSay.topic():
                json_payload = json.loads(msg.payload or "{}")

                if not self._check_siteId(json_payload):
                    return

                say = TtsSay(**json_payload)
                self.handle_say(say)
        except Exception:
            _LOGGER.exception("on_message")
Beispiel #4
0
    def test_no_play(self):
        """Test text-to-speech HTTP endpoint with play=false"""
        text = "This is a test."
        self.client.subscribe(TtsSay.topic())
        self.client.subscribe(AudioPlayBytes.topic(site_id=self.site_id))
        self.client.subscribe(TtsSayFinished.topic())
        self.client.subscribe(AudioToggleOff.topic())
        self.client.subscribe(AudioToggleOn.topic())

        response = requests.post(
            self.api_url("text-to-speech"),
            data=text,
            params={
                "siteId": self.site_id,
                "sessionId": self.session_id,
                "play": "false",
            },
        )
        self.check_status(response)

        wav_data = response.content
        self.assertGreater(len(wav_data), 0)

        # Check audioServer/toggleOff
        audio_off_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(AudioToggleOff.is_topic(audio_off_msg.topic))

        audio_off = AudioToggleOff.from_dict(json.loads(audio_off_msg.payload))
        self.assertEqual(audio_off.site_id, self.site_id)

        # Check tts/say
        tts_say_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(TtsSay.is_topic(tts_say_msg.topic))

        tts_say = TtsSay.from_dict(json.loads(tts_say_msg.payload))
        self.assertEqual(tts_say.site_id, self.site_id)
        self.assertEqual(tts_say.session_id, self.session_id)
        self.assertEqual(tts_say.text, text)

        # Check audioServer/playBytes (will be ignored by audio output system)
        play_bytes_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(AudioPlayBytes.is_topic(play_bytes_msg.topic))
        self.assertEqual(AudioPlayBytes.get_site_id(play_bytes_msg.topic),
                         self.site_id)
        self.assertEqual(play_bytes_msg.payload, wav_data)

        # Check tts/sayFinished
        tts_finished_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(TtsSayFinished.is_topic(tts_finished_msg.topic))

        tts_finished = TtsSayFinished.from_dict(
            json.loads(tts_finished_msg.payload))
        self.assertEqual(tts_finished.site_id, self.site_id)
        self.assertEqual(tts_finished.session_id, self.session_id)

        # Check audioServer/toggleOn
        audio_on_msg = self.mqtt_messages.get(timeout=5)
        self.assertTrue(AudioToggleOn.is_topic(audio_on_msg.topic))

        audio_on = AudioToggleOn.from_dict(json.loads(audio_on_msg.payload))
        self.assertEqual(audio_on.site_id, self.site_id)
Beispiel #5
0
def test_tts_say():
    """Test TtsSay."""
    assert TtsSay.topic() == "hermes/tts/say"