Advertisement
Guest User

Untitled

a guest
Sep 24th, 2019
419
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * HTTP Momentary Switch
  3. *
  4. * Copyright 2018 Daniel Ogorchock
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing permissions and limitations under the License.
  14. *
  15. * Change History:
  16. *
  17. * Date Who What
  18. * ---- --- ----
  19. * 2018-02-18 Dan Ogorchock Original Creation
  20. *
  21. *
  22. */
  23. metadata {
  24. definition (name: "HTTP Momentary Switch", namespace: "ogiewon", author: "Dan Ogorchock") {
  25. capability "Switch"
  26. capability "Momentary"
  27. }
  28.  
  29. preferences {
  30. input(name: "deviceIP", type: "string", title:"Device IP Address", description: "Enter IP Address of your HTTP server", required: true, displayDuringSetup: true)
  31. input(name: "devicePort", type: "string", title:"Device Port", description: "Enter Port of your HTTP server (defaults to 80)", defaultValue: "80", required: false, displayDuringSetup: true)
  32. input(name: "devicePath", type: "string", title:"URL Path", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
  33. input(name: "deviceMethod", type: "enum", title: "POST, GET, or PUT", options: ["POST","GET","PUT"], defaultValue: "POST", required: true, displayDuringSetup: true)
  34. }
  35. }
  36.  
  37. def parse(String description) {
  38. log.debug(description)
  39. }
  40.  
  41. def push() {
  42. //toggle the switch to generate events for anything that is subscribed
  43. sendEvent(name: "switch", value: "on", isStateChange: true)
  44. runIn(1, toggleOff)
  45. //sendEvent(name: "switch", value: "off", isStateChange: true)
  46. runCmd(devicePath, deviceMethod)
  47. }
  48.  
  49. def toggleOff() {
  50. sendEvent(name: "switch", value: "off", isStateChange: true)
  51. }
  52.  
  53. def on() {
  54. push()
  55. }
  56.  
  57. def off() {
  58. push()
  59. }
  60.  
  61. def runCmd(String varCommand, String method) {
  62. def localDevicePort = (devicePort==null) ? "80" : devicePort
  63. def path = varCommand
  64.  
  65.  
  66. def headers = [:]
  67. headers.put("HOST", "${deviceIP}:${localDevicePort}")
  68.  
  69. //HARD CODED HEADERS
  70. headers.put("API-Key", "hinotori")
  71. headers.put("Content-Type", "application/json")
  72.  
  73.  
  74. try {
  75. def hubAction = new hubitat.device.HubAction(
  76. method: method,
  77. path: path,
  78.  
  79. //HARD CODED BODY
  80. body: '{"brightness": 36, "state": "ON"}',
  81. headers: headers
  82. )
  83. log.debug hubAction
  84. return hubAction
  85. }
  86. catch (Exception e) {
  87. log.debug "runCmd hit exception ${e} on ${hubAction}"
  88. }
  89. }
Advertisement
RAW Paste Data Copied
Advertisement