Skip to content

Instantly share code, notes, and snippets.

@cwittyman
Last active December 20, 2015 11:48
Show Gist options
  • Save cwittyman/6125810 to your computer and use it in GitHub Desktop.
Save cwittyman/6125810 to your computer and use it in GitHub Desktop.
Smarthings Presence Pushover Alert
/**
* Who's coming or going?
*
* Author: [email protected]
* Date: 2013-07-31
*/
preferences {
section("When a presence sensor arrives or departs this location..") {
input "presence", "capability.presenceSensor", title: "Which sensor?"
}
section("Send Pushover alert (optional)"){
input "apiKey", "text", title: "Pushover API Key", required: false
input "userKey", "text", title: "Pushover User Key", required: false
input "deviceName", "text", title: "Pushover Device Name", required: false
input "arriveText", "text", title: "Arriving Text", required: true
input "leaveText", "text", title: "Leaving Text", required: true
input "priority", "enum", title: "Pushover Priority", required: false,
metadata :[
values: [ 'Low', 'Normal', 'High', 'Emergency']
]
input "sound", "enum", title: "Pushover Sound", required: false,
metadata:[
values:[ 'pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming', 'intermission', 'magic',
'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb', 'persistent', 'echo', 'updown', 'none' ]
]
}
}
def installed() {
subscribe(presence, "presence", presenceHandler)
}
def updated() {
unsubscribe()
subscribe(presence, "presence", presenceHandler)
}
def presenceHandler(evt) {
if (evt.value == "present") {
log.debug "${presence.label ?: presence.name} has arrived at the ${location}"
pushHandler('arrive')
} else if (evt.value == "not present") {
log.debug "${presence.label ?: presence.name} has left the ${location}"
pushHandler('leave')
}
}
def pushHandler(state){
def myMessage = ''
if(state == 'arrive'){
myMessage = "$arriveText"
}else if(state == 'leave'){
myMessage = "$leaveText"
}
log.debug myMessage
log.debug state
if(apiKey && userKey)
{
log.debug "Sending Pushover with API Key [$apiKey] and User Key [$userKey]"
def postBody = []
def pushPriority = 0
// Set Priority for Pushover Notification
if(priority == "Low")
{
pushPriority = -1
}
else if(priority == "Normal")
{
pushPriority = 0
}
else if(priority == "High")
{
pushPriority = 1
}
else if(priority == "Emergency")
{
pushPriority = 2
}
if(deviceName)
{
log.debug "Sending Pushover to Device: $deviceName"
if(pushPriority == 2)
{
postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: myMessage, priority: "$pushPriority", sound: "$sound", retry: "60", expire: "3600"]
}
else
{
postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: myMessage, priority: "$pushPriority", sound: "$sound"]
}
log.debug postBody
}
else
{
log.debug "Sending Pushover to All Devices"
if(pushPriority == 2)
{
postBody = [token: "$apiKey", user: "$userKey", message: myMessage, priority: "$pushPriority", sound: "$sound", retry: "60", expire: "3600"]
}
else
{
postBody = [token: "$apiKey", user: "$userKey", message: myMessage, priority: "$pushPriority", sound: "$sound"]
}
log.debug postBody
}
def params = [
uri: 'https://api.pushover.net/1/messages.json',
body: postBody
]
httpPost(params){ response ->
log.debug "Response Received: Status [$response.status]"
if(response.status != 200)
{
sendPush("Received HTTP Error Response. Check Install Parameters.")
}
}
}//End if API
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment