4 * Copyright 2015 obycode
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import groovy.json.JsonSlurper
20 definition (name: "BeaconThing", namespace: "com.obycode", author: "obycode") {
22 capability "Presence Sensor"
25 attribute "inRange", "json_object"
26 attribute "inRangeFriendly", "string"
28 command "setPresence", ["string"]
29 command "arrived", ["string"]
30 command "left", ["string"]
34 status "present": "presence: 1"
35 status "not present": "presence: 0"
39 standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true) {
40 state("present", labelIcon:"st.presence.tile.present", backgroundColor:"#53a7c0")
41 state("not present", labelIcon:"st.presence.tile.not-present", backgroundColor:"#ffffff")
43 valueTile("inRange", "device.inRangeFriendly", inactiveLabel: true, height:1, width:3, decoration: "flat") {
44 state "default", label:'${currentValue}', backgroundColor:"#ffffff"
47 details (["presence","inRange"])
51 // parse events into attributes
52 def parse(String description) {
53 log.debug "Parsing '${description}'"
57 sendEvent(name: "presence", value: "not present")
59 def json = new groovy.json.JsonBuilder(emptyList)
60 sendEvent(name:"inRange", value:json.toString())
63 def setPresence(status) {
64 log.debug "Status is $status"
65 sendEvent(name:"presence", value:status)
69 log.debug "$id has arrived"
70 def theList = device.latestValue("inRange")
71 def inRangeList = new JsonSlurper().parseText(theList)
72 if (inRangeList.contains(id)) {
76 def json = new groovy.json.JsonBuilder(inRangeList)
77 log.debug "Now in range: ${json.toString()}"
78 sendEvent(name:"inRange", value:json.toString())
80 // Generate human friendly string for tile
81 def friendlyList = "Nearby: " + inRangeList.join(", ")
82 sendEvent(name:"inRangeFriendly", value:friendlyList)
84 if (inRangeList.size() == 1) {
85 setPresence("present")
90 log.debug "$id has left"
91 def theList = device.latestValue("inRange")
92 def inRangeList = new JsonSlurper().parseText(theList)
94 def json = new groovy.json.JsonBuilder(inRangeList)
95 log.debug "Now in range: ${json.toString()}"
96 sendEvent(name:"inRange", value:json.toString())
98 // Generate human friendly string for tile
99 def friendlyList = "Nearby: " + inRangeList.join(", ")
101 if (inRangeList.empty) {
102 setPresence("not present")
103 friendlyList = "No one is nearby"
106 sendEvent(name:"inRangeFriendly", value:friendlyList)