forked from platypush/platypush
[#400] Dynamic logic to infer procedures/hooks arguments.
This allows procedures and event hooks to have more flexible signatures. Along the lines of: ```python @when(SomeEvent) def hook(event): ... @when(SomeOtherEvent) def hook2(): ... ``` Instead of supporting only the full context spec: ```python @when(SomeEvent) def hook(event, **ctx): ... ``` Closes: #400
This commit is contained in:
parent
2ab1743bec
commit
32b8296244
4 changed files with 52 additions and 19 deletions
examples/config
|
@ -1,9 +1,13 @@
|
|||
# A more versatile way to define event hooks than the YAML format of `config.yaml` is through native Python scripts.
|
||||
# You can define hooks as simple Python functions that use the `platypush.event.hook.hook` decorator to specify on
|
||||
# which event type they should be called, and optionally on which event attribute values.
|
||||
# A more versatile way to define event hooks than the YAML format of
|
||||
# `config.yaml` is through native Python scripts. You can define hooks as simple
|
||||
# Python functions that use the `platypush.event.hook.hook` decorator to specify
|
||||
# on which event type they should be called, and optionally on which event
|
||||
# attribute values.
|
||||
#
|
||||
# Event hooks should be stored in Python files under `~/.config/platypush/scripts`. All the functions that use the
|
||||
# @when decorator will automatically be discovered and imported as event hooks into the platform at runtime.
|
||||
# Event hooks should be stored in Python files under
|
||||
# `~/.config/platypush/scripts`. All the functions that use the @when decorator
|
||||
# will automatically be discovered and imported as event hooks into the platform
|
||||
# at runtime.
|
||||
|
||||
# `run` is a utility function that runs a request by name (e.g. `light.hue.on`).
|
||||
from platypush import when, run
|
||||
|
@ -16,12 +20,14 @@ from platypush.message.event.assistant import (
|
|||
|
||||
|
||||
@when(SpeechRecognizedEvent, phrase='play ${title} by ${artist}')
|
||||
def on_music_play_command(event, title=None, artist=None, **context):
|
||||
def on_music_play_command(event, title=None, artist=None):
|
||||
"""
|
||||
This function will be executed when a SpeechRecognizedEvent with `phrase="play the music"` is triggered.
|
||||
`event` contains the event object and `context` any key-value info from the running context.
|
||||
Note that in this specific case we can leverage the token-extraction feature of SpeechRecognizedEvent through
|
||||
${} that operates on regex-like principles to extract any text that matches the pattern into context variables.
|
||||
This function will be executed when a SpeechRecognizedEvent with
|
||||
`phrase="play the music"` is triggered. `event` contains the event object
|
||||
and `context` any key-value info from the running context. Note that in this
|
||||
specific case we can leverage the token-extraction feature of
|
||||
SpeechRecognizedEvent through ${} that operates on regex-like principles to
|
||||
extract any text that matches the pattern into context variables.
|
||||
"""
|
||||
results = run(
|
||||
'music.mpd.search',
|
||||
|
@ -31,16 +37,17 @@ def on_music_play_command(event, title=None, artist=None, **context):
|
|||
},
|
||||
)
|
||||
|
||||
if results:
|
||||
if results and results[0]:
|
||||
run('music.mpd.play', results[0]['file'])
|
||||
else:
|
||||
run('tts.say', "I can't find any music matching your query")
|
||||
|
||||
|
||||
@when(ConversationStartEvent)
|
||||
def on_conversation_start(event, **context):
|
||||
def on_conversation_start():
|
||||
"""
|
||||
A simple hook that gets invoked when a new conversation starts with a voice assistant and simply pauses the music
|
||||
to make sure that your speech is properly detected.
|
||||
A simple hook that gets invoked when a new conversation starts with a voice
|
||||
assistant and simply pauses the music to make sure that your speech is
|
||||
properly detected.
|
||||
"""
|
||||
run('music.mpd.pause_if_playing')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue