From a14d6fe65204d71bed4b39e635299305a7094f12 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 21 Mar 2018 23:21:41 +0100 Subject: [PATCH] - Stop assistant playback only if there was a user request. This prevents a PortAudio write on input-only stream error in the Assistant SDK, see https://github.com/googlesamples/assistant-sdk-python/issues/185 - Make sure that user_request is always defined before returning it - Use assistant.google.pushtotalk as a fallback if assistant.google is not configured/available --- .../backend/assistant/google/pushtotalk.py | 7 ++++++- platypush/message/event/assistant/__init__.py | 7 +++++-- platypush/plugins/assistant/google/__init__.py | 18 ++++++++++++++++++ .../plugins/assistant/google/pushtotalk.py | 18 ++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 platypush/plugins/assistant/google/__init__.py create mode 100644 platypush/plugins/assistant/google/pushtotalk.py diff --git a/platypush/backend/assistant/google/pushtotalk.py b/platypush/backend/assistant/google/pushtotalk.py index d81318bb9b..c10f0a12ce 100644 --- a/platypush/backend/assistant/google/pushtotalk.py +++ b/platypush/backend/assistant/google/pushtotalk.py @@ -159,6 +159,7 @@ class AssistantGooglePushtotalkBackend(Backend): logging.info('Assistant conversation triggered') continue_conversation = True + user_request = None while continue_conversation: (user_request, continue_conversation) = self.assistant.assist() @@ -243,6 +244,8 @@ class SampleAssistant(object): yield c self.conversation_stream.start_playback() + user_request = None + # This generator yields AssistResponse proto messages # received from the gRPC Google Assistant API. for resp in self.assistant.Assist(iter_assist_requests(), @@ -285,7 +288,9 @@ class SampleAssistant(object): concurrent.futures.wait(device_actions_futures) logging.info('Finished playing assistant response.') - self.conversation_stream.stop_playback() + + if user_request: + self.conversation_stream.stop_playback() return (user_request, continue_conversation) def gen_assist_requests(self): diff --git a/platypush/message/event/assistant/__init__.py b/platypush/message/event/assistant/__init__.py index 8c09eb3e4b..483270c751 100644 --- a/platypush/message/event/assistant/__init__.py +++ b/platypush/message/event/assistant/__init__.py @@ -12,8 +12,11 @@ class AssistantEvent(Event): try: self._assistant = get_backend('assistant.google') except KeyError as e: - logging.warning('google.assistant backend not configured/initialized') - self._assistant = None + try: + self._assistant = get_backend('assistant.google.pushtotalk') + except KeyError as e: + logging.warning('google.assistant backend not configured/initialized') + self._assistant = None class ConversationStartEvent(AssistantEvent): diff --git a/platypush/plugins/assistant/google/__init__.py b/platypush/plugins/assistant/google/__init__.py new file mode 100644 index 0000000000..a3589284de --- /dev/null +++ b/platypush/plugins/assistant/google/__init__.py @@ -0,0 +1,18 @@ +from platypush.context import get_backend +from platypush.message.response import Response + +from platypush.plugins import Plugin + +class AssistantGooglePlugin(Plugin): + def start_conversation(self): + assistant = get_backend('assistant.google') + assistant.start_conversation() + return Response(output='', errors=[]) + + def stop_conversation(self): + assistant = get_backend('assistant.google') + assistant.stop_conversation() + return Response(output='', errors=[]) + +# vim:sw=4:ts=4:et: + diff --git a/platypush/plugins/assistant/google/pushtotalk.py b/platypush/plugins/assistant/google/pushtotalk.py new file mode 100644 index 0000000000..41bedccc78 --- /dev/null +++ b/platypush/plugins/assistant/google/pushtotalk.py @@ -0,0 +1,18 @@ +from platypush.context import get_backend +from platypush.message.response import Response + +from platypush.plugins import Plugin + +class AssistantGooglePushtotalkPlugin(Plugin): + def start_conversation(self): + assistant = get_backend('assistant.google.pushtotalk') + assistant.start_conversation() + return Response(output='', errors=[]) + + def stop_conversation(self): + assistant = get_backend('assistant.google.pushtotalk') + assistant.stop_conversation() + return Response(output='', errors=[]) + +# vim:sw=4:ts=4:et: +