Help needed: how to connect to Homey using Javascript


#1

Hi all,

I’ve been writing my own “dashboard”-like web-app using JavaScript, running on an network-internal Apache web server. First to create a touchscreen interface only to trigger Flows by using the HTTP request app (by Erik van Dongen), which worked perfectly and was quite easy to do. Since then I’ve added the possibility to define different kinds of widgets and the ability to load more configurations (widget pages) so the same application can be used for multiple devices, whilst still being able to use different layouts per device and manage them from a central location.

Now however I’d like to extend the web application by adding the possibility to retrieve information from Homey, using JSON requests. I’ve added the athom-api.min.js to load as a script, but appear to be stuck at how to set up a connection to Homey’s API however. The documentation is not clear enough for me, I am not that experienced as a developer.

Would anyone here be willing to explain how to go about setting up that connection? Preferably without going to the cloud if that’s even possible these days. Once I have the connection set up I think I know how to use the API and get data… I hope… :slight_smile:

Any help is very much appreciated! Thanks!
DenW


#2

Athom has documented their API:
https://developer.athom.com/docs/api/HomeyAPI.html

It is not complete but there is a lot information. It is cloudbased however.


#3

Sounds like you want to recreate an existing app

https://apps.athom.com/app/com.swttt.homeydash

His code is on github, so you can look at that.


#4

Thanks Koktail,
Yes, I was/am aware of the documentation. That is exactly where I got stuck…
As I said, I’ve added the Athom javascript bit, but not sure how to proceed from there.
From what I gather, this bit of code should be a start:

function StartHomeyAPI() {
        HomeyCloudAPI = new AthomAPI.AthomCloudAPI({
  		clientId: HomeyID,
  		clientSecret: HomeyBearer,
  		redirectUrl: 'http://localhost'
	    });

        HUser = HomeyCloudAPI.getAuthenticatedUser();
        alert(HUser);

}

HomeyID and HomeyBearer are defined elsewhere.
Calling the function alerts an Object promise, but from what I gather that could also be an error. Not sure how to proceed, as I said this API stuff is new to me.

Thanks again,
DenW


#5

Hi Gruiter,

Not recreating, but building something similar, yes. Just a learning project…
swtt is using a javascript framework, whereas I’m trying to keep it simple and use javascript\html\css only. The use of the framework makes the code all but unreadable to me.

Thanks, DenW


#6

You can learn a great deal by looking to other developers code.
By searching github you can find some code examples:
https://github.com/search?q=getAuthenticatedUser+homey&type=Code

I’ve seen that you can get the homey from the user (that is provided by the promise).

HomeyCloudAPI.getAuthenticatedUser()
    .then(user => {
        const homeys = user.getHomeys();

        // log first homey object
        console.log(homeys[0]);

    }).catch(exception => {
  
    // log the exception
    console.log(exception);
});

Have a look at the javascript documentation from mozilla:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise


#7

Thanks Koktail…

I’ll have a look and study… :slight_smile: