Help with HUE API Groovy Code Please

I am trying to create an http button that supports hue scenes and transition times. My code throws no errors, but it doesn't turn my lights on either. How do I capture the return json from the hue API and then log the return so I can see what HE is receiving?

Here is my code:

/**
 *  Hue HTTP Momentary Switch
 *
 *  Copyright 2018 Daniel Ogorchock
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 *  Change History:
 *
 *    Date        Who            What
 *    ----        ---            ----
 *    2018-02-18  Dan Ogorchock  Original Creation
 *    2019-11-01  Joe Rosiak	 Modified to support Hue Scenes
 *
 */
metadata {
	definition (name: "Hue HTTP Momentary Switch", namespace: "Keo", author: "Joe Rosiak") {
        capability "Switch"
        capability "Momentary"
        capability "Switch Level"
	}

	preferences {
		input(name: "hueWhiteListUser", type: "sting", title: "Hue Whitelist User", required: true, displayDuringSetup: true)
		input(name: "deviceIP", type: "string", title:"Device IP Address", description: "Enter IP Address of your HTTP server", required: true, displayDuringSetup: true)
		input(name: "devicePort", type: "string", title:"Device Port", description: "Enter Port of your HTTP server (defaults to 80)", defaultValue: "80", required: false, displayDuringSetup: true)
		//input(name: "devicePath", type: "string", title:"URL Path", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
		input(name: "deviceMethod", type: "enum", title: "POST, GET, or PUT", options: ["POST","GET","PUT"], defaultValue: "POST", required: true, displayDuringSetup: true)
	    input(name: "hueGroupID", type: "string", title: "Hue Group ID", required: true, displayDuringSetup: true)
		input(name: "hueSceneID", type: "string", title: "Hue Scene ID", required: true, displayDuringSetup: true)
		input(name: "hueSceneTTime", type: "int", title: "Hue Scene Transition time", required: true, displayDuringSetup: true)		
	}
}

def parse(String description) {
	log.debug(description)
}

def push() {
    //toggle the switch to generate events for anything that is subscribed
    sendEvent(name: "switch", value: "on", isStateChange: true)
    runIn(1, toggleOff)
    //sendEvent(name: "switch", value: "off", isStateChange: true)
    runCmd(devicePath, deviceMethod)
}

def toggleOff() {
    sendEvent(name: "switch", value: "off", isStateChange: true)
}

def on() {
	push()
}

def off() {
	push()
}

def setheattemp() {
  def path = "/setheat?" + "somevar"
  //Add logic to controll heat
}

def setfanspeed() {
  def path = "/setfanspeed?" + "somevar"
  //Add logic to control the fan speed
}

def toggleoscillation() {
  def path = "/oscillation?" + "somevar"
  //Add logic to toggle oscillation
}

def runCmd(String varCommand, String method) {
	def localDevicePort = (devicePort==null) ? "80" : devicePort
	def path = varCommand 
	def body = "{ 'scene': '${hueSceneID}', 'transitiontime': '${hueSceneTTime}'}" 
	def headers = [:] 
	headers.put("HOST", "${deviceIP}/api/${hueWhiteListUser}/groups/${hueGroupID}/action")
	//:${localDevicePort}")
	headers.put("Content-Type", "application/x-www-form-urlencoded")

	try {
		def hubAction = new hubitat.device.HubAction(
			method: method,
			path: path,
			body: body,
			headers: headers
			)
		log.debug hubAction
		return hubAction
	}
	catch (Exception e) {
        log.debug "runCmd hit exception ${e} on ${hubAction}"
	}  
}

Doing a quick scan what immediately pops out is your hand rolled* json using single quotes('), which must be double quotes(") to be valid. Assuming the parser isn't totally lax, it won't parse your body.

*) groovy has nice object -> json library, why do people always hand roll json? Hand rolling it just ensurea it'll be wrong at some point especially when you're mixing ' and " and/or escaping " quotes.

I can see that you're creating a HubAction, but you're not doing anything with it; you need to use

sendHubCommand(hubAction)

for it to do something with the action.

You will get the results in your parse() method

It may be easier to just use the httpPost() or asyncHttpPost() method - see Common Methods Object - Hubitat Documentation

You know.... I completely forgot to check fo this. I code in python quite a bit and knew it worked with that language.
I also just wanted to see this work, so this wasn't going to be the final product . I'll get this corrected. Thanks!

I'll check this out! thanks

@keo, you should probably check out the the latest update from @ogiewon's http switch (he updated it a week ago). There is already some added functionality in there for make a json POST request that was added precisely to work with the Hue Api. You obviously want to add a lot more customization, but it would at least be a good template to start from. For the Hue api to accept commands (not sure about scenes but probably the same) you would need to POST with a json content-type. That functionality has been added to the driver already.

3 Likes