forked from KristopherKubicki/device-improved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device-mobile-presence.groovy
95 lines (81 loc) · 2.47 KB
/
device-mobile-presence.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
metadata {
// Automatically generated. Make future change here.
definition (name: "Improved Mobile Presence", namespace: "smartthings", author: "SmartThings") {
capability "Presence Sensor"
capability "Sensor"
capability "Refresh"
command "present"
command "away"
}
simulator {
status "present": "presence: 1"
status "not present": "presence: 0"
}
tiles {
standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true) {
state("present", labelIcon:"st.presence.tile.mobile-present", backgroundColor:"#53a7c0")
state("not present", labelIcon:"st.presence.tile.mobile-not-present", backgroundColor:"#ffffff")
}
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat") {
state "default", action:"refresh.refresh", icon:"st.secondary.refresh"
}
main "presence"
details(["presence", "refresh"])
}
}
def refresh() {
away()
}
def away() {
sendEvent(name: 'presence', value: "not present", descriptionText: "$device.displayName set to away")
}
def present() {
sendEvent(name: 'presence', value: "present", descriptionText: "$device.displayName set to present")
}
def parse(String description) {
def name = parseName(description)
def value = parseValue(description)
def linkText = getLinkText(device)
def descriptionText = parseDescriptionText(linkText, value, description)
def handlerName = getState(value)
def isStateChange = isStateChange(device, name, value)
def results = [
name: name,
value: value,
unit: null,
linkText: linkText,
descriptionText: descriptionText,
handlerName: handlerName,
isStateChange: isStateChange,
displayed: displayed(description, isStateChange)
]
log.debug "Parse returned $results.descriptionText"
return results
}
private String parseName(String description) {
if (description?.startsWith("presence: ")) {
return "presence"
}
null
}
private String parseValue(String description) {
switch(description) {
case "presence: 1": return "present"
case "presence: 0": return "not present"
default: return description
}
}
private parseDescriptionText(String linkText, String value, String description) {
switch(value) {
case "present": return "$linkText has arrived"
case "not present": return "$linkText has left"
default: return value
}
}
private getState(String value) {
switch(value) {
case "present": return "arrived"
case "not present": return "left"
default: return value
}
}