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

Commit a4f1aa3

Browse files
proppyGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "google-assistant-sdk/pushtotalk: fix device action handler init"
2 parents f146b6a + 1ef2e7b commit a4f1aa3

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:
@@ -407,6 +398,15 @@ def onoff(on):
407398
with open(device_config, 'w') as f:
408399
json.dump(payload, f)
409400

401+
device_handler = device_helpers.DeviceRequestHandler(device_id)
402+
403+
@device_handler.command('action.devices.commands.OnOff')
404+
def onoff(on):
405+
if on:
406+
logging.info('Turning device on')
407+
else:
408+
logging.info('Turning device off')
409+
410410
with SampleAssistant(lang, device_model_id, device_id,
411411
conversation_stream,
412412
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)