You are currently viewing the Homey Apps SDK v2 documentation. New apps should use Homey Apps SDK v3 ››

Homey.Driver

const { Driver } = require('homey');

The Driver class manages all Device instances, which represent all paired devices. This class should be extended and exported from driver.js. Methods prefixed with on are meant to be overriden. It is not allowed to overwrite the constructor.

Table of Contents

Library:

Methods

getDevice(deviceData) → {Device}

Get a Device instance by its deviceData object.

Parameters:
Name Type Description
deviceData Object

Unique Device object as provided during pairing

Returns:
Type:
Device

Device

getDevices() → {Array}

Get an Array with all Device instances

Returns:
Type:
Array

Devices

getDiscoveryStrategy() → {DiscoveryStrategy}

Get the driver's discovery strategy when defined in the manifest

Returns:
Type:
DiscoveryStrategy

getManifest() → {Object}

Gets the driver's manifest (app.json entry)

Returns:
Type:
Object

onInit()

This method is called when the driver is inited.

onMapDeviceClass(device)

When this method exists, it will be called prior to initing the device instance. Return a class that extends Device.

Parameters:
Name Type Description
device Device

A temporary Device instance to check certain properties before deciding which class the device should use. This class will exist for a single tick, and does not support async methods.

Example
class MyDriver extends Homey.Driver {

  onMapDeviceClass( device ) {
    if( device.hasCapability('dim') ) {
      return MyDeviceDim;
    } else {
      return MyDevice;
    }
  }
}

onPair(socket)

This method is called when a pair session starts.

Parameters:
Name Type Description
socket EventEmitter

Bi-directional socket for communication with the front-end

onPairListDevices(data, callbackopt)

This method is called when no custom onPair() method has been defined, and the default is being used. Simple drivers should override this method to provide a list of devices ready to be paired.

Parameters:
Name Type Attributes Description
data Object

Empty object

callback function <optional>
Name Type Description
err Error
result Array

An array of device objects

onRepair(socket, device)

This method is called when a repair session starts.

Parameters:
Name Type Description
socket EventEmitter

Bi-directional socket for communication with the front-end

device Device

ready(callback)

Pass a callback method, which is called when the Driver is ready (Driver#onInit has been run). The callback is executed immediately when the Drivers Manager was already ready.

Parameters:
Name Type Description
callback function