Most drivers will suffice using the provided pairing templates. Some advanced drivers however can benefit from creating their own views.
The pairing views consists of .html files in the /drivers/<driver_id>/pair-folder. Where the name of the file is the view ID, as id described in the App Manifest.
The pairing views have a few Homey-specific JavaScript functions available. They are documented below.
Back-end API
The session property passed in onPair can control the front-end programmatically.
/drivers/<driver_id>/driver.js
constHomey=require('homey');classDriverextendsHomey.Driver{asynconPair(session){ // Show a specific view by IDawaitsession.showView("my_view"); // Show the next viewawaitsession.nextView(); // Show the previous viewawaitsession.prevView(); // Close the pair sessionawaitsession.done(); // Received when a view has changedsession.setHandler("showView",asyncfunction(viewId){console.log("View: "+ viewId);});}}module.exports= Driver;
Front-end API
The following methods are available at the front-end to communicate with the back-end. The Homey object is globally available.
Emit an event
Homey.emit( String event, Mixed data ): Promise<any>
Emit an event to your app. The function called will be the one registered by session.setHandler( String event, Function callback ) in driver.js.
Receive an event
Homey.on( String event, Function callback )
Listen to a message from your app. You can trigger this function from your app by calling session.emit().
Set a Title
Homey.setTitle( String title )
Set the window's title.
Example
Set a Subtitle
Homey.setSubtitle( String subtitle )
Set the window's subtitle.
Example
Show a View
Homey.showView( String viewId )
Navigate to another view. The parameter viewId should be an ID as specified in your App Manifest.
The device object must at least contain the properties data and name and may contain icon, class, capabilities, capabilitiesOptions, store and settings.
Example:
Get current zone
Homey.getZone(): Promise<string>
Get the Zone ID of the active Zone. The promise resolves to the zone id.
Get the options of a view, or the current view when viewId is omitted. The promise resolves to the viewOptions of the specified view.
View options may be added to a view by specifying an options object in the App Manifest.
Set navigation close
Homey.setNavigationClose()
Remove all navigation buttons and show a single Close button.
Close the pair session
Homey.done()
Close the pairing window.
Alert dialog
Homey.alert( String message[, String icon] ): Promise<void> Show an alert dialog. The second parameter icon can be null, error, warning or info.
Confirm dialog
Homey.confirm( String message[, String icon] ): Promise<boolean> Show a confirm dialog. The second parameter icon can be null, error, warning or info.
The promise will resolve to true when the user pressed OK.
Popup
Homey.popup( String url ) Show a popup with a remote website.
Internationalisation
Homey.__( String key [, Object tokens] )
Translate a string programmatically. The first argument key is the name in your /locales/__language__.json. Use dots to get a sub-property, e.g. settings.title. The optional second argument tokens is an object with replacers.
Within your custom views, you can also use translations. For example:
<script type="application/javascript">
Homey.emit("my_event", { foo: "bar" }).then(function (result) {
console.log(result); // result is: Hello!
});
</script>
/drivers/<driver_id>/driver.js
const Homey = require('homey');
class Driver extends Homey.Driver {
async onPair(session) {
session.setHandler("my_event", async function (data) {
// data is { 'foo': 'bar' }
return "Hello!";
});
}
}
module.exports = Driver;
/drivers/<driver_id>/pair/start.html
<script type="application/javascript">
Homey.on("hello", function (message) {
Homey.alert(message); // Hello to you!
return "Hi!"; // send a reply back to the pairing session
// you can return a promise if you need to do some async
// work before replying to the message.
});
</script>
/drivers/<driver_id>/driver.js
const Homey = require('homey');
class Driver extends Homey.Driver {
async onPair(session) {
session.setHandler("showView", async (viewId) => {
if (viewId === "start") {
const data = await session.emit("hello", "Hello to you!");
console.log(data); // Hi!
}
});
}
}
module.exports = Driver;
<script type="application/javascript">
Homey.createDevice({
// The name of the device that will be shown to the user
name: "My Device",
// The data object is required and should contain only unique properties for the device.
// So a MAC address is good, but an IP address is bad (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: Initial device settings that can be changed by the user afterwards
settings: {
pincode: "1234",
},
})
.then(function (result) {
Homey.done();
})
.catch(function (error) {
Homey.alert(error);
});
</script>
/drivers/<driver_id>/pair/start.html
<script type="application/javascript">
function onHomeyReady(Homey) {
alert(Homey.__("pair.title")); // will alert "Settings page title"
}
</script>