Advertisement
Guest User

Untitled

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