Skip to content

Commit e87b4a6

Browse files
committed
googlesamples/grpc/device_helper: ignore noop execution
Bug: 66963608 Change-Id: Ia1b2074749266aa135a483ead3406c4fa6118947
1 parent 74ffa06 commit e87b4a6

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,24 @@ def decorator(fn):
6767
return decorator
6868

6969
def submit_commands(self, devices, execution):
70-
"""Submit device command executions."""
70+
"""Submit device command executions.
71+
72+
Returns: a list of concurrent.futures for scheduled executions.
73+
"""
7174
fs = []
7275
for device in devices:
73-
if device[key_id_] == self.device_id:
74-
for command in execution:
75-
f = self.executor.submit(
76-
self.dispatch_command, **command
77-
)
78-
fs.append(f)
79-
else:
76+
if device[key_id_] != self.device_id:
8077
logging.warning('Ignoring command for unknown device: %s'
8178
% device[key_id_])
79+
continue
80+
if not execution:
81+
logging.warning('Ignoring noop execution')
82+
continue
83+
for command in execution:
84+
f = self.executor.submit(
85+
self.dispatch_command, **command
86+
)
87+
fs.append(f)
8288
return fs
8389

8490
def dispatch_command(self, command, params=None):

google-assistant-sdk/tests/test_device_helpers.py

+27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ def build_device_request(device_id, command, arg):
3939
}
4040

4141

42+
def build_noop_device_request(device_id):
43+
return {
44+
'inputs': [{
45+
'intent': 'action.devices.EXECUTE',
46+
'payload': {
47+
'commands': [{
48+
'devices': [
49+
{'id': device_id}
50+
],
51+
'execution': None,
52+
}]
53+
}
54+
}],
55+
'requestId': '42'
56+
}
57+
58+
4259
class DeviceRequestHandlerTest(unittest.TestCase):
4360
def setUp(self):
4461
self.handler_called = False
@@ -83,6 +100,16 @@ def test_unknown_command(self):
83100
self.assertEqual(len(fs), 1)
84101
self.assertFalse(self.handler_called)
85102

103+
def test_noop_execution(self):
104+
device_handler = device_helpers.DeviceRequestHandler(
105+
'some-device',
106+
)
107+
device_handler.command('SOME_COMMAND')(self.handler)
108+
device_request = build_noop_device_request('some-device')
109+
fs = device_handler(device_request)
110+
self.assertEqual(len(fs), 0)
111+
self.assertFalse(self.handler_called)
112+
86113
def test_exception(self):
87114
err = Exception('some error')
88115

0 commit comments

Comments
 (0)