Hi,
I’m new to developing for Homey. I have experiance coding in c# and have a basis understanding of javascript and typescript. I’m however stuck in adding a driver for my app. I’m planing to develop a SmartThings integration.
I created a new project and added a driver. The implementation of my driver.ts looks like the following:
import Homey from ‘homey’;
import fetch, { Headers } from ‘node-fetch’;
class MyDriver extends Homey.Driver {
async onInit() {
this.log('MyDriver has been initialized');
}
async onPairListDevices() {
//TODO: Place variables in settings
var url = "https://api.smartthings.com/v1/devices?capability=airConditionerMode";
var token = "<api key here>";
this.log(url);
var requestHeaders = new Headers();
requestHeaders.append("Authorization", "Bearer " + token);
requestHeaders.append("Accept", "application/json");
requestHeaders.append("Content-Type", "application/json");
var deviceList: any = [];
this.log(deviceList);
await fetch(url, { method: 'Get', headers: requestHeaders })
.then(response => response.json())
.then(json => {
json.items.forEach((item: { label: any; deviceId: any; }) => {
var jsonData = {
'name': item.label,
'data': {
'id': item.deviceId
}
};
this.log(jsonData);
deviceList.push(jsonData);
});
})
.catch(error => this.log(error));
this.log(deviceList);
return deviceList;
}
}
module.exports = MyDriver;
All this.log’s are displayed, however the returned device list is empty. The api is returning data. The line this.log(jsonData) is returning data, after this.log(deviceList) is called. It’s probably an await or somethin like that. Does someone seeing what I’m doing wrong, or has a wodking example, calling an external API.
Thanks in advance.