Other
/**
* Copyright 2015 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: “Presence Switch”, namespace: “smartthings”, author: “SmartThings”) {
capability "Presence Sensor"
capability "Sensor"
capability “Switch”
}
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("button", "device.switch", width: 2, height: 2, canChangeIcon: false) {
state "off", label: 'Off', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
state "on", label: 'On', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#79b821", nextState: "off"
}
main "presence"
details(["button","presence"])
}
}
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
}
def on() {
sendEvent(displayed: true, isStateChange: true, name: “presence”, value: “present”, descriptionText: “$device.displayName is $v”)
}
def off() {
sendEvent(displayed: true, isStateChange: true, name: “presence”, value: “not present”, descriptionText: “$device.displayName is $v”)
}
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
}
}