Esempio n. 1
0
        def handle_finished():
            say_finished: typing.Optional[TtsSayFinished] = None
            play_bytes: typing.Optional[
                AudioPlayBytes
            ] = None if capture_audio else True
            play_finished = not wait_play_finished

            while True:
                topic, message = yield

                if isinstance(message, TtsSayFinished) and (message.id == tts_id):
                    say_finished = message
                    play_finished = True
                elif isinstance(message, TtsError):
                    # Assume audio playback didn't happen
                    say_finished = message
                    play_bytes = True
                    play_finished = True
                elif isinstance(message, AudioPlayBytes):
                    request_id = AudioPlayBytes.get_request_id(topic)
                    if request_id == tts_id:
                        play_bytes = message
                elif isinstance(message, AudioPlayError):
                    play_bytes = message

                if say_finished and play_bytes and play_finished:
                    return (say_finished, play_bytes)
Esempio n. 2
0
 async def on_message(
     self,
     message: Message,
     site_id: typing.Optional[str] = None,
     session_id: typing.Optional[str] = None,
     topic: typing.Optional[str] = None,
 ) -> GeneratorType:
     """Received message from MQTT broker."""
     if isinstance(message, AudioPlayBytes):
         assert site_id and topic, "Missing site_id or topic"
         request_id = AudioPlayBytes.get_request_id(topic)
         session_id = session_id or ""
         async for play_result in self.handle_play(request_id,
                                                   message.wav_bytes,
                                                   site_id=site_id,
                                                   session_id=session_id):
             yield play_result
     elif isinstance(message, AudioGetDevices):
         async for device_result in self.handle_get_devices(message):
             yield device_result
     elif isinstance(message, AudioToggleOff):
         self.enabled = False
         _LOGGER.debug("Disabled audio")
     elif isinstance(message, AudioToggleOn):
         self.enabled = True
         _LOGGER.debug("Enabled audio")
     else:
         _LOGGER.warning("Unexpected message: %s", message)
Esempio n. 3
0
def test_audio_play_bytes():
    """Test AudioPlayBytes."""
    assert AudioPlayBytes.is_topic(
        AudioPlayBytes.topic(site_id=site_id, request_id=request_id))
    assert (AudioPlayBytes.get_site_id(
        AudioPlayBytes.topic(site_id=site_id,
                             request_id=request_id)) == site_id)
    assert (AudioPlayBytes.get_request_id(
        AudioPlayBytes.topic(site_id=site_id,
                             request_id=request_id)) == request_id)