google.assistant.library package.


class google.assistant.library.Assistant(credentials)[source]

Client for the Google Assistant Library.

Provides basic control functionality and lifecycle handling for the Google Assistant. It is best practice to use the Assistant as a ContextManager:

with Assistant(credentials) as assistant:

This allows the underlying native implementation to properly handle memory management. Once started, the Assistant generates a stream of Events relaying the various states the Assistant is currently in, for example:

ON_CONVERSATION_TURN_STARTED
ON_END_OF_UTTERANCE
ON_RECOGNIZING_SPEECH_FINISHED
{'text': 'what time is it'}
ON_RESPONDING_STARTED:
{'is_error_response': False}
ON_RESPONDING_FINISHED
ON_CONVERSATION_TURN_FINISHED
{'with_follow_on_turn': False}

See google.assistant.event.EventType for details on all events and their arguments.

Glossary:

Hotword: The phrase the Assistant listens for when not muted:

Ok Google OR Hey Google

Turn: A single user request followed by a response from the Assistant.

Conversation: One or more turns which result in a desired final result from the Assistant:

What time is it? -> The time is 6:24 PM

OR

Set a timer -> Okay, for how long? ->
5 minutes -> Sure, 5 minutes, starting now!

set_mic_mute(is_muted)[source]

Stops the Assistant from listening for the hotword.

Allows for disabling the Assistant from listening for the hotword. This provides functionality similar to the privacy button on the back of Google Home.

This method is a no-op if the Assistant has not yet been started.

Args:

is_muted(bool): True stops the Assistant from listening and False allows it to start again.

start()[source]

Starts the Assistant, which includes listening for a hotword.

Once start() is called, the Assistant will begin processing data from the ‘default’ ALSA audio source, listening for the hotword. This will also start other services provided by the Assistant, such as timers/alarms. This method can only be called once. Once called, the Assistant will continue to run until __exit__ is called.

Returns:

google.assistant.event.IterableEventQueue: A queue of events that notify of changes to the Assistant state.

start_conversation()[source]

Manually starts a new conversation with the Assistant.

Starts both recording the user’s speech and sending it to Google, similar to what happens when the Assistant hears the hotword.

This method is a no-op if the Assistant is not started or has been muted.

stop_conversation()[source]

Stops any active conversation with the Assistant.

The Assistant could be listening to the user’s query OR responding. If there is no active conversation, this is a no-op.

version

Returns the version of the Assistant.

Returns:

dict: Contains the version of the Assistant being used.


class google.assistant.library.event.Event(event_type, args)[source]

An event generated by the Assistant.

Attributes:

type(EventType): The type of event that was generated.
args(dict): Argument key/value pairs associated with this event.


class google.assistant.library.event.EventType[source]
ON_START_FINISHED = 0

The Assistant library has finished starting.

ON_CONVERSATION_TURN_STARTED = 1

Indicates a new turn has started.

The Assistant is currently listening, waiting for a user query. This could be the result of hearing the hotword or start_conversation() being called on the Assistant.

ON_CONVERSATION_TURN_TIMEOUT = 2

The Assistant timed out waiting for a discernable query.

This could be caused by a mistrigger of the Hotword or the Assistant could not understand what the user said.

ON_END_OF_UTTERANCE = 3

The Assistant has stopped listening to a user query.

The Assistant may not have finished figuring out what the user has said but it has stopped listening for more audio data.

ON_RECOGNIZING_SPEECH_FINISHED = 5

The Assistant has determined the final recognized speech.

Args:
text(str): The final text interpretation of a user’s query.
ON_RESPONDING_STARTED = 6

The Assistant is starting to respond by voice.

The Assistant will be responding until ON_RESPONDING_FINISHED is received.

Args:

is_error_response(bool): True means a local error TTS is being played, otherwise the Assistant responds with a server response.

ON_RESPONDING_FINISHED = 7

The Assistant has finished responding by voice.

ON_NO_RESPONSE = 8

The Assistant successfully completed its turn but has nothing to say.

ON_CONVERSATION_TURN_FINISHED = 9

The Assistant finished the current turn.

This includes both processing a user’s query and speaking the full response.

Args:

with_follow_on_turn(bool): If True, the Assistant is expecting a follow up interaction from the user. The microphone will be re-opened to allow the user to answer a follow-up question.

ON_ALERT_STARTED = 10

Indicates that an alert has started sounding.

This alert will continue until ON_ALERT_FINISHED with the same alert_type is received. Only one alert should be active at any given time.

Args:

alert_type(int): The id of the Enum representing the currently sounding type of alert.

ON_ALERT_FINISHED = 11

Indicates the alert of alert_type has finished sounding.

Args:

alert_type(int): The id of the Enum representing the type of alert which just finished.

ON_ASSISTANT_ERROR = 12

Indicates if the Assistant library has encountered an error.

Args:

is_fatal(bool): If True then the Assistant will be unable to recover and should be restarted.

ON_MUTED_CHANGED = 13

Indicates that the Assistant is currently listening or not.

start() will always generate an ON_MUTED_CHANGED to report the initial value.

Args:

is_muted(bool): If True then the Assistant is not currently listening for its hotword and will not respond to user queries.


class google.assistant.library.event.AlertType[source]
ALARM = 0

An event set for an absolute time such as ‘3 A.M on Monday’

TIMER = 1

An event set for a relative time such as ‘30 seconds from now.’


class google.assistant.library.event.IterableEventQueue(timeout=3600)[source]

Extends queue.Queue to add an __iter__ interface.

offer(event)[source]

Offer an event to put in the queue.

If the queue is currently full the event will be logged but not added.

Args:
event(Event): The event to try to add to the queue.