Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.96 KB | None | 0 0
  1. /**
  2.  *  Tasmota Garage Door Opener
  3.  *
  4.  *  Copyright 2014 SmartThings
  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.  */
  16. metadata {
  17.     definition (name: "Garage Door Opener", namespace: "whoace", author: "whoace") {
  18.         capability "Switch"
  19.         capability "Refresh"
  20.     }
  21.     tiles {
  22.         standardTile("switch", "device.switch", width: 2, height: 2) {
  23.             state("unknown", label:'${name}', action:"refresh.refresh", icon:"st.doors.garage.garage-open", backgroundColor:"#ffffff")
  24.             state("closed", label:'${name}', action:"switch.on", icon:"st.doors.garage.garage-closed", backgroundColor:"#00a0dc")
  25.             state("open", label:'${name}', action:"switch.off", icon:"st.doors.garage.garage-open", backgroundColor:"#e86d13")
  26.             state("opening", label:'${name}', icon:"st.doors.garage.garage-opening", backgroundColor:"#e86d13")
  27.             state("closing", label:'${name}', icon:"st.doors.garage.garage-closing", backgroundColor:"#00a0dc")
  28.  
  29.         }
  30.         standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
  31.             state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
  32.         }
  33.  
  34.         main "switch"
  35.         details(["switch", "refresh"])
  36.     }
  37. }
  38. preferences {
  39.     input(name: "ipAddress", type: "string", title: "IP Address", description: "IP Address of Sonoff", displayDuringSetup: true, required: true)
  40.     section("Authentication") {
  41.         input(name: "username", type: "string", title: "Username", description: "Username", displayDuringSetup: false, required: false)
  42.         input(name: "password", type: "password", title: "Password (Sent in plaintext)", description: "Caution: Password is sent in plaintext", displayDuringSetup: false, required: false)
  43.     }
  44. }
  45.  
  46. def installed(){
  47.     updated()
  48. }
  49.  
  50. def updated(){
  51.     refresh()
  52.     unschedule()
  53.     runEvery5Minutes(refresh)
  54. }
  55.  
  56. //  ----- BASIC PLUG COMMANDS ------------------------------------
  57. def on() {
  58.     sendEvent(name: "switch", value: "opening")
  59.     pressSwitch()
  60. }
  61.  
  62. def off() {
  63.     sendEvent(name: "switch", value: "closing")
  64.     pressSwitch()
  65. }
  66.  
  67. //  ----- REFRESH ------------------------------------------------
  68. def refresh(){
  69.     sendToDevice("Status", "0");
  70. }
  71.  
  72. //  ----- SEND COMMAND DATA TO THE SERVER -------------------------------------
  73. private sendToDevice(String command, value){
  74.     def ipAddress = ipAddress ?: settings?.ipAddress ?: device.latestValue("ipAddress");
  75.     def username = username ?: settings?.username ?: device.latestValue("username");
  76.     def password = password ?: settings?.password ?: device.latestValue("password");
  77.  
  78.     if (!ipAddress) {
  79.         log.warn "aborting. ip address of device not set"
  80.         return null;
  81.     }
  82.  
  83.     def path = "/cm"
  84.     if (value){
  85.         path += "?cmnd=${command}%20${value}"
  86.     }
  87.     else{
  88.         path += "?cmnd=${command}"
  89.     }
  90.    
  91.     if (username){
  92.         path += "&user=${username}"
  93.         if (password){
  94.             path += "&password=${password}"
  95.         }
  96.     }
  97.     def params = [
  98.         uri:  "http://${ipAddress}${path}"
  99.     ]
  100.     log.debug params
  101.     try {
  102.         httpGet(params) {resp ->
  103.             if (command == "Status") {
  104.                 if (resp.data.StatusSNS.Switch2 == "OFF") {
  105.                     sendEvent(name: "switch", value: "open")
  106.                 } else if (resp.data.StatusSNS.Switch2 == "ON") {
  107.                     sendEvent(name: "switch", value: "closed")
  108.                 }
  109.             }
  110.         }
  111.     } catch (e) {
  112.         log.error "error: $e"
  113.     }
  114. }
  115.  
  116. def pressSwitch() {
  117.     sendToDevice("Power", "On")
  118.     runIn(2, powerOff)
  119.     runIn(15, refresh)
  120. }
  121.  
  122. def powerOff() {
  123.     sendToDevice("Power", "Off")
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement