forked from copy-ninja/SmartThings_MyQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyQ.GarageDoorOpener.SmartDevice.groovy
144 lines (128 loc) · 5.74 KB
/
MyQ.GarageDoorOpener.SmartDevice.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* MyQ Garage Door Opener
*
* Copyright 2015 Jason Mok
*
* 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.
*
* Last Updated : 7/15/2015
*
*/
metadata {
definition (name: "MyQ Garage Door Opener", namespace: "copy-ninja", author: "Jason Mok") {
capability "Garage Door Control"
capability "Door Control"
capability "Contact Sensor"
capability "Refresh"
capability "Polling"
capability "Actuator"
capability "Switch"
capability "Momentary"
capability "Sensor"
attribute "lastActivity", "string"
command "updateDeviceStatus", ["string"]
command "updateDeviceLastActivity", ["number"]
}
simulator { }
tiles {
standardTile("door", "device.door", width: 2, height: 2) {
state("unknown", label:'${name}', action:"door control.close", icon:"st.doors.garage.garage-open", backgroundColor:"#ffa81e", nextState: "closing")
state("closed", label:'${name}', action:"door control.open", icon:"st.doors.garage.garage-closed", backgroundColor:"#79b821", nextState: "opening")
state("open", label:'${name}', action:"door control.close", icon:"st.doors.garage.garage-open", backgroundColor:"#ffa81e", nextState: "closing")
state("opening", label:'${name}', icon:"st.doors.garage.garage-opening", backgroundColor:"#ffe71e", nextState: "open")
state("closing", label:'${name}', icon:"st.doors.garage.garage-closing", backgroundColor:"#ffe71e", nextState: "closed")
state("stopped", label:'stopped', action:"door control.close", icon:"st.doors.garage.garage-opening", backgroundColor:"#1ee3ff", nextState: "closing")
}
standardTile("refresh", "device.door", inactiveLabel: false, decoration: "flat") {
state("default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh")
}
standardTile("contact", "device.contact") {
state("open", label:'${name}', icon:"st.contact.contact.open", backgroundColor:"#ffa81e")
state("closed", label:'${name}', icon:"st.contact.contact.closed", backgroundColor:"#79b821")
}
standardTile("button", "device.switch") {
state("on", label:'${name}', icon:"st.contact.contact.open", backgroundColor:"#ffa81e")
state("off", label:'${name}', icon:"st.contact.contact.closed", backgroundColor:"#79b821")
}
valueTile("lastActivity", "device.lastActivity", inactiveLabel: false, decoration: "flat") {
state "default", label:'Last activity: ${currentValue}', action:"refresh.refresh", backgroundColor:"#ffffff"
}
main "door"
details(["door", "lastActivity", "refresh"])
}
}
def parse(String description) {}
def on() {
push()
sendEvent(name: "button", value: "on", isStateChange: true, display: false, displayed: false)
}
def off() {
sendEvent(name: "button", value: "off", isStateChange: true, display: false, displayed: false)
}
def push() {
def doorState = device.currentState("door")?.value
if (doorState == "open" || doorState == "stopped") {
close()
} else if (doorState == "closed") {
open()
}
sendEvent(name: "momentary", value: "pushed", display: false, displayed: false)
}
def open() {
parent.sendCommand(this, "desireddoorstate", 1)
updateDeviceStatus(4)
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}
def close() {
parent.sendCommand(this, "desireddoorstate", 0)
updateDeviceStatus(5)
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}
def refresh() {
parent.refresh()
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}
def poll() { refresh() }
// update status
def updateDeviceStatus(status) {
def currentState = device.currentState("door")?.value
if (status == "1" || status == "9") {
sendEvent(name: "door", value: "open", display: true, descriptionText: device.displayName + " is open")
sendEvent(name: "contact", value: "open", display: false, displayed: false)
}
if (status == "2") {
sendEvent(name: "door", value: "closed", display: true, descriptionText: device.displayName + " is closed")
sendEvent(name: "contact", value: "closed", display: false, displayed: false)
}
if (status == "3") {
sendEvent(name: "door", value: "stopped", display: true, descriptionText: device.displayName + " has stopped")
sendEvent(name: "contact", value: "closed", display: false, displayed: false)
}
if (status == "4" || (status=="8" && currentState=="closed")) {
sendEvent(name: "door", value: "opening", display: false, displayed: false)
}
if (status == "5" || (status=="8" && currentState=="open")) {
sendEvent(name: "door", value: "closing", display: false, displayed: false)
}
}
def updateDeviceLastActivity(long lastActivity) {
def lastActivityValue = ""
def diffTotal = now() - lastActivity
def diffDays = (diffTotal / 86400000) as long
def diffHours = (diffTotal % 86400000 / 3600000) as long
def diffMins = (diffTotal % 86400000 % 3600000 / 60000) as long
if (diffDays == 1) lastActivityValue += "${diffDays} Day "
else if (diffDays > 1) lastActivityValue += "${diffDays} Days "
if (diffHours == 1) lastActivityValue += "${diffHours} Hour "
else if (diffHours > 1) lastActivityValue += "${diffHours} Hours "
if (diffMins == 1 || diffMins == 0 ) lastActivityValue += "${diffMins} Min"
else if (diffMins > 1) lastActivityValue += "${diffMins} Mins"
sendEvent(name: "lastActivity", value: lastActivityValue, display: false , displayed: false)
}