Skip to content

Instantly share code, notes, and snippets.

@aurman
Last active May 27, 2018 07:13
Show Gist options
  • Save aurman/6862503 to your computer and use it in GitHub Desktop.
Save aurman/6862503 to your computer and use it in GitHub Desktop.
Device Handler Code for On/Off Shield (example)
/*
* On/Off Shield (example)
*
* Author: urman
* Date: 2013-03-14
* Revision: 2014-07-10
* Capabilities:
* Switch
* Custom Attributes:
* greeting
* Custom Commands
* hello
* goodbye
*/
metadata {
definition (name: "On/Off Shield (example)", author: "urman") {
capability "Switch"
command "hello"
command "goodbye"
attribute "greeting","string"
}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: 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"
}
standardTile("greeting", "device.greeting", width: 1, height: 1, canChangeIcon: true, canChangeBackground: true) {
state "default", label: 'hello', action: "hello", icon: "st.switches.switch.off", backgroundColor: "#ccccff"
}
valueTile("message", "device.greeting", inactiveLabel: false) {
state "greeting", label:'${currentValue}', unit:""
}
main "switch"
details(["switch","greeting","message"])
}
simulator {
status "on": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
status "off": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
// reply messages
reply "raw 0x0 { 00 00 0a 0a 6f 6e }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
reply "raw 0x0 { 00 00 0a 0a 6f 66 66 }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
}
}
Map parse(String description) {
def value = zigbee.parse(description)?.text
def linkText = getLinkText(device)
def descriptionText = getDescriptionText(description, linkText, value)
def handlerName = value
def isStateChange = value != "ping"
def displayed = value && isStateChange
def result = [
value: value,
name: value in ["on","off"] ? "switch" : (value && value != "ping" ? "greeting" : null),
handlerName: handlerName,
linkText: linkText,
descriptionText: descriptionText,
isStateChange: isStateChange,
displayed: displayed
]
log.debug result.descriptionText
result
}
def on() {
zigbee.smartShield(text: "on").format()
}
def off() {
zigbee.smartShield(text: "off").format()
}
def hello() {
log.debug "Hello World!"
zigbee.smartShield(text: "hello").format()
}
def goodbye() {
log.debug "Bye Bye!"
zigbee.smartShield(text: "goodbye").format()
}
def parse(String description) {
log.debug "Parsing '${description}'"
// TODO: handle 'switch' attribute
// TODO: handle 'greeting' attribute
}
@mager
Copy link

mager commented Oct 18, 2014

I had to comment out line 22 because it was giving an error in the simulator.

@AndrewArace
Copy link

Is this still correct/applicable?
This code is the ONLY code i've seen for all the other device types that uses the Map parse(String description) code.
I haven't seen this code anywhere else in any of the sample event types, and, in fact, the "On/Off Shield (Example)" isn't even listed any more.

@uchobby
Copy link

uchobby commented Oct 17, 2015

I just got everything setup and the on/off button is not working. The hello button works fine. Using a new hub, a new Arduino Shield, the latest lib, and the on/off example from the web site, unmodified.

@samywee
Copy link

samywee commented Jul 4, 2016

This code do not show text coming from Arduino. However, there is a built in device type with this name which show that on the time. Don't know how to get that done. Ex. "buttonhello" example has a line item "smartthing.send("fancy");" with which can show "fancy" in a time. Don't know how to get that.

To fix change line 33 from
valueTile("message", "device.greeting", inactiveLabel: false) {

to
valueTile("message", "device.message", inactiveLabel: false) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment