API and Endpoints


(SStozki) #1

Hi all,

It would be nice to see how folks are setting up endpoints within their environments.


(Brandon De Young) #2

I’m wondering if someone can help me setup integration with Tasker using endpoints? Can’t seem to find a tutorial anywhere explaining how to enable that functionality.

Thanks,


(Csader) #3

I had a lengthy Project write-up on how to make this all work…looks like it got wiped out with the new forums. :frowning:


(Brandon De Young) #4

Yeah I haven’t been here to the new forum site yet so I’m still trying to understand how to get around and where to look for things. Maybe your write-up is somewhere here: http://community.smartthings.com/category/archive-migrated-from-old-forums?


(Brandon De Young) #5

Here’s a string where you talked about the write-up, but the link doesn’t work anymore…


(Csader) #6

Yeah, seems as though the write-up is gone. It was a 3-parter, so I’m not sure I want to go through the effort to re-write it here. Would have been nice if ST had archived those somewhere.


(Brandon De Young) #7

Damn, could you by chance let me know what the first steps were? If I recall correctly, there was just a link to click on to enable OAuth or something, and then it asked to check the box for each device you want to control. Then somewhere in the process it gave you the URL to use to view the devices and their control URLs. I guess the link to enable OAuth is really all I need as the rest I can probably figure out.


(Csader) #8

Yup, its http://labs.smartthings.com/exampleOauth.php

Let me know if you run into trouble after that.


(Brandon De Young) #9

Awesome! That was exactly what I needed. Thanks csader!


(Brandon De Young) #10

Hey csader, another quick question about this… do you know if it’s possible to run a SmartApp by use of endpoints? I see you can toggle switches, unlock doors, etc., but I’m wondering if you can run a SmartApp by doing an HTTP Get task through Tasker.

Thanks


(Matt Martz) #11

@triggertact yes, you can. It’s been a few months since I did ST development, but yes and you can pass things in the URL and I think I read somewhere that it would take a JSON object (but I never verified that). In the app you have a mapping section like:

   mappings {
      path("/panel/:eventcode/:zoneorpart") {
        action: [
          GET: "updateZoneOrPartition"
        ]
      }
    }

where “updateZoneOrPartition” is a function. If you check out the app portion of this repo

https://github.com/kholloway/smartthings-dsc-alarm

you’ll get an idea of how it works. If you’re asking what I think you are, that is…


(Brandon De Young) #12

Hmm, I’m not sure if this is or isn’t what I’m looking for. I’m real novice when it comes to the coding side of SmartThings. What I’m mainly concerned with is when I run through the process of Authorizing Endpoint Access here: http://labs.smartthings.com/exampleOauth.php that @csader just referenced above, it only seems to allow me to check boxes next to devices but not any smartapps.

In a separate forum string, I’m trying to figure out how to create a smartapp that has the little play button on its icon so I can activate it manually by pressing the app button, rather than having the app activate itself by way of some automated trigger specified within. The use case for this would be if I wanted to trigger a smartapp for a scenario not able to be planned for. For example, if I want to have all my lights dim when I want to watch a movie I would like to just manually activate the smartapp (or use AutoVoice/Tasker).

So, in this string, I’m trying to determine if I can have this smartapp be checkmarked at this Authorize Endpoint Access webpage so it can also be controlled through Tasker like my devices can. I’m wondering if your previous answer is indeed telling me that it can, but that it will take some coding to setup?


(Brandon De Young) #13

Hey guus, just wondering if anyone had any ideas regarding my last post. Hopefully it made sense! Basically, I’m just trying to activate a SmartApp using HTTP Get through Tasker/AutoVoice.

Thanks in advance for any suggestions!


(John Essey) #14

I dont have much help here, but I would like to see something “official” in the way of using the the OAuth.php to call particular apps. http://labs.smartthings.com/exampleOauth.php – while great, I need a bit more functionality besides toggle/on/off. I know it can be done… but like others here, it appears I’m missing one little step (mostly, in addressing how I call the OAuth Client and ID).

I think it would be wise (from an end user point of view) for SmartThings to really push this feature or help users get this specific aspect working. A little show and tell would go a long, long way. I’m pretty sure HomeKit is going to have a voice action feature to “turn on/off/dim”, etc., and I can only imagine what will come at I/O this year (and possibly make good on the Android@Home).

Without much more banter, I hope someone at SmartThings can hook up a show-n-tell on how to call custom SmartApps over HTTPS. Users seem to want it, the functionality is there…

Thanks!


(Cassidy) #15

@triggertact - take a look at my post on creating virtual switches that execute smartapps -

This should help lead you in the right direction.


(Brandon De Young) #16

@nelemansc Oh very cool! I’m definitely a novice when it comes to the code side of things. That said, is there an easy way to explain how to manipulate the code you created to execute SmartApps, rather than change modes?


(Cassidy) #17

Yup, you just need to add this to the preferences section of the Smartapp

    {
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
}

Followed by initializing the on/off handlers with this

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
    subscribe(theSwitch, "switch.Off", offHandler)
}

If you’d like help, go ahead and copy/paste the code for your Smartapp and I’ll edit it for you


(Brandon De Young) #18

Ok cool, I’m basically using the Make It So app… I pasted the two sections of code in that you provided, and I also added the virtual on/off switch as explained in your write-up. Then after installing the app to my phone, I selected this new virtual switch as the “Switch to Monitor” in the app, but when I turn the switch on it’s not executing the app. The app does execute when you touch its play button, though.


(Cassidy) #19

Make sure you also have the initialize in the def installed & def updated groupings

def installed() {
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def updated() {
	unsubscribe()
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

(Brandon De Young) #20

Hmmm, just added that in as well but still not executing when turning on the virtual switch. Here’s the full code I’m working with:

definition(
    name: "Movie Lights",
    namespace: "smartthings",
    author: "SmartThings",
    description: "Saves the states of a specified set switches and thermostat setpoints and restores them at each mode change. To use 1) Set the mode, 2) Change switches and setpoint to where you want them for that mode, and 3) Install or update the app. Changing to that mode or touching the app will set the devices to the saved state.",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch@2x.png"
)

preferences {
	section("Switches") {
		input "switches", "capability.switch", multiple: true, required: false
	}
	section("Thermostats") {
		input "thermostats", "capability.thermostat", multiple: true, required: false
	}
	section("Locks") {
		input "locks", "capability.lock", multiple: true, required: false
	}
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
}

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
    subscribe(theSwitch, "switch.Off", offHandler)
}

def installed() {
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def updated() {
	unsubscribe()
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def appTouch(evt)
{
	restoreState(currentMode)
}

def changedLocationMode(evt)
{
	restoreState(evt.value)
}

private restoreState(mode)
{
	log.info "restoring state for mode '$mode'"
	def map = state[mode] ?: [:]
	switches?.each {
		def value = map[it.id]
		if (value?.switch == "on") {
			def level = value.level
			if (level) {
				log.debug "setting $it.label level to $level"
				it.setLevel(level)
			}
			else {
				log.debug "turning $it.label on"
				it.on()
			}
		}
		else if (value?.switch == "off") {
			log.debug "turning $it.label off"
			it.off()
		}
	}

	thermostats?.each {
		def value = map[it.id]
		if (value?.coolingSetpoint) {
			log.debug "coolingSetpoint = $value.coolingSetpoint"
			it.setCoolingSetpoint(value.coolingSetpoint)
		}
		if (value?.heatingSetpoint) {
			log.debug "heatingSetpoint = $value.heatingSetpoint"
			it.setHeatingSetpoint(value.heatingSetpoint)
		}
	}

	locks?.each {
		def value = map[it.id]
		if (value) {
			if (value?.locked) {
				it.lock()
			}
			else {
				it.unlock()
			}
		}
	}
}


private saveState()
{
	def mode = currentMode
	def map = state[mode] ?: [:]

	switches?.each {
		map[it.id] = [switch: it.currentSwitch, level: it.currentLevel]
	}

	thermostats?.each {
		map[it.id] = [coolingSetpoint: it.currentCoolingSetpoint, heatingSetpoint: it.currentHeatingSetpoint]
	}

	locks?.each {
		map[it.id] = [locked: it.currentLock == "locked"]
	}

	state[mode] = map
	log.debug "saved state for mode ${mode}: ${state[mode]}"
	log.debug "state: $state"
}

private getCurrentMode()
{
	location.mode ?: "_none_"
}