Sending http requests to the local network (Hue)


(Jeff) #1

Hi all,

I’m trying to write a smart app to manage my Hue lights that will automatically turn them on with a specific scene set. The Hue REST API supports this behaviour (can tell a group of lights to recall a given scene) so I know I’m set there.

My problem is in how to actually send these requests from the SmartThings app to the Hue REST API. Going in I had assumed this would be the easy part, but it doesn’t seem to be that way.

So as I understand it, HttpGet/HttpPost don’t work on the local network. I’ve discovered SendHubCommand method, but I’m struggling to get it to work.

Based on what I’ve been able to find on these forums, my code currently looks like this:

def httpRequest = [
      	method:		"GET",
        path: 		"/api/hueUserName/groups",
        headers:	[
        				HOST:		"192.168.1.73",
						Accept: 	"*/*",
                    ]
	]

	def hubAction = new physicalgraph.device.HubAction(httpRequest)
	sendHubCommand(hubAction)

This compiles and executes at runtime, but it doesn’t appear to actually do anything. I’m listening for responses by subscribing to location event and looking for something that looks like a response to my request, but nothing ever comes back.

I’ve gone through the code for the Hue (Connect) app and it seems to send requests in a similar way to this, though I’ll confess that I find the code in that app to be pretty confusing.

Any help would be greatly appreciated.


(Jason Mok) #2

Try referring to @geko’s radio thermostat for LAN-to-LAN example. The documentation isn’t detailed at all, I’m speculating that probably there’s proper APIs coming and it isn’t ready to be shared yet. One of the catch of using LAN-to-LAN is your device network ID needs to be hexed ip address and port in order to listen to the response in parse() method


(Jeff) #3

Is that example a device type or a smart app? I think it’s a device type and from what I’ve read it seems like SendHubCommand behaves differently for apps than device types?

I found another example posted by @geko (here) that shows a SmartApp use of the call that doesn’t require the hexed IP iirc. Unfortunately, I’m not really sure where my code differs from that example, unless it’s in the HOST property of the call.


(Jason Mok) #4

You’d want to write a Event Handler SmartApp to manage SmartDevices. Refer to the [architecture] 1 of SmartThings.
In the case of Hue(Connect), this is actually a Service Manager SmartApp, which is different from Event Handler types.

in short:
Service Manager is used to discover devices,create devices and manage connections e.g REST. This is only for Cloud connected or LAN connected device
Event Handler is used to subscribe event/commands from a device, then take action like invoking commands on a device itself. Typically you don’t want to do any lower level communications like calling REST


(Jeff) #5

So looking at the way the different pieces of the Hue stack interact, it seems like there is a Hue Bulb Device Type that provides the methods that can be used in an Event Handler SmartApp. These methods delegate to the Hue (Connect) Service Manager app, which in turn issues the appropriate REST requests to the Hue API.

Assuming that understanding is correct, I’m not certain I can follow that pattern and still achieve what I’m looking to achieve.

What I’d like to do is use the Hue API to retrieve a list of light Groups and Scenes and then set a selected scene on the selected group in response to some event. These concepts don’t really fit into the Bulb device type, though it seems reasonable for the Service Manager to support them.

I guess what I’m missing is how to consume those methods from an Event Handler SmartApp if I was to add them on the Service Manager. Do I have to add some kind of Hue Group device type? Or would it make sense to add this capability to the Hue Bridge device type?

Thanks for all the help by the way.