Skip to content

Commit

Permalink
Allows wait argument to be int value for seconds (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcobb authored Nov 2, 2023
1 parent bcdff2a commit 6a2a2ae
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,16 +1437,17 @@ def _register_adapt_intent(self,

# skill developer facing utils
def speak(self, utterance: str, expect_response: bool = False,
wait: bool = False, meta: Optional[dict] = None):
wait: Union[bool, int] = False, meta: Optional[dict] = None):
"""Speak a sentence.
Args:
utterance (str): sentence mycroft should speak
expect_response (bool): set to True if Mycroft should listen
for a response immediately after
speaking the utterance.
wait (bool): set to True to block while the text
is being spoken.
wait (Union[bool, int]): set to True to block while the text
is being spoken for 15 seconds. Alternatively, set
to an integer to specify a timeout in seconds.
meta: Information of what built the sentence.
"""
# registers the skill as being active
Expand All @@ -1473,6 +1474,7 @@ def speak(self, utterance: str, expect_response: bool = False,
self.bus.emit(m)

if wait:
timeout = wait if isinstance(wait, int) else 15
sessid = SessionManager.get(m).session_id
event = Event()

Expand All @@ -1482,12 +1484,12 @@ def handle_output_end(msg):
event.set()

self.bus.on("recognizer_loop:audio_output_end", handle_output_end)
event.wait(timeout=15)
event.wait(timeout=timeout)
self.bus.remove("recognizer_loop:audio_output_end",
handle_output_end)

def speak_dialog(self, key: str, data: Optional[dict] = None,
expect_response: bool = False, wait: bool = False):
expect_response: bool = False, wait: Union[bool, int] = False):
"""
Speak a random sentence from a dialog file.
Expand All @@ -1498,8 +1500,9 @@ def speak_dialog(self, key: str, data: Optional[dict] = None,
expect_response (bool): set to True if Mycroft should listen
for a response immediately after
speaking the utterance.
wait (bool): set to True to block while the text
is being spoken.
wait (Union[bool, int]): set to True to block while the text
is being spoken for 15 seconds. Alternatively, set
to an integer to specify a timeout in seconds.
"""
if self.dialog_renderer:
data = data or {}
Expand Down Expand Up @@ -1670,7 +1673,8 @@ def __get_response(self, session: Session):
def get_response(self, dialog: str = '', data: Optional[dict] = None,
validator: Optional[Callable[[str], bool]] = None,
on_fail: Optional[Union[str, Callable[[str], str]]] = None,
num_retries: int = -1, message: Message = None) -> Optional[str]:
num_retries: int = -1, message: Message = None,
wait: Union[bool, int] = True) -> Optional[str]:
"""
Get a response from the user. If a dialog is supplied it is spoken,
followed immediately by listening for a user response. If the dialog is
Expand All @@ -1687,6 +1691,10 @@ def get_response(self, dialog: str = '', data: Optional[dict] = None,
* If the user asks to "cancel", this method will exit
* If the user doesn't respond and this is `-1` this will only retry
once.
@param message: Optional message to use for context
@param wait: If True, wait for the response to finish speaking before
listening. If False, start listening immediately. Can be an int
to set the timeout in seconds.
@return: String user response (None if no valid response is given)
"""
message = message or dig_for_message() or \
Expand Down Expand Up @@ -1722,7 +1730,7 @@ def validator_default(utterance):

# Speak query and wait for user response
if dialog:
self.speak_dialog(dialog, data, expect_response=True, wait=True)
self.speak_dialog(dialog, data, expect_response=True, wait=wait)
else:
self.bus.emit(message.forward('mycroft.mic.listen'))

Expand Down

0 comments on commit 6a2a2ae

Please sign in to comment.