/** * Copyright 2019 * * This driver includes a preference to invert/reverse the open and close states. * * 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: "Virtual Contact Sensor", namespace: "mlritchie", author: "ritchierich") { capability "Contact Sensor" capability "Sensor" command "open" command "close" } preferences { input name: "reverseStates", type: "bool", title: "Reverse States?", defaultValue: false } } def installed() { initialize() } def updated() { initialize() } private initialize() { if (reverseStates == null) { device.updateSetting("reverseStates",[value:"false",type:"bool"]) } } def parse(String description) { def pair = description.split(":") if (reverseStates == true) { def value = pair[1].trim() == "open" ? "closed" : "open" } createEvent(name: pair[0].trim(), value: value) } def open() { def value = reverseStates == false ? "open" : "closed" sendEvent(name: "contact", value: value) } def close() { def value = reverseStates == false ? "closed" : "open" sendEvent(name: "contact", value: value) }