erocm123

ZigBee Gang 2 Switch

Jan 4th, 2019
231
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  4. * in compliance with the License. You may obtain a copy of the License at:
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  9. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  10. * for the specific language governing permissions and limitations under the License.
  11. *
  12. * Based on on original by Lazcad / RaveTam
  13. * 01/2017 corrected the temperature reading
  14. * 02/2017 added heartbeat to monitor connectivity health of outlet
  15. * 02/2017 added multiattribute tile
  16. * 03/2018 Fixed status updates comming from device
  17. */
  18.  
  19. metadata {
  20. definition (name: "ZigBee Gang 2 Switch", namespace: "smartthings", author: "smartthings") {
  21. capability "Actuator"
  22. capability "Configuration"
  23. capability "Refresh"
  24. capability "Switch"
  25. attribute "lastCheckin", "string"
  26. attribute "switch1", "string"
  27. attribute "switch2", "string"
  28. command "on"
  29. command "off"
  30. command "on1"
  31. command "off1"
  32. command "on2"
  33. command "off2"
  34. }
  35.  
  36. tiles(scale: 2) {
  37. multiAttributeTile(name:"switch", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  38. tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
  39. attributeState "on", label:'${name}', action:"off", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  40. attributeState "off", label:'${name}', action:"on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  41. attributeState "turningOn", label:'${name}', action:"off", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  42. attributeState "turningOff", label:'${name}', action:"on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  43. }
  44. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  45. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  46. }
  47. }
  48.  
  49. multiAttributeTile(name:"switch1", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  50. tileAttribute ("device.switch1", key: "PRIMARY_CONTROL") {
  51. attributeState "on", label:'SW1', action:"off1", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  52. attributeState "off", label:'SW1', action:"on1", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  53. attributeState "turningOn", label:'SW1', action:"off1", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  54. attributeState "turningOff", label:'SW1', action:"on1", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  55. }
  56. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  57. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  58. }
  59. }
  60.  
  61. multiAttributeTile(name:"switch2", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  62. tileAttribute ("device.switch2", key: "PRIMARY_CONTROL") {
  63. attributeState "on", label:'SW2', action:"off2", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  64. attributeState "off", label:'SW2', action:"on2", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  65. attributeState "turningOn", label:'SW2', action:"off2", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  66. attributeState "turningOff", label:'SW2', action:"on2", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  67. }
  68. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  69. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  70. }
  71. }
  72.  
  73. standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
  74. state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
  75. }
  76. main(["switch1","switch2"])
  77. details(["switch1","switch2", "refresh"])
  78. }
  79. }
  80.  
  81. // Parse incoming device messages to generate events
  82.  
  83. def parse(String description) {
  84. log.debug "Parsing '${description}'"
  85.  
  86. def value = zigbee.parse(description)?.text
  87. log.debug "Parse: $value"
  88. Map map = [:]
  89.  
  90. if (description?.startsWith('catchall:')) {
  91. map = parseCatchAllMessage(description)
  92. }
  93. else if (description?.startsWith('read attr -')) {
  94. map = parseReportAttributeMessage(description)
  95. }
  96. else if (description?.startsWith('on/off: ')){
  97. log.debug "onoff"
  98. def refreshCmds = zigbee.readAttribute(0x0006, 0x0000, [destEndpoint: 0x10]) +
  99. zigbee.readAttribute(0x0006, 0x0000, [destEndpoint: 0x11])
  100. return refreshCmds.collect {
  101. new physicalgraph.device.HubAction(it)
  102. }
  103. }
  104.  
  105. log.debug "Parse returned $map"
  106. // send event for heartbeat
  107. def now = new Date()
  108.  
  109. sendEvent(name: "lastCheckin", value: now)
  110.  
  111. def results = map ? createEvent(map) : null
  112. return results;
  113. }
  114.  
  115. private Map parseCatchAllMessage(String description) {
  116. Map resultMap = [:]
  117. def cluster = zigbee.parse(description)
  118. log.debug cluster
  119.  
  120. if (cluster.clusterId == 0x0006 && cluster.command == 0x01){
  121. if (cluster.sourceEndpoint == 0x10) {
  122. log.debug "Its Switch one"
  123. def onoff = cluster.data[-1]
  124. if (onoff == 1) {
  125. resultMap = createEvent(name: "switch1", value: "on")
  126. }
  127. else if (onoff == 0) {
  128. resultMap = createEvent(name: "switch1", value: "off")
  129. }
  130. }
  131. else if (cluster.sourceEndpoint == 0x11) {
  132. log.debug "Its Switch two"
  133. def onoff = cluster.data[-1]
  134. if (onoff == 1) {
  135. resultMap = createEvent(name: "switch2", value: "on")
  136. }
  137. else if (onoff == 0) {
  138. resultMap = createEvent(name: "switch2", value: "off")
  139. }
  140. }
  141. }
  142. return resultMap
  143. }
  144.  
  145. private Map parseReportAttributeMessage(String description) {
  146. Map descMap = (description - "read attr - ").split(",").inject([:]) {
  147. map, param ->
  148. def nameAndValue = param.split(":")
  149. map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
  150. }
  151.  
  152. Map resultMap = [:]
  153.  
  154. if (descMap.cluster == "0001" && descMap.attrId == "0020") {
  155. resultMap = getBatteryResult(convertHexToInt(descMap.value / 2))
  156. }
  157.  
  158. else if (descMap.cluster == "0008" && descMap.attrId == "0000") {
  159. resultMap = createEvent(name: "switch", value: "off")
  160. }
  161. return resultMap
  162. }
  163.  
  164. def off() {
  165. log.debug "off()"
  166. sendEvent(name: "switch", value: "off")
  167. "st cmd 0x${device.deviceNetworkId} 0xFF 0x0006 0x0 {}"
  168.  
  169. }
  170.  
  171. def on() {
  172. log.debug "on()"
  173. sendEvent(name: "switch", value: "on")
  174. "st cmd 0x${device.deviceNetworkId} 0xFF 0x0006 0x1 {}"
  175.  
  176. }
  177.  
  178. def off1() {
  179. log.debug "off1()"
  180. sendEvent(name: "switch1", value: "off")
  181. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x0 {}"
  182. }
  183.  
  184. def on1() {
  185. log.debug "on1()"
  186. sendEvent(name: "switch1", value: "on")
  187. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x1 {}"
  188. }
  189.  
  190. def off2() {
  191. log.debug "off2()"
  192. sendEvent(name: "switch2", value: "off")
  193. "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x0 {}"
  194. }
  195.  
  196. def on2() {
  197. log.debug "on2()"
  198. sendEvent(name: "switch2", value: "on")
  199. "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x1 {}"
  200. }
  201.  
  202. def refresh() {
  203. log.debug "refreshing"
  204. [
  205. "st rattr 0x${device.deviceNetworkId} 0x10 0x0006 0x0", "delay 1000",
  206. "st rattr 0x${device.deviceNetworkId} 0x11 0x0006 0x0", "delay 1000",
  207. ]
  208. }
  209.  
  210. def configure() {
  211. sendEvent(name: "checkInterval", value: 2 * 10 * 60 + 2 * 60, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
  212. log.debug "Configuring Reporting and Bindings."
  213. zigbee.onOffRefresh() + zigbee.onOffConfig()
  214. }
  215.  
  216. private Integer convertHexToInt(hex) {
  217. Integer.parseInt(hex,16)
  218. }
RAW Paste Data