Pairing

Pairing allows users to add new devices to Homey.

Pairing is started when the user selects the device they want to add from the Homey app. The pair property of the driver defines a list of views, which the user navigates through. These views are called pairing templates. Homey includes a set of system templates that implement consistent pairing steps for most devices.

Homey already knows how to pair Zigbee and Z-Wave devices so it is not possible to implement your own pairing for those devices. Read the Zigbee and Z-Wave documentations to learn how to pair devices using those technologies with Homey.

Basic pairing example

This example is a basic way to enable pairing in your driver. To add pairing to your driver, add the following to your driver.compose.json:

/drivers/<driver_id>/driver.compose.json
{
  "name": { "en": "My Driver" },
  "images": {
    "small": "/drivers/my_driver/assets/images/small.png",
    "large": "/drivers/my_driver/assets/images/large.png"
  },
  "class": "socket",
  "capabilities": ["onoff"],
  "platforms": ["local", "cloud"],
  "connectivity": "cloud",
  "pair": [
    {
      "id": "list_my_devices",
      // we use a system template here, for consistency, and less work for us!
      "template": "list_devices",
      // show pair view with id 'add_my_devices' when clicked 'Next'
      "navigation": { "next": "add_my_devices" }
    },
    {
      "id": "add_my_devices",
      // again, use a template
      "template": "add_devices"
    }
  ]
}

This defines a pairing process with 2 steps. The first step requires the user to pick the devices to add from a list and the second step will automatically add these devices to Homey. Both these steps use Homey's built-in pairing templates list_devices and add_devices. The navigation option determines which of the steps the pairing will go to when the user presses the "Next" button.

The navigation object also supports a prev option for when a user can go back to the previous screen. This can be useful, for example, with a login_credentials system templates to allow users to retry logging-in.

If your pairing only uses the list_devices and add_devices templates you can use the Driver#onPairListDevices() method to quickly implement a pairing process. From this method you can return a list of devices that will be presented to the user.

/drivers/<driver_id>/driver.js
const Homey = require("homey");
const DeviceApi = require("device-api");

class Driver extends Homey.Driver {
  async onPairListDevices() {
    const devices = await DeviceApi.discoverDevices();
    return devices;
  }
}

module.exports = Driver;

Device pairing data

The following is an overview of the data you can supply for a device to be added, you should return an array of objects like this from Driver#onPairListDevices() or session.setHandler("list_devices").

{
  // The name of the device that will be displayed
  name: "My Device",

  // The data object is required and should be unique for the device.
  // So a device's MAC address would be good, but an IP address would
  // be bad since it can change over time.
  data: {
    id: "abcd",
  },

  // Optional: The store is dynamic and persistent storage for your device
  store: {
    // For example store the IP address of your device
    address: "127.0.0.1",
  },

  // Optional: sets the devices initial settings, this allows users to change
  // them after pairing in the device settings screen.
  settings: {
    pincode: "1234",
  },

  // Optional: These properties overwrite the defaults
  // that you specified in the driver manifest:
  icon: "/my_icon.svg", // relative to: /drivers/<driver_id>/assets/
  capabilities: ["onoff", "target_temperature"],
  capabilitiesOptions: {
    target_temperature: {
      min: 5,
      max: 35,
    },
  },
}

System Views

The pairing templates are built with HTML, CSS and JavaScript. Most drivers will suffice using the system templates that are provided by Homey.

Custom Views

Most drivers will suffice using the provided templates. In certain cases you may want, or need, to create pairing screens that are more suited to your driver. For these cases it is possible to create custom pairing views, to learn more read the custom pairing view guide.

Repairing

To ensure users with a great experience, your app's devices should always stay available without user interaction.

However, sometimes when a device explicitly needs user interaction to be fixed (for example an OAuth2 token has been revoked and the user needs to authenticate again), the user can initiate a repair process.

To enable repairing, you must add support for this to your driver by adding repair to your App Manifest:

/drivers/<driver_id>/driver.compose.json
{
  "name": { "en": "My Driver" },
  "images": {
    "small": "/drivers/my_driver/assets/images/small.png",
    "large": "/drivers/my_driver/assets/images/large.png"
  },
  "repair": [
    {
      "id": "login_oauth2",
      "template": "login_oauth2"
    }
  ]
}

It is also possible to use custom pairing views for repairing. To learn more about custom pairing templates read the custom pairing view guide.

Note that when repairing the Homey.createDevice() method is not available in the custom view and you can add a onRepair method to your driver, which is similar to the onPair method.

/drivers/<driver_id>/driver.js
const Homey = require("homey");

class Driver extends Homey.Driver {
  onRepair(session, device) {
    // Argument session is a PairSocket, similar to Driver.onPair
    // Argument device is a Homey.Device that's being repaired

    session.setHandler("my_event", (data) => {
      // Your code
    });

    session.setHandler("disconnect", () => {
      // Cleanup
    });
  }
}

module.exports = Driver;

Last updated