クエリを実行して実行する

ユーザーが Google Assistant を操作してデバイスの現在の状態を照会すると、フルフィルメントは、デバイス ID のリストを含む action.devices.QUERY インテントを受け取ります(SYNC レスポンスで提供されます)。ユーザーがデバイスを操作するコマンドを Assistant に送信すると、フルフィルメントは action.devices.EXECUTE インテントを受け取ります。

QUERY インテントを処理する

QUERY レスポンスには、リクエストされたデバイスでサポートされている各トレイトのステータスがすべて含まれます。

リクエスト
{
    "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
      },
      "456": {
        "on": true,
        "online": true,
        "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
        },
        456: {
          on: true,
          online: true,
          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(
              "456",
              new HashMap<String, Object>() {
                {
                  put("on", true);
                  put("online", true);
                  put("brightness", 80);
                  put(
                      "color",
                      new HashMap<String, Object>() {
                        {
                          put("name", "cerulean");
                          put("spectrumRGB", 31655);
                        }
                      });
                }
              });
        }
      });

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

詳細については、QUERY インテントのリファレンス ドキュメントをご覧ください。

EXECUTE インテントを処理する

QUERY と同様に、1 つのインテントで複数のデバイス ID をターゲットに設定できます。1 つの EXECUTE インテントに、デバイスのグループに指定された複数のコマンドを含めることもできます。たとえば、トリガーされたインテントでは、ライトのグループの明るさと色の両方を設定したり、複数のライトをそれぞれ異なる色に設定したりできます。実行後、EXECUTE レスポンスでデバイスの新しい状態が返されます。

ユーザーのデバイスの状態が変更された場合は、Report State を使用します。たとえば、EXECUTE インテントや、ローカルの状態変化(照明のスイッチを手動で切り替えるなど)が原因で発生することがあります。これにより、Google Home Graph がクラウド サービスと同期されます。

リクエスト
{
    "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);
}

詳細については、EXECUTE インテントのリファレンス ドキュメントをご覧ください。

ステータス レスポンス

QUERYEXECUTE のレスポンスには、リクエストの結果を報告するための status フィールドが含まれています。ステータス レスポンスで返される可能性のある値は次のとおりです。

  • SUCCESS: リクエストが成功した。
  • OFFLINE: ターゲット デバイスがオフラインであるか、接続できない。
  • EXCEPTIONS: リクエストに関連する問題またはアラートがある。
  • ERROR: 対応する errorCode でリクエストが失敗した。

ERROREXCEPTIONS の詳細については、エラーと例外の処理エラーと例外をご覧ください。