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

Commit 1ef2e7b

Browse files
committed
google-assistant-sdk/pushtotalk: fix device action handler init
- move the device handler initialization so that it could get the device-id form the device_config file. - add end to end test for OnOff command. Bug: 74241166 Change-Id: I3307f2170713ffd61a4fbd1f657e3cd856bfaa69
1 parent 96a583e commit 1ef2e7b

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,6 @@ def main(api_endpoint, credentials, project_id,
356356
sample_width=audio_sample_width,
357357
)
358358

359-
device_handler = device_helpers.DeviceRequestHandler(device_id)
360-
361-
@device_handler.command('action.devices.commands.OnOff')
362-
def onoff(on):
363-
if on:
364-
logging.info('Turning device on')
365-
else:
366-
logging.info('Turning device off')
367-
368359
if not device_id or not device_model_id:
369360
try:
370361
with open(device_config) as f:
@@ -404,6 +395,15 @@ def onoff(on):
404395
with open(device_config, 'w') as f:
405396
json.dump(payload, f)
406397

398+
device_handler = device_helpers.DeviceRequestHandler(device_id)
399+
400+
@device_handler.command('action.devices.commands.OnOff')
401+
def onoff(on):
402+
if on:
403+
logging.info('Turning device on')
404+
else:
405+
logging.info('Turning device off')
406+
407407
with SampleAssistant(lang, device_model_id, device_id,
408408
conversation_stream,
409409
grpc_channel, grpc_deadline,
26.3 KB
Binary file not shown.

google-assistant-sdk/tests/test_endtoend.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def device_model():
3434
'--project-id', PROJECT_ID,
3535
'register-model', '--model', device_model_id,
3636
'--type', 'LIGHT',
37+
'--trait', 'action.devices.traits.OnOff',
3738
'--manufacturer', 'assistant-sdk-test',
3839
'--product-name', 'assistant-sdk-test'])
3940
yield device_model_id
@@ -123,3 +124,22 @@ def test_endtoend_textinput(device_model, device_instance):
123124
assert err is None
124125
assert 'grapefruit' in out
125126
assert 'pamplemousse' in out
127+
128+
129+
def test_onoff_device_action(device_model):
130+
temp_dir = tempfile.mkdtemp()
131+
audio_out_file = os.path.join(temp_dir, 'out.raw')
132+
# Use an non-existing device config file intentionally
133+
# to force device registration.
134+
device_config = os.path.join(temp_dir, 'device_config.json')
135+
out = subprocess.check_output(['python', '-m',
136+
'googlesamples.assistant.grpc.pushtotalk',
137+
'--verbose',
138+
'--project-id', PROJECT_ID,
139+
'--device-model-id', device_model,
140+
'--device-config', device_config,
141+
'-i', 'tests/data/turnon.riff',
142+
'-o', audio_out_file],
143+
stderr=subprocess.STDOUT)
144+
assert 'turning device on' in builtins.str(out).lower()
145+
assert os.path.getsize(audio_out_file) > 0

0 commit comments

Comments
 (0)