Implement MQTT for Advanced LVA Controls
Adds a handler for the VOICE_ASSISTANT_ERROR event, publishing a voice_error event to the EventBus so the LedController can provide visual feedback. Fixes a race condition where the "Responding" LED effect was being cancelled prematurely, ensuring it now plays for the full duration of the TTS audio.
This commit is contained in:
@@ -65,6 +65,7 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
self._tts_played = False
|
self._tts_played = False
|
||||||
self._continue_conversation = False
|
self._continue_conversation = False
|
||||||
self._timer_finished = False
|
self._timer_finished = False
|
||||||
|
self._is_speaking = False # ADDED: Flag to track TTS state
|
||||||
|
|
||||||
def handle_voice_event(
|
def handle_voice_event(
|
||||||
self, event_type: VoiceAssistantEventType, data: Dict[str, str]
|
self, event_type: VoiceAssistantEventType, data: Dict[str, str]
|
||||||
@@ -87,23 +88,33 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
self._is_streaming_audio = False
|
self._is_streaming_audio = False
|
||||||
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_INTENT_PROGRESS:
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_INTENT_PROGRESS:
|
||||||
if data.get("tts_start_streaming") == "1":
|
if data.get("tts_start_streaming") == "1":
|
||||||
# Start streaming early
|
|
||||||
self.play_tts()
|
self.play_tts()
|
||||||
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_INTENT_END:
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_INTENT_END:
|
||||||
if data.get("continue_conversation") == "1":
|
if data.get("continue_conversation") == "1":
|
||||||
self._continue_conversation = True
|
self._continue_conversation = True
|
||||||
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_TTS_START:
|
||||||
|
self.state.event_bus.publish("voice_tts_start", data)
|
||||||
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_TTS_END:
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_TTS_END:
|
||||||
self._tts_url = data.get("url")
|
self._tts_url = data.get("url")
|
||||||
self.play_tts()
|
self.play_tts()
|
||||||
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_RUN_END:
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_RUN_END:
|
||||||
|
# --- THIS IS THE FIX ---
|
||||||
|
# Ignore this event if we are currently playing TTS audio.
|
||||||
|
# The real end event will be triggered by the _tts_finished callback.
|
||||||
|
if self._is_speaking:
|
||||||
|
return
|
||||||
|
|
||||||
self._is_streaming_audio = False
|
self._is_streaming_audio = False
|
||||||
if not self._tts_played:
|
if not self._tts_played:
|
||||||
self._tts_finished()
|
self._tts_finished()
|
||||||
|
|
||||||
self._tts_played = False
|
self._tts_played = False
|
||||||
self.state.event_bus.publish("voice_run_end")
|
self.state.event_bus.publish("voice_run_end")
|
||||||
|
|
||||||
|
elif event_type == VoiceAssistantEventType.VOICE_ASSISTANT_ERROR:
|
||||||
|
self.state.event_bus.publish("voice_error", data)
|
||||||
|
self.state.event_bus.publish("voice_run_end")
|
||||||
|
|
||||||
# TODO: handle error
|
|
||||||
|
|
||||||
def handle_timer_event(
|
def handle_timer_event(
|
||||||
self,
|
self,
|
||||||
@@ -120,26 +131,19 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
|
|
||||||
def handle_message(self, msg: message.Message) -> Iterable[message.Message]:
|
def handle_message(self, msg: message.Message) -> Iterable[message.Message]:
|
||||||
if isinstance(msg, VoiceAssistantEventResponse):
|
if isinstance(msg, VoiceAssistantEventResponse):
|
||||||
# Pipeline event
|
|
||||||
data: Dict[str, str] = {}
|
data: Dict[str, str] = {}
|
||||||
for arg in msg.data:
|
for arg in msg.data:
|
||||||
data[arg.name] = arg.value
|
data[arg.name] = arg.value
|
||||||
|
|
||||||
self.handle_voice_event(VoiceAssistantEventType(msg.event_type), data)
|
self.handle_voice_event(VoiceAssistantEventType(msg.event_type), data)
|
||||||
elif isinstance(msg, VoiceAssistantAnnounceRequest):
|
elif isinstance(msg, VoiceAssistantAnnounceRequest):
|
||||||
_LOGGER.debug("Announcing: %s", msg.text)
|
_LOGGER.debug("Announcing: %s", msg.text)
|
||||||
|
|
||||||
assert self.state.media_player_entity is not None
|
assert self.state.media_player_entity is not None
|
||||||
|
|
||||||
urls = []
|
urls = []
|
||||||
if msg.preannounce_media_id:
|
if msg.preannounce_media_id:
|
||||||
urls.append(msg.preannounce_media_id)
|
urls.append(msg.preannounce_media_id)
|
||||||
|
|
||||||
urls.append(msg.media_id)
|
urls.append(msg.media_id)
|
||||||
|
|
||||||
self.state.stop_word.is_active = True
|
self.state.stop_word.is_active = True
|
||||||
self._continue_conversation = msg.start_conversation
|
self._continue_conversation = msg.start_conversation
|
||||||
|
|
||||||
self.duck()
|
self.duck()
|
||||||
yield from self.state.media_player_entity.play(
|
yield from self.state.media_player_entity.play(
|
||||||
urls, announcement=True, done_callback=self._tts_finished
|
urls, announcement=True, done_callback=self._tts_finished
|
||||||
@@ -188,37 +192,26 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
max_active_wake_words=2,
|
max_active_wake_words=2,
|
||||||
)
|
)
|
||||||
_LOGGER.info("Connected to Home Assistant")
|
_LOGGER.info("Connected to Home Assistant")
|
||||||
# --- THIS IS THE CHANGE ---
|
|
||||||
# Announce the connection so other components (like LEDs) can react.
|
|
||||||
self.state.event_bus.publish("ha_connected")
|
self.state.event_bus.publish("ha_connected")
|
||||||
elif isinstance(msg, VoiceAssistantSetConfiguration):
|
elif isinstance(msg, VoiceAssistantSetConfiguration):
|
||||||
# Change active wake words
|
|
||||||
active_wake_words: Set[str] = set()
|
active_wake_words: Set[str] = set()
|
||||||
|
|
||||||
for wake_word_id in msg.active_wake_words:
|
for wake_word_id in msg.active_wake_words:
|
||||||
if wake_word_id in self.state.wake_words:
|
if wake_word_id in self.state.wake_words:
|
||||||
# Already active
|
|
||||||
active_wake_words.add(wake_word_id)
|
active_wake_words.add(wake_word_id)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
model_info = self.state.available_wake_words.get(wake_word_id)
|
model_info = self.state.available_wake_words.get(wake_word_id)
|
||||||
if not model_info:
|
if not model_info:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
_LOGGER.debug("Loading wake word: %s", model_info.config_path)
|
_LOGGER.debug("Loading wake word: %s", model_info.config_path)
|
||||||
self.state.wake_words[wake_word_id] = model_info.load(
|
self.state.wake_words[wake_word_id] = model_info.load(
|
||||||
self.state.libtensorflowlite_c_path
|
self.state.libtensorflowlite_c_path
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER.info("Wake word set: %s", wake_word_id)
|
_LOGGER.info("Wake word set: %s", wake_word_id)
|
||||||
active_wake_words.add(wake_word_id)
|
active_wake_words.add(wake_word_id)
|
||||||
break
|
break
|
||||||
|
|
||||||
for wake_word in self.state.wake_words.values():
|
for wake_word in self.state.wake_words.values():
|
||||||
wake_word.is_active = wake_word.id in active_wake_words
|
wake_word.is_active = wake_word.id in active_wake_words
|
||||||
|
|
||||||
_LOGGER.debug("Active wake words: %s", active_wake_words)
|
_LOGGER.debug("Active wake words: %s", active_wake_words)
|
||||||
|
|
||||||
self.state.preferences.active_wake_words = list(active_wake_words)
|
self.state.preferences.active_wake_words = list(active_wake_words)
|
||||||
self.state.save_preferences()
|
self.state.save_preferences()
|
||||||
self.state.wake_words_changed = True
|
self.state.wake_words_changed = True
|
||||||
@@ -226,19 +219,15 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
def handle_audio(self, audio_chunk: bytes) -> None:
|
def handle_audio(self, audio_chunk: bytes) -> None:
|
||||||
if not self._is_streaming_audio:
|
if not self._is_streaming_audio:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.send_messages([VoiceAssistantAudio(data=audio_chunk)])
|
self.send_messages([VoiceAssistantAudio(data=audio_chunk)])
|
||||||
|
|
||||||
def wakeup(self, wake_word: Union[MicroWakeWord, OpenWakeWord]) -> None:
|
def wakeup(self, wake_word: Union[MicroWakeWord, OpenWakeWord]) -> None:
|
||||||
self.state.event_bus.publish("voice_wakeword")
|
self.state.event_bus.publish("voice_wakeword")
|
||||||
|
|
||||||
if self._timer_finished:
|
if self._timer_finished:
|
||||||
# Stop timer instead
|
|
||||||
self._timer_finished = False
|
self._timer_finished = False
|
||||||
self.state.tts_player.stop()
|
self.state.tts_player.stop()
|
||||||
_LOGGER.debug("Stopping timer finished sound")
|
_LOGGER.debug("Stopping timer finished sound")
|
||||||
return
|
return
|
||||||
|
|
||||||
wake_word_phrase = wake_word.wake_word
|
wake_word_phrase = wake_word.wake_word
|
||||||
_LOGGER.debug("Detected wake word: %s", wake_word_phrase)
|
_LOGGER.debug("Detected wake word: %s", wake_word_phrase)
|
||||||
self.send_messages(
|
self.send_messages(
|
||||||
@@ -251,7 +240,6 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
self.state.stop_word.is_active = False
|
self.state.stop_word.is_active = False
|
||||||
self.state.tts_player.stop()
|
self.state.tts_player.stop()
|
||||||
|
|
||||||
if self._timer_finished:
|
if self._timer_finished:
|
||||||
self._timer_finished = False
|
self._timer_finished = False
|
||||||
_LOGGER.debug("Stopping timer finished sound")
|
_LOGGER.debug("Stopping timer finished sound")
|
||||||
@@ -262,12 +250,10 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
def play_tts(self) -> None:
|
def play_tts(self) -> None:
|
||||||
if (not self._tts_url) or self._tts_played:
|
if (not self._tts_url) or self._tts_played:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.state.event_bus.publish("voice_tts_start", {"url": self._tts_url})
|
self._is_speaking = True # ADDED: Set flag before playing
|
||||||
|
|
||||||
self._tts_played = True
|
self._tts_played = True
|
||||||
_LOGGER.debug("Playing TTS response: %s", self._tts_url)
|
_LOGGER.debug("Playing TTS response: %s", self._tts_url)
|
||||||
|
|
||||||
self.state.stop_word.is_active = True
|
self.state.stop_word.is_active = True
|
||||||
self.state.tts_player.play(self._tts_url, done_callback=self._tts_finished)
|
self.state.tts_player.play(self._tts_url, done_callback=self._tts_finished)
|
||||||
|
|
||||||
@@ -280,24 +266,22 @@ class VoiceSatelliteProtocol(APIServer):
|
|||||||
self.state.music_player.unduck()
|
self.state.music_player.unduck()
|
||||||
|
|
||||||
def _tts_finished(self) -> None:
|
def _tts_finished(self) -> None:
|
||||||
|
self._is_speaking = False # ADDED: Clear flag when done
|
||||||
self.state.event_bus.publish("voice_run_end")
|
self.state.event_bus.publish("voice_run_end")
|
||||||
self.state.stop_word.is_active = False
|
self.state.stop_word.is_active = False
|
||||||
self.send_messages([VoiceAssistantAnnounceFinished()])
|
self.send_messages([VoiceAssistantAnnounceFinished()])
|
||||||
|
|
||||||
if self._continue_conversation:
|
if self._continue_conversation:
|
||||||
self.send_messages([VoiceAssistantRequest(start=True)])
|
self.send_messages([VoiceAssistantRequest(start=True)])
|
||||||
self._is_streaming_audio = True
|
self._is_streaming_audio = True
|
||||||
_LOGGER.debug("Continuing conversation")
|
_LOGGER.debug("Continuing conversation")
|
||||||
else:
|
else:
|
||||||
self.unduck()
|
self.unduck()
|
||||||
|
|
||||||
_LOGGER.debug("TTS response finished")
|
_LOGGER.debug("TTS response finished")
|
||||||
|
|
||||||
def _play_timer_finished(self) -> None:
|
def _play_timer_finished(self) -> None:
|
||||||
if not self._timer_finished:
|
if not self._timer_finished:
|
||||||
self.unduck()
|
self.unduck()
|
||||||
return
|
return
|
||||||
|
|
||||||
self.state.tts_player.play(
|
self.state.tts_player.play(
|
||||||
self.state.timer_finished_sound,
|
self.state.timer_finished_sound,
|
||||||
done_callback=lambda: call_all(
|
done_callback=lambda: call_all(
|
||||||
|
|||||||
Reference in New Issue
Block a user