Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tasmota Garage Door Opener
- *
- * Copyright 2014 SmartThings
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
- * for the specific language governing permissions and limitations under the License.
- *
- */
- metadata {
- definition (name: "Garage Door Opener", namespace: "whoace", author: "whoace") {
- capability "Switch"
- capability "Refresh"
- }
- tiles {
- standardTile("switch", "device.switch", width: 2, height: 2) {
- state("unknown", label:'${name}', action:"refresh.refresh", icon:"st.doors.garage.garage-open", backgroundColor:"#ffffff")
- state("closed", label:'${name}', action:"switch.on", icon:"st.doors.garage.garage-closed", backgroundColor:"#00a0dc")
- state("open", label:'${name}', action:"switch.off", icon:"st.doors.garage.garage-open", backgroundColor:"#e86d13")
- state("opening", label:'${name}', icon:"st.doors.garage.garage-opening", backgroundColor:"#e86d13")
- state("closing", label:'${name}', icon:"st.doors.garage.garage-closing", backgroundColor:"#00a0dc")
- }
- standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
- state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
- }
- main "switch"
- details(["switch", "refresh"])
- }
- }
- preferences {
- input(name: "ipAddress", type: "string", title: "IP Address", description: "IP Address of Sonoff", displayDuringSetup: true, required: true)
- section("Authentication") {
- input(name: "username", type: "string", title: "Username", description: "Username", displayDuringSetup: false, required: false)
- input(name: "password", type: "password", title: "Password (Sent in plaintext)", description: "Caution: Password is sent in plaintext", displayDuringSetup: false, required: false)
- }
- }
- def installed(){
- updated()
- }
- def updated(){
- refresh()
- unschedule()
- runEvery5Minutes(refresh)
- }
- // ----- BASIC PLUG COMMANDS ------------------------------------
- def on() {
- sendEvent(name: "switch", value: "opening")
- pressSwitch()
- }
- def off() {
- sendEvent(name: "switch", value: "closing")
- pressSwitch()
- }
- // ----- REFRESH ------------------------------------------------
- def refresh(){
- sendToDevice("Status", "0");
- }
- // ----- SEND COMMAND DATA TO THE SERVER -------------------------------------
- private sendToDevice(String command, value){
- def ipAddress = ipAddress ?: settings?.ipAddress ?: device.latestValue("ipAddress");
- def username = username ?: settings?.username ?: device.latestValue("username");
- def password = password ?: settings?.password ?: device.latestValue("password");
- if (!ipAddress) {
- log.warn "aborting. ip address of device not set"
- return null;
- }
- def path = "/cm"
- if (value){
- path += "?cmnd=${command}%20${value}"
- }
- else{
- path += "?cmnd=${command}"
- }
- if (username){
- path += "&user=${username}"
- if (password){
- path += "&password=${password}"
- }
- }
- def params = [
- uri: "http://${ipAddress}${path}"
- ]
- log.debug params
- try {
- httpGet(params) {resp ->
- if (command == "Status") {
- if (resp.data.StatusSNS.Switch2 == "OFF") {
- sendEvent(name: "switch", value: "open")
- } else if (resp.data.StatusSNS.Switch2 == "ON") {
- sendEvent(name: "switch", value: "closed")
- }
- }
- }
- } catch (e) {
- log.error "error: $e"
- }
- }
- def pressSwitch() {
- sendToDevice("Power", "On")
- runIn(2, powerOff)
- runIn(15, refresh)
- }
- def powerOff() {
- sendToDevice("Power", "Off")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement