Smart Home Switch Guide

Device TYPE

action.devices.types.SWITCH Switch, a basic device in Smart Home, has binary modes on/off only.

action.devices.traits.OnOff

These are our recommendations for traits on this type of device, however you are free to mix and match from all available traits to best match your existing product functionality.

Please see each individual trait document for implementation details like required attributes, EXECUTE and QUERY.

Sample SYNC Request and Response

This is an example using the device type and traits above. It is intended to give an idea of how to build a SYNC response. If you add or remove traits, this will need to be modified to reflect those changes.

Initialize switch setup.

Request
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.SYNC"
    }]
}
Node.js
'use strict';

const {smarthome} = require('actions-on-google');
const functions = require('firebase-functions');

const app = smarthome();

app.onSync((body, headers) => {
  return {
    requestId: body.requestId,
    payload: {
      agentUserId: '1836.15267389',
      devices: [{
        id: '123',
        type: 'action.devices.types.SWITCH',
        traits: [
          'action.devices.traits.OnOff'
        ],
        name: {
          defaultNames: ['Smart Switch'],
          name: 'switch',
          nicknames: []
        },
        willReportState: false,
        deviceInfo: {
          manufacturer: 'AAA',
          model: '442',
          hwVersion: '3.2',
          swVersion: '11.4'
        },
        customData: {
          fooValue: 10,
          barValue: true,
          bazValue: 'switchabc'
        }
      }]
    }
  };
});

// ...

exports.smarthome = functions.https.onRequest(app);
Java
@NotNull
@Override
public SyncResponse onSync(@NotNull SyncRequest syncRequest,
    @Nullable Map<?, ?> headers) {
  SyncResponse.Payload payload = new SyncResponse.Payload();
  payload.setAgentUserId("1836.15267389");
  payload.setDevices(new SyncResponse.Payload.Device[] {
      new SyncResponse.Payload.Device.Builder()
          .setId("123")
          .setType("action.devices.types.SWITCH")
          .addTrait("action.devices.traits.OnOff")
          .setName(
              Collections.singletonList("Smart Switch"),
              "switch",
              new ArrayList()
          )
          .setWillReportState(true)
          .setDeviceInfo("AAA", "442", "3.2", "11.4")
          .setCustomData(new JSONObject()
              .put("fooValue", 10)
              .put("barValue", true)
              .put("bazValue", "goatjive")
              .toString()
          )
          .build()
  });
  return new SyncResponse(syncRequest.getRequestId(), payload);
}
JSON
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "agentUserId": "1836.15267389",
    "devices": [
      {
        "id": "123",
        "type": "action.devices.types.SWITCH",
        "traits": [
          "action.devices.traits.OnOff"
        ],
        "name": {
          "defaultNames": [
            "Smart Switch"
          ],
          "name": "switch",
          "nicknames": []
        },
        "willReportState": false,
        "deviceInfo": {
          "manufacturer": "AAA",
          "model": "442",
          "hwVersion": "3.2",
          "swVersion": "11.4"
        },
        "customData": {
          "fooValue": 10,
          "barValue": true,
          "bazValue": "switchabc"
        }
      }
    ]
  }
}
Validator

Device ERRORS

See the full list of errors and exceptions.