onPairListDevices, TypeScript and SDK3

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.

So the last this.log(deviceList) is also showing the correct data? What does the data look like, can you give an example?

Is the await in combination with the .then really working?
I would assume, that logging inside then is working, but still async.

If using await, shouldn’t the fetch promise return the result like
let result = await fetch(…);
let data = await result.json();

Then loop over the JSON and build the device list.

Marcel has a nice example (imho):

Yes, it is. A .then() chain ultimately returns a Promise, and that’s the one being await’ed on. Quick PoC that you can run in Node.js locally:

void async function() {
  let productList = [];
  await fetch('https://dummyjson.com/products')
    .then(response => response.json())
    .then(json => {
      for (const product of json.products) {
        productList.push({
          id:    product.id,
          title: product.title
        });
      }
    });
  console.log(productList);
}();
1 Like

Thanks for your reply. The returned element is an empty array.

Thanks for your reply, that is not working.

Can you elaborate on “not working” a bit more? And point towards the full code of your app.

Thanks

Hi Robert,
Thanks again. Today I ran the same script as before, and it executed as expected. Thanks for your reply.

Turned out the code was working. Somehow the app was not updated.

Just a thought, I think it is nicer to give Robert the ‘solution’ credit instead of yourself :wink:

1 Like

Hi @Peter_Kawa ,
Of course @robertklep deserves credits, as does anyone that has responded. And I can give him te solution, however my originl code was working and can be used by someone else for their implementation. How will it come clear to them, without reading the threat? Do you’ve got any suggestions?