[autotest] cras_utils: Add method to query node id from node type

This method will be used when user wants to set active node type through
chrome.audio API.

BUG=chromium:552256
TEST=not used yet

Change-Id: I22751be3457f37381868cdd566dedb37fbceaafb
Reviewed-on: https://chromium-review.googlesource.com/311218
Commit-Ready: Cheng-Yi Chiang <cychiang@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Kalin Stoyanov <kalin@chromium.org>
diff --git a/client/cros/audio/cras_utils.py b/client/cros/audio/cras_utils.py
index 35fcbca..d3c49a8 100644
--- a/client/cros/audio/cras_utils.py
+++ b/client/cros/audio/cras_utils.py
@@ -11,6 +11,11 @@
 
 _CRAS_TEST_CLIENT = '/usr/bin/cras_test_client'
 
+
+class CrasUtilsError(Exception):
+    pass
+
+
 def playback(*args, **kargs):
     """A helper function to execute the playback_cmd.
 
@@ -493,3 +498,20 @@
 
     """
     get_cras_control_interface().RemoveActiveInputNode(node_id)
+
+
+def get_node_id_from_node_type(node_type):
+    """Gets node id from node type.
+
+    @param types: A node type defined in CRAS_NODE_TYPES.
+
+    """
+    nodes = get_cras_nodes()
+    find_ids = []
+    for node in nodes:
+        if node['Type'] == node_type:
+            find_ids.append(node['Id'])
+    if len(find_ids) != 1:
+        raise CrasUtilsError(
+                'Can not find unique node id from node type %s' % node_type)
+    return find_ids[0]