Skip to content

Instantly share code, notes, and snippets.

@rappleg
Created March 6, 2014 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rappleg/9394764 to your computer and use it in GitHub Desktop.
Save rappleg/9394764 to your computer and use it in GitHub Desktop.
metadata {
// simulator metadata
simulator {
// status messages
status "on": "on/off: 1"
status "off": "on/off: 0"
// reply messages
reply "zcl on-off on": "on/off: 1"
reply "zcl on-off off": "on/off: 0"
}
// UI tile definitions
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
}
main "switch"
details "switch"
}
}
// Parse incoming device messages to generate events
def parse(String description) {
log.debug "Parse description $description"
def map = [:]
if (description?.startsWith("read attr -")) {
def descMap = parseDescriptionAsMap(description)
log.debug "Desc Map: $descMap"
if (descMap.cluster == "0006" && descMap.attrId == "0000") {
map.name = "switch"
map.value = descMap.value.endsWith("01") ? "on" : "off"
} else if (descMap.cluster == "0702" && descMap.attrId == "0000") {
//def encoding = description.split(",").find {it.split(":")[0].trim() == "encoding"}?.split(":")[1].trim()
def reportValue = description.split(",").find {it.split(":")[0].trim() == "value"}?.split(":")[1].trim()
map.name = "cumulative"
// assume 24 bit signed for encoding and power divisor is 1000 for watts
map.value = Integer.parseInt(reportValue, 16) / 1000
} else if (descMap.cluster == "0702" && descMap.attrId == "0400") {
//def encoding = description.split(",").find {it.split(":")[0].trim() == "encoding"}?.split(":")[1].trim()
def reportValue = description.split(",").find {it.split(":")[0].trim() == "value"}?.split(":")[1].trim()
map.name = "instantaneous"
// assume 24 bit signed for encoding and power divisor is 1000 for watts
map.value = Integer.parseInt(reportValue, 16) / 1000
}
} else {
map.name = description?.startsWith("on/off: ") ? "switch" : null
map.value = name == "switch" ? (description?.endsWith(" 1") ? "on" : "off") : null
}
def result = null
if (map) {
result = createEvent(map)
}
log.debug "Parse returned ${result?.descriptionText}"
return result
}
def parseDescriptionAsMap(description) {
(description - "read attr - ").split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
// Commands to device
def on() {
'zcl on-off on'
}
def off() {
'zcl on-off off'
}
def instantaneous() {
"st rattr 0x${device.deviceNetworkId} 2 0x702 0x400"
}
def cumulative() {
"st rattr 0x${device.deviceNetworkId} 2 0x702 0"
}
def refresh() {
[
instantaneous(), "delay 200",
cumulative()
]
}
def configure() {
// [
// "zdo bind 0x${device.deviceNetworkId} 1 1 8 {${device.zigbeeId}} {}", "delay 200",
// "zdo bind 0x${device.deviceNetworkId} 1 1 6 {${device.zigbeeId}} {}", "delay 200",
// "zdo bind 0x${device.deviceNetworkId} 1 1 0xB04 {${device.zigbeeId}} {}"
// ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment