From f38121d1760181f39afe50700c6032d24ddaaee6 Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <blacklight86@gmail.com>
Date: Sun, 19 Jan 2020 16:32:41 +0100
Subject: [PATCH] Always pause/resume speech detection on backend level

---
 platypush/plugins/assistant/__init__.py       | 25 ++++++++++++-------
 platypush/plugins/assistant/echo/__init__.py  | 14 -----------
 .../plugins/assistant/google/__init__.py      | 18 +------------
 .../plugins/assistant/google/pushtotalk.py    | 18 -------------
 4 files changed, 17 insertions(+), 58 deletions(-)

diff --git a/platypush/plugins/assistant/__init__.py b/platypush/plugins/assistant/__init__.py
index 8ea4b363..8c60ff1b 100644
--- a/platypush/plugins/assistant/__init__.py
+++ b/platypush/plugins/assistant/__init__.py
@@ -1,6 +1,7 @@
 from abc import ABC, abstractmethod
 
-from platypush.plugins import Plugin
+from platypush.context import get_backend
+from platypush.plugins import Plugin, action
 
 
 class AssistantPlugin(ABC, Plugin):
@@ -22,26 +23,32 @@ class AssistantPlugin(ABC, Plugin):
         """
         raise NotImplementedError
 
-    @abstractmethod
-    def pause_detection(self, *args, **kwargs):
+    def _get_assistant(self):
+        return get_backend('assistant.snowboy')
+
+    @action
+    def pause_detection(self):
         """
         Put the assistant on pause. No new conversation events will be triggered.
         """
-        raise NotImplementedError
+        assistant = self._get_assistant()
+        assistant.pause_detection()
 
-    @abstractmethod
-    def resume_detection(self, *args, **kwargs):
+    @action
+    def resume_detection(self):
         """
         Resume the assistant hotword detection from a paused state.
         """
-        raise NotImplementedError
+        assistant = self._get_assistant()
+        assistant.resume_detection()
 
-    @abstractmethod
+    @action
     def is_detecting(self) -> bool:
         """
         :return: True if the asistant is detecting, False otherwise.
         """
-        raise NotImplementedError
+        assistant = self._get_assistant()
+        return assistant.is_detecting()
 
 
 # vim:sw=4:ts=4:et:
diff --git a/platypush/plugins/assistant/echo/__init__.py b/platypush/plugins/assistant/echo/__init__.py
index 8716ed6e..83ffeea3 100644
--- a/platypush/plugins/assistant/echo/__init__.py
+++ b/platypush/plugins/assistant/echo/__init__.py
@@ -3,7 +3,6 @@
 """
 
 import os
-import threading
 
 from platypush.context import get_bus
 from platypush.plugins import action
@@ -71,7 +70,6 @@ class AssistantEchoPlugin(AssistantPlugin):
         self.audio = Audio(device_name=audio_device)
         self.alexa = Alexa(avs_config_file, audio_player=audio_player)
         self._ready = False
-        self._detection_paused = threading.Event()
 
         self.alexa.state_listener.on_ready = self._on_ready()
         self.alexa.state_listener.on_listening = self._on_listening()
@@ -128,17 +126,5 @@ class AssistantEchoPlugin(AssistantPlugin):
         self.audio.stop()
         self._on_finished()()
 
-    @action
-    def pause_detection(self):
-        self._detection_paused.set()
-
-    @action
-    def resume_detection(self):
-        self._detection_paused.clear()
-
-    @action
-    def is_detecting(self) -> bool:
-        return not self._detection_paused.is_set()
-
 
 # vim:sw=4:ts=4:et:
diff --git a/platypush/plugins/assistant/google/__init__.py b/platypush/plugins/assistant/google/__init__.py
index a01459b9..941052f4 100644
--- a/platypush/plugins/assistant/google/__init__.py
+++ b/platypush/plugins/assistant/google/__init__.py
@@ -17,8 +17,7 @@ class AssistantGooglePlugin(AssistantPlugin):
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
 
-    @staticmethod
-    def _get_assistant():
+    def _get_assistant(self):
         return get_backend('assistant.google')
 
     @action
@@ -37,20 +36,5 @@ class AssistantGooglePlugin(AssistantPlugin):
         assistant = self._get_assistant()
         assistant.stop_conversation()
 
-    @action
-    def pause_detection(self):
-        assistant = self._get_assistant()
-        assistant.pause_detection()
-
-    @action
-    def resume_detection(self):
-        assistant = self._get_assistant()
-        assistant.resume_detection()
-
-    @action
-    def is_detecting(self) -> bool:
-        assistant = self._get_assistant()
-        return assistant.is_detecting()
-
 
 # vim:sw=4:ts=4:et:
diff --git a/platypush/plugins/assistant/google/pushtotalk.py b/platypush/plugins/assistant/google/pushtotalk.py
index d046f625..3b46ce19 100644
--- a/platypush/plugins/assistant/google/pushtotalk.py
+++ b/platypush/plugins/assistant/google/pushtotalk.py
@@ -4,7 +4,6 @@
 
 import json
 import os
-import threading
 
 from platypush.context import get_bus
 from platypush.message.event.assistant import ConversationStartEvent, \
@@ -86,7 +85,6 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
         self.play_response = play_response
         self.assistant = None
         self.interactions = []
-        self._detection_paused = threading.Event()
 
         with open(self.device_config) as f:
             device = json.load(f)
@@ -221,10 +219,6 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
 
         from platypush.plugins.assistant.google.lib import SampleAssistant
 
-        if not self.is_detecting():
-            self.logger.info('Conversation start event received but detection is currently paused')
-            return
-
         if not language:
             language = self.language
 
@@ -279,17 +273,5 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
                 device_model_id=self.device_model_id,
                 on=on))
 
-    @action
-    def pause_detection(self):
-        self._detection_paused.set()
-
-    @action
-    def resume_detection(self):
-        self._detection_paused.clear()
-
-    @action
-    def is_detecting(self) -> bool:
-        return not self._detection_paused.is_set()
-
 
 # vim:sw=4:ts=4:et: