When users interact with Google Assistant to query the current
state of a device, your fulfillment receives an
[`action.devices.QUERY`](https://developers.home.google.com/cloud-to-cloud/intents/query.md.txt) intent containing a
list of device IDs (as provided by your `SYNC` response).
Your fulfillment receives an
[`action.devices.EXECUTE`](https://developers.home.google.com/cloud-to-cloud/intents/execute.md.txt) intent
when users send commands to Assistant to control your
device.

## Handle `QUERY` intents

Your `QUERY` response includes a full set of states for each of the traits
supported by the requested devices.
Request

```
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.QUERY",
      "payload": {
        "devices": [{
          "id": "123",
          "customData": {
            "fooValue": 74,
            "barValue": true,
            "bazValue": "foo"
          }
        }, {
          "id": "456",
          "customData": {
            "fooValue": 12,
            "barValue": false,
            "bazValue": "bar"
          }
        }]
      }
    }]
}
```

##### JSON

```
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "devices": {
      "123": {
        "on": true,
        "online": true,
        "status": "SUCCESS"
      },
      "456": {
        "on": true,
        "online": true,
        "status": "SUCCESS",
        "brightness": 80,
        "color": {
          "name": "cerulean",
          "spectrumRGB": 31655
        }
      }
    }
  }
}
```

##### Node.js

```
const {smarthome} = require('actions-on-google');
const app = smarthome();
// ...
app.onQuery((body, headers) => {
  // TODO Get device state
  return {
    requestId: body.requestId,
    payload: {
      devices: {
        123: {
          on: true,
          online: true,
          status: "SUCCESS"
        },
        456: {
          on: true,
          online: true,
          status: "SUCCESS",
          brightness: 80,
          color: {
            name: "cerulean",
            spectrumRGB: 31655
          }
        }
      }
    }
  };
});
```

##### Java

```
@NotNull
@Override
public QueryResponse onQuery(@NotNull QueryRequest queryRequest, @Nullable Map<?, ?> map) {
  QueryResponse.Payload payload = new QueryResponse.Payload();
  payload.setDevices(
      new HashMap<String, Map<String, Object>>() {
        {
          put(
              "123",
              new HashMap<String, Object>() {
                {
                  put("on", true);
                  put("online", true);
                  put("status", "SUCCESS");
                }
              });
          put(
              "456",
              new HashMap<String, Object>() {
                {
                  put("on", true);
                  put("online", true);
                  put("status", "SUCCESS");
                  put("brightness", 80);
                  put(
                      "color",
                      new HashMap<String, Object>() {
                        {
                          put("name", "cerulean");
                          put("spectrumRGB", 31655);
                        }
                      });
                }
              });
        }
      });

  return new QueryResponse(queryRequest.getRequestId(), payload);
}
```

For more information, see the [`QUERY`](https://developers.home.google.com/cloud-to-cloud/intents/query.md.txt) intent
reference documentation.

## Handle `EXECUTE` intents

Similar to `QUERY`, a single intent can target multiple device IDs. A single
`EXECUTE` intent may also contain multiple distinct commands given to a group
of devices. For example, a triggered intent may set both brightness and color
on a group of lights, or set multiple lights each to a different color. Your
`EXECUTE` response should return the new state of the device after the
execution.

Use [Report State](https://developers.home.google.com/cloud-to-cloud/integration/report-state.md.txt)
when the state of the users' device changes. For example, due to an `EXECUTE`
intent or a local state change (such as manually flipping a light switch).
This keeps Google Home Graph synchronized with your cloud service.
Request

```
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.EXECUTE",
      "payload": {
        "commands": [{
          "devices": [{
            "id": "123",
            "customData": {
              "fooValue": 74,
              "barValue": true,
              "bazValue": "sheepdip"
            }
          }, {
            "id": "456",
            "customData": {
              "fooValue": 36,
              "barValue": false,
              "bazValue": "moarsheep"
            }
          }],
          "execution": [{
            "command": "action.devices.commands.OnOff",
            "params": {
              "on": true
            }
          }]
        }]
      }
    }]
}
```

##### JSON

```
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "commands": [
      {
        "ids": [
          "123"
        ],
        "status": "SUCCESS",
        "states": {
          "on": true,
          "online": true
        }
      },
      {
        "ids": [
          "456"
        ],
        "status": "ERROR",
        "errorCode": "deviceTurnedOff"
      }
    ]
  }
}
```

##### Node.js

```
const {smarthome} = require('actions-on-google');
const app = smarthome();
// ...
app.onExecute((body, headers) => {
  // TODO Send command to device
  return {
    requestId: body.requestId,
    payload: {
      commands: [{
        ids: ["123"],
        status: "SUCCESS",
        states: {
          on: true,
          online: true
        }
      }, {
        ids: ["456"],
        status: "ERROR",
        errorCode: "deviceTurnedOff"
      }]
    }
  };
});
```

##### Java

```
@NotNull
@Override
public ExecuteResponse onExecute(
    @NotNull ExecuteRequest executeRequest, @Nullable Map<?, ?> map) {
  ExecuteResponse.Payload payload = new ExecuteResponse.Payload();

  payload.setCommands(
      new Commands[] {
        new Commands(
            new String[] {"123"},
            "SUCCESS",
            new HashMap<String, Object>() {
              {
                put("on", true);
                put("online", true);
              }
            },
            null,
            null),
        new Commands(new String[] {"456"}, "ERROR", null, "deviceTurnedOff", null)
      });
  return new ExecuteResponse(executeRequest.getRequestId(), payload);
}
```

For more information, see the [`EXECUTE`](https://developers.home.google.com/cloud-to-cloud/intents/execute.md.txt)
intent reference documentation.

## Status responses

Your `QUERY` and `EXECUTE` responses include a `status` field to report the
result of the request. Each status response can provide one of the following
values:

- `SUCCESS`: The request succeeded.
- `OFFLINE`: Target device is offline or otherwise unreachable.
- `EXCEPTIONS`: There is an issue or alert associated with the request.
- `ERROR`: The request failed with the corresponding `errorCode`.

For `ERROR` and `EXCEPTIONS`, see
[Handle errors and exceptions](https://developers.home.google.com/cloud-to-cloud/integration/error-handling.md.txt) and
[Errors and exceptions](https://developers.home.google.com/cloud-to-cloud/intents/errors-exceptions.md.txt) for more
details.