Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit 20a378c

Browse files
proppyGerrit Code Review
authored andcommitted
Merge "Updates SDK sample and updates lib changelog" into eap-device-actions
2 parents 9e2108d + 57c8f5b commit 20a378c

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

google-assistant-library/CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Changelog
22
=========
3+
0.1.0
4+
-----
5+
- Support for local device actions
6+
37
0.0.3
48
-----
59
- Support for x86_64 Linux

google-assistant-sdk/googlesamples/assistant/library/hotword.py

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
32
# Copyright (C) 2017 Google Inc.
43
#
54
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,38 +12,77 @@
1312
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1413
# See the License for the specific language governing permissions and
1514
# limitations under the License.
16-
17-
1815
from __future__ import print_function
19-
2016
import argparse
2117
import os.path
2218
import json
23-
19+
import google.auth.transport.requests
2420
import google.oauth2.credentials
25-
2621
from google.assistant.library import Assistant
2722
from google.assistant.library.event import EventType
2823
from google.assistant.library.file_helpers import existing_file
24+
DEVICE_API_URL = 'https://embeddedassistant.googleapis.com/v1alpha2'
2925

3026

31-
def process_event(event):
32-
"""Pretty prints events.
27+
def process_device_actions(event, device_id):
28+
if 'inputs' in event.args:
29+
for i in event.args['inputs']:
30+
if i['intent'] == 'action.devices.EXECUTE':
31+
for c in i['payload']['commands']:
32+
for device in c['devices']:
33+
if device['id'] == device_id:
34+
if 'execution' in c:
35+
for e in c['execution']:
36+
if e['params']:
37+
yield e['command'], e['params']
38+
else:
39+
yield e['command'], None
3340

41+
42+
def process_event(event, device_id):
43+
"""Pretty prints events.
3444
Prints all events that occur with two spaces between each new
3545
conversation and a single space between turns of a conversation.
36-
3746
Args:
3847
event(event.Event): The current event to process.
3948
"""
4049
if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
4150
print()
42-
4351
print(event)
44-
4552
if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
4653
event.args and not event.args['with_follow_on_turn']):
4754
print()
55+
if event.type == EventType.ON_DEVICE_ACTION:
56+
for command, params in process_device_actions(event, device_id):
57+
print('Do command', command, 'with params', str(params))
58+
59+
60+
def register_device(project_id, credentials, device_model_id, device_id):
61+
"""Register the device if needed.
62+
Registers a new assistant device if an instance with the given id
63+
does not already exists for this model.
64+
Args:
65+
project_id(str): The project ID used to register device instance.
66+
credentials(google.oauth2.credentials.Credentials): The Google
67+
OAuth2 credentials of the user to associate the device
68+
instance with.
69+
device_model_id: The registered device model ID.
70+
device_id: The device ID of the new instance.
71+
"""
72+
base_url = '/'.join([DEVICE_API_URL, 'projects', project_id, 'devices'])
73+
device_url = '/'.join([base_url, device_id])
74+
session = google.auth.transport.requests.AuthorizedSession(credentials)
75+
r = session.get(device_url)
76+
print(device_url, r.status_code)
77+
if r.status_code == 404:
78+
print('Registering....', end='', flush=True)
79+
r = session.post(base_url, data=json.dumps({
80+
'id': device_id,
81+
'model_id': device_model_id,
82+
}))
83+
if r.status_code != 200:
84+
raise Exception('failed to register device: ' + r.text)
85+
print('\rDevice registered.')
4886

4987

5088
def main():
@@ -58,14 +96,26 @@ def main():
5896
'credentials.json'
5997
),
6098
help='Path to store and read OAuth2 credentials')
99+
parser.add_argument('--device_model_id', type=str,
100+
metavar='DEVICE_MODEL_ID', required=True,
101+
help='The device model ID registered with Google.')
102+
parser.add_argument('--project_id', type=str,
103+
metavar='PROJECT_ID', required=False,
104+
help='The project ID used to register device '
105+
+ 'instances.')
61106
args = parser.parse_args()
62107
with open(args.credentials, 'r') as f:
63108
credentials = google.oauth2.credentials.Credentials(token=None,
64109
**json.load(f))
65-
66-
with Assistant(credentials) as assistant:
67-
for event in assistant.start():
68-
process_event(event)
110+
with Assistant(credentials, args.device_model_id) as assistant:
111+
events = assistant.start()
112+
print('device_model_id:', args.device_model_id + '\n' +
113+
'device_id:', assistant.device_id + '\n')
114+
if args.project_id:
115+
register_device(args.project_id, credentials,
116+
args.device_model_id, assistant.device_id)
117+
for event in events:
118+
process_event(event, assistant.device_id)
69119

70120

71121
if __name__ == '__main__':

0 commit comments

Comments
 (0)