Added support (not working yet) for custom Google device actions and events
This commit is contained in:
parent
008fce70fa
commit
ca030b06e0
2 changed files with 37 additions and 10 deletions
|
@ -0,0 +1,22 @@
|
|||
from platypush.message.event import Event
|
||||
|
||||
class GoogleDeviceEvent(Event):
|
||||
"""
|
||||
Base class for Google device events, see `managing traits and handlers <https://developers.google.com/assistant/sdk/guides/service/python/extend/add-trait-and-handler>`_.
|
||||
"""
|
||||
|
||||
def __init__(self, device_id, device_model_id=None, *args, **kwargs):
|
||||
super().__init__(*args, device_id=device_id, device_model_id=device_model_id, **kwargs)
|
||||
|
||||
|
||||
class GoogleDeviceOnOffEvent(GoogleDeviceEvent):
|
||||
"""
|
||||
Event triggered when a device receives an on/off command
|
||||
"""
|
||||
|
||||
def __init__(self, on, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.on = on
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
|
@ -13,6 +13,8 @@ from platypush.message.event.assistant import ConversationStartEvent, \
|
|||
ConversationEndEvent, SpeechRecognizedEvent, VolumeChangedEvent, \
|
||||
ResponseEvent
|
||||
|
||||
from platypush.message.event.google import GoogleDeviceOnOffEvent
|
||||
|
||||
from platypush.plugins import action
|
||||
from platypush.plugins.assistant import AssistantPlugin
|
||||
from platypush.plugins.assistant.google.lib import SampleAssistant
|
||||
|
@ -44,6 +46,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
|||
audio_block_size = audio_helpers.DEFAULT_AUDIO_DEVICE_BLOCK_SIZE
|
||||
audio_flush_size = audio_helpers.DEFAULT_AUDIO_DEVICE_FLUSH_SIZE
|
||||
grpc_deadline = 60 * 3 + 5
|
||||
device_handler = None
|
||||
|
||||
def __init__(self,
|
||||
credentials_file=os.path.join(
|
||||
|
@ -100,7 +103,6 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
|||
|
||||
self.grpc_channel = None
|
||||
self.conversation_stream = None
|
||||
self.device_handler = None
|
||||
|
||||
def _init_assistant(self):
|
||||
from google.auth.transport.grpc import secure_authorized_channel
|
||||
|
@ -138,7 +140,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
|||
sample_width=self.audio_sample_width,
|
||||
)
|
||||
|
||||
self.device_handler = device_helpers.DeviceRequestHandler(self.device_id)
|
||||
self._install_device_handlers()
|
||||
|
||||
@staticmethod
|
||||
def on_conversation_start():
|
||||
|
@ -184,13 +186,6 @@ 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, *args, language=None, **kwargs):
|
||||
"""
|
||||
|
@ -230,7 +225,7 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
|||
display=None,
|
||||
channel=self.grpc_channel,
|
||||
deadline_sec=self.grpc_deadline,
|
||||
device_handler=self.on_device_action(),
|
||||
device_handler=self.device_handler,
|
||||
on_conversation_start=self.on_conversation_start(),
|
||||
on_conversation_end=self.on_conversation_end(),
|
||||
on_volume_changed=self.on_volume_changed(),
|
||||
|
@ -260,5 +255,15 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
|
|||
|
||||
get_bus().post(ConversationEndEvent())
|
||||
|
||||
def _install_device_handlers(self):
|
||||
self.device_handler = device_helpers.DeviceRequestHandler(self.device_id)
|
||||
|
||||
@self.device_handler.command('action.devices.commands.OnOff')
|
||||
def handler(on):
|
||||
get_bus().post(GoogleDeviceOnOffEvent(
|
||||
device_id=self.device_id,
|
||||
device_model_id=self.device_model_id,
|
||||
on=on))
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
||||
|
|
Loading…
Reference in a new issue