Example
/drivers/my_driver/device.js
const Homey = require('homey');
class MyDevice extends Homey.Device {
async onInit() {
this.log('MyDevice has been initialized');
}
}
module.exports = MyDevice;
Instance Properties
driver
driver: Driverhomey
homey: HomeyInstance Methods
addCapability
(async) addCapability(capabilityId)Add a capability to this device. Note: this is an expensive method so use it only when needed.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string |
getAvailable
getAvailable(): booleanGet the device's availability
Returns
If the device is marked as available
getCapabilities
getCapabilities(): Array.<string>Get an array of capabilities
Returns
The device's capabilities array
getCapabilityOptions
getCapabilityOptions(capabilityId): anyGet a device's capability options.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string |
Returns
getCapabilityValue
getCapabilityValue(capabilityId): anyGet a device's capability value
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string |
Returns
The value, or null when unknown
getClass
getClass(): stringGet the device's class
Returns
The device's class
getData
getData(): anyGet the device's data object
Returns
The device's data object
getEnergy
getEnergy(): anyGet the device's energy object
Returns
The device's energy info object
getName
getName(): stringGet the device's name
Returns
The device's name
getSetting
getSetting(key): anyGet a device's setting value
Parameters
| Name | Type | Description |
|---|---|---|
key
|
string |
Returns
The value, or null when unknown
getSettings
getSettings(): anyGet the device's settings object
Returns
The device's settings object
getState
getState(): anyGet the device's state (capability values)
Returns
The device's state object
getStore
getStore(): anyGet the entire store
Returns
getStoreKeys
getStoreKeys(): Array.<string>Get all store keys.
Returns
getStoreValue
getStoreValue(key): anyGet a store value.
Parameters
| Name | Type | Description |
|---|---|---|
key
|
string |
Returns
value
hasCapability
hasCapability(capabilityId): booleanReturns true if the device has a certain capability
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string |
Returns
onAdded
onAdded()This method is called when the user adds the device, called just after pairing.
onDeleted
onDeleted()This method is called when the user deleted the device.
onDiscoveryAddressChanged
onDiscoveryAddressChanged(discoveryResult)This method is called when the device's address has changed.
Parameters
| Name | Type | Description |
|---|---|---|
discoveryResult
|
DiscoveryResult |
onDiscoveryAvailable
onDiscoveryAvailable(discoveryResult)This method is called when the device is found for the first time. Overload this method to create a connection to the device. Throwing here will make the device unavailable with the error message.
Parameters
| Name | Type | Description |
|---|---|---|
discoveryResult
|
DiscoveryResult |
onDiscoveryLastSeenChanged
onDiscoveryLastSeenChanged(discoveryResult)This method is called when the device has been found again.
Parameters
| Name | Type | Description |
|---|---|---|
discoveryResult
|
DiscoveryResult |
onDiscoveryResult
onDiscoveryResult(discoveryResult)This method is called when a device has been discovered. Overload this method, and return a truthy value when the result belongs to the current device or falsy when it doesn't. By default, the method will match on a device's data.id property.
Parameters
| Name | Type | Description |
|---|---|---|
discoveryResult
|
DiscoveryResult |
onInit
(async) onInit()This method is called when the device is loaded, and properties such as name, capabilities and state are available.
onRenamed
onRenamed(name)This method is called when the user updates the device's name. Use this to synchronize the name to the device or bridge.
Parameters
| Name | Type | Description |
|---|---|---|
name
|
string | The new name |
onSettings
(async) onSettings(event): Promise.<(string|void)>This method is called when the user updates the device's settings.
Parameters
| Name | Type | Description |
|---|---|---|
event
|
object | the onSettings event data |
oldSettings
|
object | The old settings object |
newSettings
|
object | The new settings object |
changedKeys
|
Array.<string> | An array of keys changed since the previous version |
|
|
Returns
return a custom message that will be displayed
onUninit
(async) onUninit()This method is called when the device is destroyed.
ready
(async) ready(): Promise.<void>Returns a Promise which is resolved when the Device is ready (Device#onInit has been run).
Returns
registerCapabilityListener
registerCapabilityListener(capabilityId, listener)Register a listener for a capability change event. This is invoked when a device's state change is requested.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string | |
listener
|
Device.CapabilityCallback |
Example
this.registerCapabilityListener('dim', async (value, opts) => {
this.log('value', value);
this.log('opts', opts);
});
registerMultipleCapabilityListener
registerMultipleCapabilityListener(capabilityIds, listener, timeout)Register a listener for multiple capability change events. The callback is debounced with timeout
This is invoked when a device's state change is requested.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityIds
|
Array.<string> | |
listener
|
Device.MultipleCapabilityCallback | |
timeout
|
number
default:250
|
The debounce timeout |
Example
this.registerMultipleCapabilityListener(['dim', 'light_hue', 'light_saturation'], async (capabilityValues, capabilityOptions) => {
this.log('capabilityValues', capabilityValues);
this.log('capabilityOptions', capabilityOptions);
}, 500);
removeCapability
(async) removeCapability(capabilityId)Removes a capability from this device. Any Flow that depends on this capability will become broken. Note: this is an expensive method so use it only when needed.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string |
setAlbumArtImage
(async) setAlbumArtImage(image): Promise.<any>setAvailable
(async) setAvailable(): Promise.<any>Set the device's availability to true
Returns
setCameraImage
(async) setCameraImage(id, title, image): Promise.<any>Set a device's camera image
Parameters
| Name | Type | Description |
|---|---|---|
id
|
string | Unique ID of the image (e.g. |
title
|
string | Title of the image (e.g. |
image
|
Image |
Returns
setCameraVideo
(async) setCameraVideo(id, title, video): Promise.<any>Set a device's camera stream
Parameters
| Name | Type | Description |
|---|---|---|
id
|
string | Unique ID of the video (e.g. |
title
|
string | Title of the video (e.g. |
video
|
Video | The video instance |
Returns
setCapabilityOptions
(async) setCapabilityOptions(capabilityId, options)Set a device's capability options. Note: this is an expensive method so use it only when needed.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string | |
options
|
object |
setCapabilityValue
(async) setCapabilityValue(capabilityId, value): Promise.<void>Set a device's capability value
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string | |
value
|
any |
Returns
setClass
(async) setClass(deviceClass): Promise.<void>Set the device's class Any Flow that depends on this class will become broken.
Parameters
| Name | Type | Description |
|---|---|---|
deviceClass
|
string |
Returns
setEnergy
(async) setEnergy(energy)Set the device's energy object
Parameters
| Name | Type | Description |
|---|---|---|
energy
|
object |
setLastSeenAt
(async) setLastSeenAt()Set a device's lastSeenAt property. This method should be called if the device is known to be alive and responding. Note: available since Homey v12.6.1.
setSettings
(async) setSettings(settings): Promise.<void>Set the device's settings object. The newSettings object may contain a subset of all settings.
Note that the Device#onSettings method will not be called when the settings are changed programmatically.
Parameters
| Name | Type | Description |
|---|---|---|
settings
|
any | A settings object |
Returns
setStoreValue
(async) setStoreValue(key, value): Promise.<void>Set a store value.
Parameters
| Name | Type | Description |
|---|---|---|
key
|
string | |
value
|
any |
Returns
setWarning
(async) setWarning(messageopt): Promise.<any>Set a warning message for this device, to be shown to the user. This message is persistent, so make sure to unset it when necessary.
Parameters
| Name | Type | Description |
|---|---|---|
message
|
string
|
null
<optional> |
Custom warning message, or |
Returns
triggerCapabilityListener
(async) triggerCapabilityListener(capabilityId, value, opts): Promise.<any>Trigger a capability listener programmatically.
Parameters
| Name | Type | Description |
|---|---|---|
capabilityId
|
string | |
value
|
any | |
opts
|
object |
Returns
unsetStoreValue
(async) unsetStoreValue(key): Promise.<void>Unset a store value.
Parameters
| Name | Type | Description |
|---|---|---|
key
|
string |
Returns
unsetWarning
(async) unsetWarning(): Promise.<any>Unset the warning message for this device
Returns
Type Definitions
CapabilityCallback
CapabilityCallback(value, opts): Promise.<void>|voidParameters
| Name | Type | Description |
|---|---|---|
value
|
any | The new value |
opts
|
any | An object with optional properties, e.g. |
Returns
MultipleCapabilityCallback
MultipleCapabilityCallback(capabilityValues, capabilityOptions): Promise.<void>|voidParameters
| Name | Type | Description |
|---|---|---|
capabilityValues
|
Object.<string, any> | An object with the changed capability values, e.g. |
capabilityOptions
|
Object.<string, any> | An object with optional properties, per capability, e.g. |