diff --git a/platypush/message/event/assistant/__init__.py b/platypush/message/event/assistant/__init__.py index 531192977f..7facd40ec4 100644 --- a/platypush/message/event/assistant/__init__.py +++ b/platypush/message/event/assistant/__init__.py @@ -10,18 +10,21 @@ logger = logging.getLogger(__name__) class AssistantEvent(Event): """ Base class for assistant events """ - def __init__(self, *args, **kwargs): + def __init__(self, assistant=None, *args, **kwargs): super().__init__(*args, **kwargs) - try: + + if assistant: + self._assistant = assistant + else: self._assistant = get_backend('assistant.google') - except KeyError: - try: + + if not self._assistant: self._assistant = get_plugin('assistant.google.pushtotalk') - except Exception: + + if not self._assistant: logger.warning('google.assistant not configured/initialized') self._assistant = None - class ConversationStartEvent(AssistantEvent): """ Event triggered when a new conversation starts @@ -82,15 +85,18 @@ class SpeechRecognizedEvent(AssistantEvent): Event triggered when a speech is recognized """ - def __init__(self, phrase, *args, **kwargs): + def __init__(self, phrase, *args, assistant=None, **kwargs): """ :param phrase: Recognized user phrase :type phrase: str """ - super().__init__(phrase=phrase, *args, **kwargs) + super().__init__(*args, phrase=phrase, assistant=assistant, **kwargs) self.recognized_phrase = phrase.strip().lower() + if not self._assistant and assistant: + self._assistant = assistant + def matches_condition(self, condition): """ Overrides matches condition, and stops the conversation to prevent the @@ -98,8 +104,12 @@ class SpeechRecognizedEvent(AssistantEvent): """ result = super().matches_condition(condition) + if result.is_match and self._assistant and 'phrase' in condition.args: - self._assistant.stop_conversation() + if hasattr(self._assistant, 'play_response'): + self._assistant.play_response = False + elif hasattr(self._assistant, 'stop_conversation'): + self._assistant.stop_conversation() return result @@ -182,4 +192,3 @@ class TimerEndEvent(AlertEndEvent): # vim:sw=4:ts=4:et: - diff --git a/platypush/plugins/assistant/google/lib/__init__.py b/platypush/plugins/assistant/google/lib/__init__.py index 4962467cff..0222594451 100644 --- a/platypush/plugins/assistant/google/lib/__init__.py +++ b/platypush/plugins/assistant/google/lib/__init__.py @@ -59,6 +59,7 @@ class SampleAssistant(object): self.device_id = device_id self.conversation_stream = conversation_stream self.display = display + self.play_response = True # Opaque blob provided in AssistResponse that, # when provided in a follow-up AssistRequest, @@ -131,6 +132,7 @@ class SampleAssistant(object): logging.info('End of audio request detected.') logging.info('Stopping recording.') self.conversation_stream.stop_recording() + self.play_response = True if self.detected_speech and self.on_speech_recognized: self.on_speech_recognized(self.detected_speech) @@ -144,9 +146,13 @@ class SampleAssistant(object): if len(resp.audio_out.audio_data) > 0: if not self.conversation_stream.playing: self.conversation_stream.stop_recording() - self.conversation_stream.start_playback() - logging.info('Playing assistant response.') - self.conversation_stream.write(resp.audio_out.audio_data) + + if self.play_response: + self.conversation_stream.start_playback() + logging.info('Playing assistant response.') + + if self.play_response: + self.conversation_stream.write(resp.audio_out.audio_data) if resp.dialog_state_out.conversation_state: conversation_state = resp.dialog_state_out.conversation_state logging.debug('Updating conversation state.') diff --git a/platypush/plugins/assistant/google/pushtotalk.py b/platypush/plugins/assistant/google/pushtotalk.py index 3f97a998cf..702b2574be 100644 --- a/platypush/plugins/assistant/google/pushtotalk.py +++ b/platypush/plugins/assistant/google/pushtotalk.py @@ -146,21 +146,21 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): def on_conversation_start(self): """ Conversation start handler """ def handler(): - get_bus().post(ConversationStartEvent()) + get_bus().post(ConversationStartEvent(assistant=self.assistant)) return handler def on_conversation_end(self): """ Conversation end handler """ def handler(with_follow_on_turn): - get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn)) + get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn, assistant=self.assistant)) return handler def on_speech_recognized(self): """ Speech recognized handler """ def handler(phrase): - get_bus().post(SpeechRecognizedEvent(phrase=phrase)) + get_bus().post(SpeechRecognizedEvent(phrase=phrase, assistant=self.assistant)) self.interactions.append({'request': phrase}) return handler @@ -168,14 +168,14 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): def on_volume_changed(self): """ Volume changed event """ def handler(volume): - get_bus().post(VolumeChangedEvent(volume=volume)) + get_bus().post(VolumeChangedEvent(volume=volume, assistant=self.assistant)) return handler def on_response(self): """ Response handler """ def handler(response): - get_bus().post(ResponseEvent(response_text=response)) + get_bus().post(ResponseEvent(response_text=response, assistant=self.assistant)) if not self.interactions: self.interactions.append({'response': response}) @@ -184,6 +184,13 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): return handler + def on_device_action(self): + """ Device action handler """ + def handler(request): + self.logger.info('Received request: {}'.format(request)) + + return handler + @action def start_conversation(self, language=None): """ @@ -219,7 +226,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): display=None, channel=self.grpc_channel, deadline_sec=self.grpc_deadline, - device_handler=None, + device_handler=self.on_device_action(), on_conversation_start=self.on_conversation_start(), on_conversation_end=self.on_conversation_end(), on_volume_changed=self.on_volume_changed(), @@ -237,7 +244,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin): """ Stop a conversation """ if self.assistant: self.conversation_stream.stop_playback() - get_bus().post(ConversationEndEvent()) + get_bus().post(ConversationEndEvent(assistant=self.assistant)) # vim:sw=4:ts=4:et: