diff --git a/devicetypes/mitchpond/iris-care-pendant.src/iris-care-pendant.groovy b/devicetypes/mitchpond/iris-care-pendant.src/iris-care-pendant.groovy new file mode 100644 index 0000000..c3422c5 --- /dev/null +++ b/devicetypes/mitchpond/iris-care-pendant.src/iris-care-pendant.groovy @@ -0,0 +1,225 @@ +/** + * Iris Care Pendant + * + * Copyright 2015 Mitch Pond + * + * 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: "Iris Care Pendant", namespace: "mitchpond", author: "Mitch Pond") { + capability "Sensor" + capability "Battery" + capability "Configuration" + capability "Button" + + command "enrollResponse" + + fingerprint endpointId: "01", inClusters: "0000,0001,0003,0020,0500", outClusters: "0003,0019", manufacturer: "CentraLite", model: "3455-L" + } + + preferences {} + + tiles(scale: 2) { + standardTile("button", "device.button", decoration: "flat", width: 2, height:2) { + state "default", icon: "st.unknown.zwave.remote-controller", backgroundColor: "#ffffff" + state "pushed", backgroundColor: "#79b821" + } + valueTile("battery", "device.battery", decoration: "flat", inactiveLabel: false, width: 2, height: 2) { + state "battery", label:'${currentValue}% battery', unit:"" + } + + main (["battery"]) + details(["button","battery"]) + } +} + +def parse(String description) { + //log.debug "description: $description" + + def results = [] + if (description?.startsWith('catchall:')) { + results = parseCatchAllMessage(description) + } + else if (description?.startsWith('read attr -')) { + results = parseReportAttributeMessage(description) + } + else if (description?.startsWith('zone status')) { + results = parseIasMessage(description) + } + + if (description?.startsWith('enroll request')) { + List cmds = enrollResponse() + log.debug "enroll response: ${cmds}" + results = cmds?.collect { new physicalgraph.device.HubAction(it) } + } + return results +} + +private Map parseCatchAllMessage(String description) { + Map resultMap = [:] + def cluster = zigbee.parse(description) + if (shouldProcessMessage(cluster)) { + switch(cluster.clusterId) { + case 0x0001: + resultMap = getBatteryResult(cluster.data.last()) + break + } + } + + return resultMap +} + +private boolean shouldProcessMessage(cluster) { + // 0x0B is default response indicating message got through + // 0x07 is bind message + boolean ignoredMessage = cluster.profileId != 0x0104 || + cluster.command == 0x0B || + cluster.command == 0x07 || + (cluster.data.size() > 0 && cluster.data.first() == 0x3e) + return !ignoredMessage +} + +private parseReportAttributeMessage(String description) { + Map descMap = (description - "read attr - ").split(",").inject([:]) { map, param -> + def nameAndValue = param.split(":") + map += [(nameAndValue[0].trim()):nameAndValue[1].trim()] + } + + def results = [] + + if (descMap.cluster == "0001" && descMap.attrId == "0020") { + log.debug "Received battery level report" + results = createEvent(getBatteryResult(Integer.parseInt(descMap.value, 16))) + } + + return results +} + +private parseIasMessage(String description) { + List parsedMsg = description.split(' ') + String msgCode = parsedMsg[2] + int status = Integer.decode(msgCode) + def linkText = getLinkText(device) + + def results = [] + //log.debug(description) + if (status & 0b00000010) {results << createEvent(getButtonResult('pushed'))} + else if (~status & 0b00000010) results << createEvent(getButtonResult('released')) + + if (status & 0b00000100) { + //tampered + } + else if (~status & 0b00000100) { + //not tampered + } + + if (status & 0b00001000) { + } + else if (~status & 0b00001000) { + //log.debug "${linkText} battery OK" + } + //log.debug results + return results +} + +private Map getBatteryResult(rawValue) { + log.debug 'Battery' + def linkText = getLinkText(device) + + def result = [ + name: 'battery' + ] + + def volts = rawValue / 10 + def descriptionText + if (volts > 3.5) { + result.descriptionText = "${linkText} battery has too much power (${volts} volts)." + } + else { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + result.value = Math.min(100, (int) pct * 100) + result.descriptionText = "${linkText} battery was ${result.value}%" + } + + return result +} + +private Map getButtonResult(value) { + //log.debug 'Button Status' + def linkText = getLinkText(device) + def descriptionText = "${linkText} was ${value == 'pushed' ? 'pushed' : 'released'}" + return [ + name: 'button', + value: value, + data: [buttonNumber: 1], + descriptionText: descriptionText, + displayed: true, + isStateChange: true + ] +} + +def configure() { + + String zigbeeEui = swapEndianHex(device.hub.zigbeeEui) + log.debug "Configuring Reporting, IAS CIE, and Bindings." + def configCmds = [ + "zcl global write 0x500 0x10 0xf0 {${zigbeeEui}}", "delay 100", + "send 0x${device.deviceNetworkId} 1 1", "delay 500", + + "zdo bind 0x${device.deviceNetworkId} ${endpointId} 1 1 {${device.zigbeeId}} {}", "delay 200", + "zcl global send-me-a-report 1 0x20 0x20 3600 21600 {01}", //checkin time 6 hrs + "send 0x${device.deviceNetworkId} 1 1", "delay 500", + "st rattr 0x${device.deviceNetworkId} 1 1 0x20" + ] + return configCmds +} + +def enrollResponse() { + log.debug "Sending enroll response" + String zigbeeEui = swapEndianHex(device.hub.zigbeeEui) + [ + //Resending the CIE in case the enroll request is sent before CIE is written + "zcl global write 0x500 0x10 0xf0 {${zigbeeEui}}", "delay 200", + "send 0x${device.deviceNetworkId} 1 ${endpointId}", "delay 500", + //Enroll Response + "raw 0x500 {01 23 00 00 00}", + "send 0x${device.deviceNetworkId} 1 1", "delay 200" + ] +} + +private getEndpointId() { + new BigInteger(device.endpointId, 16).toString() +} + +private hex(value) { + new BigInteger(Math.round(value).toString()).toString(16) +} + +private String swapEndianHex(String hex) { + reverseArray(hex.decodeHex()).encodeHex() +} + +private byte[] reverseArray(byte[] array) { + int i = 0; + int j = array.length - 1; + byte tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + return array +} \ No newline at end of file diff --git a/smartapps/mitchpond/3400-x-keypad-manager.src/3400-x-keypad-manager.groovy b/smartapps/mitchpond/3400-x-keypad-manager.src/3400-x-keypad-manager.groovy new file mode 100644 index 0000000..a6bb6ad --- /dev/null +++ b/smartapps/mitchpond/3400-x-keypad-manager.src/3400-x-keypad-manager.groovy @@ -0,0 +1,129 @@ +/** + * 3400-X Keypad Manager + * + * Copyright 2015 Mitch Pond + * + * 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. + * + */ +definition( + name: "3400-X Keypad Manager", + namespace: "mitchpond", + author: "Mitch Pond", + description: "Service manager for Centralite 3400-X security keypad. Keeps keypad state in sync with Smart Home Monitor", + category: "My Apps", + iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", + iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", + iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", + singleInstance: false) + + +preferences { + page(name: "setupPage") +} + +def setupPage() { + dynamicPage(name: "setupPage",title: "3400-X Keypad Manager", install: true, uninstall: true) { + section("Settings") { + input(name: "keypad", title: "Keypad", type: "capability.lockCodes", multiple: false, required: true) + input(name: "pin" , title: "PIN code", type: "number", range: "0000..9999", required: true) + paragraph "PIN should be four digits. Shorter PINs will be padded with leading zeroes. (42 becomes 0042)" + label(title: "Assign a name", required: false) + } + def routines = location.helloHome?.getPhrases()*.label + routines?.sort() + section("Routines", hideable: true, hidden: true) { + input(name: "armRoutine", title: "Arm/Away routine", type: "enum", options: routines, required: false) + input(name: "disarmRoutine", title: "Disarm routine", type: "enum", options: routines, required: false) + input(name: "stayRoutine", title: "Arm/Stay routine", type: "enum", options: routines, required: false) + //input(name: "nightRoutine", title: "Arm/Night routine", type: "enum", options: routines, required: false) + } + } +} + +def installed() { + log.debug "Installed with settings: ${settings}" + + initialize() +} + +def updated() { + log.debug "Updated with settings: ${settings}" + + unsubscribe() + initialize() +} + +def initialize() { + // TODO: subscribe to attributes, devices, locations, etc. + subscribe(location,"alarmSystemStatus",alarmStatusHandler) + subscribe(keypad,"codeEntered",codeEntryHandler) + + //initialize keypad to correct state + def event = [name:"alarmSystemStatus", value: location.currentState("alarmSystemStatus").value, + displayed: true, description: "System Status is ${shmState}"] + alarmStatusHandler(event) +} + +//Returns the PIN padded with zeroes to 4 digits +private String getPIN(){ + return settings.pin.value.toString().padLeft(4,'0') +} + +// TODO: implement event handlers +def alarmStatusHandler(event) { + log.debug "Keypad manager caught alarm status change: "+event.value + if (event.value == "off") keypad?.setDisarmed() + else if (event.value == "away") keypad?.setArmedAway() + else if (event.value == "stay") keypad?.setArmedStay() +} + +private sendSHMEvent(String shmState){ + def event = [name:"alarmSystemStatus", value: shmState, + displayed: true, description: "System Status is ${shmState}"] + sendLocationEvent(event) +} + +private execRoutine(armMode) { + if (armMode == 'away') location.helloHome?.execute(settings.armRoutine) + else if (armMode == 'stay') location.helloHome?.execute(settings.stayRoutine) + else if (armMode == 'off') location.helloHome?.execute(settings.disarmRoutine) +} + +def codeEntryHandler(evt){ + //do stuff + log.debug "Caught code entry event! ${evt.value.value}" + + def codeEntered = evt.value as String + def correctCode = getPIN() + def data = evt.data as String + def armMode = '' + + if (data == '0') armMode = 'off' + else if (data == '3') armMode = 'away' + else if (data == '1') armMode = 'stay' + else if (data == '2') armMode = 'stay' //Currently no separate night mode for SHM, set to 'stay' + else { + log.error "${app.label}: Unexpected arm mode sent by keypad!: "+data + return [] + } + + if (codeEntered == correctCode) { + log.debug "Correct PIN entered. Change SHM state to ${armMode}" + keypad.acknowledgeArmRequest(data) + sendSHMEvent(armMode) + execRoutine(armMode) + } + else { + log.debug "Invalid PIN" + //Could also call acknowledgeArmRequest() with a parameter of 4 to report invalid code. Opportunity to simplify code? + keypad.sendInvalidKeycodeResponse() + } +} \ No newline at end of file