Homey Community Forum

What is the best way to regularly poll an API for device state?

I’m all new to Homey Bridge, but I’ve started work on an app which will be able to control some of my thermostats.

These thermostats can be controlled through a REST API.

I regularly need to poll state from that API to retrieve the device state. For this, I’ve got the following questions :

  1. Should I just run a timer every n seconds using setInterval() as outlined in the documentation? Is there other ways of doing this which Homey approves?
onInit() {

    this.homey.setInterval(() => {
      // poll device state from API
    }, 30000);
  }
  1. Is there any best practise for poll intervals carried out by apps? For a thermostat, would polling the device state every 30 seconds be ok?

  2. Authentication towards the API is done with a username + password which returns a Bearer Token. The token expires every 24 hours, and I need to authenticate again with username and password. Is the settings variable the place to store username and password. I’m thinking of this from a security perspective.

I assume these data are stored on Homey servers. Is there any documentation on the security aspects of this?

First off: it sounds like you’re a community developer. If so, you should be aware that it’s likely that Athom will not accept your app. You also need to pay the €99 developer fee before they will even consider adding your app to the App Store.

That said: if your devices only support polling, you’re stuck with using a timer (like this.homey.setInterval or this.homey.setTimeout). There’s not way around that.

I don’t think there are best practises for intervals per se, but I’m sure Athom has ideas about it. You’ll hear about it when you have passed the aforementioned hurdles and submit your app for approval.

As for settings: if you want to offer the user to update/change the values, settings are the best way. Tokens, which aren’t “user-serviceable”, should be stored in the store. AFAIK there isn’t any documentation about how this is stored in the cloud, and how Athom will protect sensitive information.

Thanks @robertklep.

Yes, community developer is probably my category. I do however do software development on a daily basis through my own company, and I was hoping that Athom would kindly accept my 100€ through that company - granted that my app lives up the their standards.

Having just received a Homey Bridge with a beta code, it quickly turns out that although the bridge itself holds a lot of potential - it’s use right here and now is quite limited. The number of commercial products it is able to support of very limited. The willingness from commercial vendors to actually involve in development of e.g. Homey apps is, in my experience, very limited as well. They all tend to just want to publish their own apps. So, my hope is that Athom is open to contributions also from community developers who are willing to pay the 100€ and invest their spare time - all for the benefit of Athom ad other users of Homey :slight_smile:

Thank you for your comments on my questions. Yes, so the API is a simple REST api, and seems to offer no way of subscribing to notifications. I believe Homey do support webhooks in some way, but it seems that I cannot use that here.

Thanks also for your comments on the settings.

Also, the REST API you’re referring to is a cloud-based API, right? Because the Bridge cannot access devices locally over the network.

Yes, the API is cloud based so that part should be OK.

One more thing @robertklep, I can’t find any details on recommended dimensions for small and large device images. Do you happen to know if there are any recommendations on this?

{
    "name": {
        "en": "devicename"
    },
    "images": {
        "small": "/drivers/[...]/assets/images/small.png",
        "large": "/drivers/[...]/assets/images/large.png"
    },

Ah, thanks - i didn’t catch it there!