Basic Device Handler for IP garage door sensor--help needed

I have a garage door sensor that will call a webhook whenever the door is either opened or closed. (I have complete control over the format of the webhook, the HTTP headers, and the data that is included in the call).

I’m trying to create a “Garage Door” Device Handler that uses the “Contact Sensor” capability to keep track of whether the door is currently opened or closed. I’ve included a parse() method, but beyond that I’m not sure how to subscribe my Device Handler to a certain URL/topic that I can then call from my garage door sensor to activate the parse() method.

I tried including an “installed()” and “initialized()” method in my Device Handler so that I could call the SUBSCRIBE HubAction, however, those functions don’t appear to get called.

Is it possible to do this without creating a SmartApp, or do I need to combine a Device Handler + SmartApp to keep track of the door open/closed position? If you know of any source code for other projects that utilize webhook updates from LAN devices, please let me know.

Thank you!!

Does anyone know if it’s possible to talk to the hub directly via its local IP (such as http://192.168.1.110:39500/) or if it’s required to go through api.smartthings.com via a SmartApp? I would prefer if my custom sensor could send updates to the hub directly instead of having to route them through the cloud. (This would allow it to continue functioning if the internet is down)

Thanks!

Hi Adam,

Did you get any further which your requirement? I am trying to do the same thing…

I cant find any documentation on where to use the Subscribe or whether to create a smart app in conjunction.

Many thanks in advance

Jonathan

This may be helpful:

i’ll have a look, thanks

Hi Jonathan,

Yes, I did figure it out. There is no REST endpoint exposed for the device handler itself. You need to create a companion Smart App that it responsible for handling the REST calls and changing the state of the device. You are not able to make local LAN calls to the hub directly (unfortunately), you have to call the Smartthings Graph API (using OAuth) in order to communicate with the Smart App endpoint.

Enjoy!

Hi Adam.

Thanks for your reply. That’s where I got to last night in my
understanding, just didn’t get chance to implement. The documentation isn’t
very clear. I don’t suppose you could share your smart app and device
handler python code could you? I’d like to see a full working example.

Thanks again
Jonathan

Hi Jonathan,

Below is a basic device handler and smart app (written in Groovy). Happy Coding!

Device Handler: http://pastebin.com/raw/tg1ncV04
Smart App: http://pastebin.com/raw/XTTBxemP

This is a tutorial showing how to generate an OAuth token and determine the URL for the REST endpoint:
http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/authorization.html

Thats great. I’ll get on to that today. Looks quite simple. I guess i was expecting it to be more complicated.

Many thanks

So i’ve created my device, device type and smart app. I am just working through setting up oauth. Am i correct in assuming that when i specify the redirect_uri that this is a url on the local device that i am connecting to (in my case the pi) which can authorize that oauth request?

I have never set up oauth on the pi.

Thanks again in advance.

The redirect_uri doesn’t really matter, you won’t ever use it again. Just make sure you use the same one for both requests. You can use http://localhost.com/ or whatever. You need to have the Network Tab of your Developer Tools open so you can grab the code out of the URL after it redirects to whatever domain you specified. You can do all this from your PC and then once you have the oAuth token, that is what you’ll need to use from the Raspberry Pi in order to authenticate your REST calls.

1 Like

Thanks so much for this thread, especially @adam1010 for the sample SmartApp and Device Handler code that you posted here. This was very helpful to me in my project to connect a few wired contact sensors & a motion sensor to SmartThings via a NodeMCU ESP8266 board. I basically copied your SmartApp code :blush:

Works like a charm. Here are some details of my project if anyone is interested:

The OAuth part was a little tricky. I used the Advanced REST Client chrome app to manually make the HTTP Post to obtain the OAuth token. As @adam1010 suggested, once you have the OAuth token, you can just code that into your RaspPi or NodeMCU to sign every request with it.