If you look at a Virtual Switch template file, you will see the bare minimum a Device Handler needs in order for the code to run. Please realize that a Virtual Switch is not actually connected to a physical device, which dramatically simplifies the code.
For a simple switch device, you need to declare some metadata about the device, including its Capabilities, in this case, a “Switch”. Device capabilities allow SmartApps to interrogate each device to determine what data is available (e.g. current value of a switch) and what functions can be called (e.g. on() and off() for a switch device.)
Take a look at the documentation for Device Capabilities as it will help to make things clearer. http://docs.smartthings.com/en/latest/capabilities-reference.html
You also need a Tile definition as well as implementation of the functions required for the Capabilities you define at the top.
Here is one of my very old Virtual Switch Device Handlers as very simple example.
Typically, you would fill in actual device communications (i.e. Z-Wave, Zigbee, or LAN) in the “on()” and “off()” functions. Also, the “parse()” routine would handle and data coming from the physical device and would update the device’s tile(s) accordingly. These tiles have intrinsic Attributes (like a current value for the switch, either “on” or “off”) that SmartApps can subscribe to and be notified of when changes occur. Custom Attributes are also possible, but those essentially need custom SmartApps to use them. When possible, stick with using the standard attributes.
/**
* Virtual Switch Device Type - VirtualSwitch.device.groovy
*
* Copyright 2014 Daniel Ogorchock
*
* 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.
*
* Change History:
*
* Date Who What
* ---- --- ----
* 2015-03-27 Dan Ogorchock Original Creation
*
*
*/
metadata {
definition (name: "Virtual Switch", namespace: "ogiewon", author: "Daniel Ogorchock") {
capability "Actuator"
capability "Switch"
}
simulator {
// TODO: define status and reply messages here
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
}
main(["switch"])
details(["switch"])
}
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
// TODO: handle 'contact' attribute
}
// handle commands
def on() {
log.debug "Virtual Switch - On"
sendEvent (name: "switch", value: "on", isStateChange: true)
}
def off() {
log.debug "Virtual Switch - Off"
sendEvent (name: "switch", value: "off", isStateChange: true)
}
Hope this helps get you started!
Dan