Advertisement
Guest User

HTTP Button Creator Device Handler On/Off Switch

a guest
Nov 23rd, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.98 KB | None | 0 0
  1. /*
  2. *  HTTP Button
  3. *  Category: Device Handler
  4. *
  5. *  Source: https://community.smartthings.com/t/beta-release-uri-switch-device-handler-for-controlling-items-via-http-calls/37842
  6. *
  7. *  Credit: tguerena, surge919, CSC
  8. */
  9.  
  10. import groovy.json.JsonSlurper
  11.  
  12. metadata {
  13.     definition (name: "HTTP Button", namespace: "sc", author: "SC") {
  14.         capability "Switch"
  15.         attribute "triggerswitch", "string"
  16.         command "DeviceTrigger"
  17.     }
  18.  
  19.     preferences {
  20.         input("DeviceIP", "string", title:"Device IP Address", description: "Please enter your device's IP Address", required: true, displayDuringSetup: true)
  21.         input("DevicePort", "string", title:"Device Port", description: "Empty assumes port 80.", required: false, displayDuringSetup: true)
  22.         input("DevicePathOn", "string", title:"URL Path for ON", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
  23.         input("DevicePathOff", "string", title:"URL Path for OFF", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
  24.         input(name: "DevicePostGet", type: "enum", title: "POST or GET", options: ["POST","GET"], defaultValue: "POST", required: false, displayDuringSetup: true)
  25.         section() {
  26.             input("HTTPAuth", "bool", title:"Requires User Auth?", description: "Choose if the HTTP requires basic authentication", defaultValue: false, required: true, displayDuringSetup: true)
  27.             input("HTTPUser", "string", title:"HTTP User", description: "Enter your basic username", required: false, displayDuringSetup: true)
  28.             input("HTTPPassword", "string", title:"HTTP Password", description: "Enter your basic password", required: false, displayDuringSetup: true)
  29.         }
  30.     }
  31.  
  32.  
  33.     // simulator metadata
  34.     simulator {
  35.     }
  36.  
  37.     // UI tile definitions
  38.     tiles {
  39.         standardTile("DeviceTrigger", "device.switch", width: 2, height: 2, canChangeIcon: true) {
  40.             state "off", label: 'Off', action: "on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
  41.             state "on", label: 'On', action: "off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
  42.         }
  43.         main "DeviceTrigger"
  44.             details (["DeviceTrigger"])
  45.     }
  46. }
  47.  
  48. def parse(String description) {
  49.     log.debug(description)
  50. }
  51.  
  52. def on() {
  53.     log.debug "---ON COMMAND--- ${DevicePathOn}"
  54.     sendEvent(name: "switch", value: "on", isStateChange: true)
  55.     runCmd(DevicePathOn)
  56. }
  57.  
  58. def off() {
  59.     log.debug "---OFF COMMAND--- ${DevicePathOff}"
  60.     sendEvent(name: "switch", value: "off", isStateChange: true)
  61.     runCmd(DevicePathOff)
  62. }
  63.  
  64. def runCmd(String varCommand) {
  65.     def host = DeviceIP
  66.     def LocalDevicePort = ''
  67.     if (DevicePort==null) { LocalDevicePort = "80" } else { LocalDevicePort = DevicePort }
  68.  
  69.     def userpassascii = "${HTTPUser}:${HTTPPassword}"
  70.     def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
  71.  
  72.     log.debug "The device id configured is: $device.deviceNetworkId"
  73.  
  74.     def path = varCommand
  75.     log.debug "path is: $path"
  76.     //log.debug "Uses which method: $DevicePostGet"
  77.     def body = ""
  78.     //log.debug "body is: $body"
  79.  
  80.     def headers = [:]
  81.     headers.put("HOST", "$host:$LocalDevicePort")
  82.     headers.put("Content-Type", "application/x-www-form-urlencoded")
  83.     if (HTTPAuth) {
  84.         headers.put("Authorization", userpass)
  85.     }
  86.     log.debug "The Header is $headers"
  87.     def method = "POST"
  88.     try {
  89.         if (DevicePostGet.toUpperCase() == "GET") {
  90.             method = "GET"
  91.             }
  92.         }
  93.     catch (Exception e) {
  94.         settings.DevicePostGet = "POST"
  95.         log.debug e
  96.         log.debug "You must not have set the preference for the DevicePOSTGET option"
  97.     }
  98.     log.debug "The method is $method"
  99.     try {
  100.         def hubAction = new physicalgraph.device.HubAction(
  101.             method: method,
  102.             path: path,
  103.             body: body,
  104.             headers: headers
  105.             )
  106.         log.debug hubAction
  107.         return hubAction
  108.     }
  109.     catch (Exception e) {
  110.         log.debug "Hit Exception $e on $hubAction"
  111.     }
  112.    
  113.     //sendEvent
  114.     if (varCommand == "off"){
  115.         sendEvent(name: "switch", value: "off")
  116.         log.debug "Executing OFF"
  117.     } else {
  118.         sendEvent(name: "switch", value: "on")
  119.         log.debug "Executing ON"
  120.     }
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement