Advertisement
xetrok

2 Gang DH

Jul 18th, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  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.  
  26.  
  27. capability "Temperature Measurement"
  28.  
  29. attribute "lastCheckin", "string"
  30. attribute "switch", "string"
  31. attribute "switch1", "string"
  32. attribute "switch2", "string"
  33. command "on0"
  34. command "off0"
  35. command "on"
  36. command "off"
  37. command "on2"
  38. command "off2"
  39. }
  40.  
  41. // simulator metadata
  42. simulator {
  43. // status messages
  44. status "on": "on/off: 1"
  45. status "off": "on/off: 0"
  46.  
  47. // reply messages
  48. reply "zcl on-off on": "on/off: 1"
  49. reply "zcl on-off off": "on/off: 0"
  50.  
  51.  
  52. }
  53.  
  54. tiles(scale: 2) {
  55. multiAttributeTile(name:"switch", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  56. tileAttribute ("device.switch1", key: "PRIMARY_CONTROL") {
  57. attributeState "on", label:'switch', action:"off0", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  58. attributeState "off", label:'switch', action:"on0", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  59. attributeState "turningOn", label:'switch', action:"off0", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  60. attributeState "turningOff", label:'switch', action:"on0", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  61. }
  62. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  63. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  64. }
  65. }
  66. multiAttributeTile(name:"switch1", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  67. tileAttribute ("device.switch1", key: "PRIMARY_CONTROL") {
  68. attributeState "on", label:'switch1', action:"off", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  69. attributeState "off", label:'switch1', action:"on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  70. attributeState "turningOn", label:'switch1', action:"off", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  71. attributeState "turningOff", label:'switch1', action:"on", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  72. }
  73. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  74. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  75. }
  76. }
  77.  
  78.  
  79. multiAttributeTile(name:"switch2", type: "device.switch", width: 6, height: 4, canChangeIcon: true){
  80. tileAttribute ("device.switch2", key: "PRIMARY_CONTROL") {
  81. attributeState "on", label:'switch2', action:"off2", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  82. attributeState "off", label:'switch2', action:"on2", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  83. attributeState "turningOn", label:'switch2', action:"off2", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
  84. attributeState "turningOff", label:'switch2', action:"on2", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
  85. }
  86. tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
  87. attributeState("default", label:'Last Update: ${currentValue}',icon: "st.Health & Wellness.health9")
  88. }
  89. }
  90.  
  91.  
  92.  
  93. standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
  94. state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
  95. }
  96. main(["switch"])
  97. details(["switch1","switch2", "refresh"])
  98. }
  99. }
  100.  
  101. // Parse incoming device messages to generate events
  102.  
  103. def parse(String description) {
  104. log.debug "Parsing '${description}'"
  105.  
  106. def value = zigbee.parse(description)?.text
  107. log.debug "Parse: $value"
  108. Map map = [:]
  109.  
  110. if (description?.startsWith('catchall:')) {
  111. map = parseCatchAllMessage(description)
  112. }
  113. else if (description?.startsWith('read attr -')) {
  114. map = parseReportAttributeMessage(description)
  115. }
  116. else if (description?.startsWith('on/off: ')){
  117. log.debug "onoff"
  118. def refreshCmds = zigbee.readAttribute(0x0006, 0x0000, [destEndpoint: 0x10]) +
  119. zigbee.readAttribute(0x0006, 0x0000, [destEndpoint: 0x11])
  120.  
  121.  
  122. return refreshCmds.collect { new physicalgraph.device.HubAction(it) }
  123. //def resultMap = zigbee.getKnownDescription(description)
  124. //log.debug "${resultMap}"
  125.  
  126. //map = parseCustomMessage(description)
  127. }
  128.  
  129. log.debug "Parse returned $map"
  130. // send event for heartbeat
  131. def now = new Date()
  132.  
  133. sendEvent(name: "lastCheckin", value: now)
  134.  
  135. def results = map ? createEvent(map) : null
  136. return results;
  137. }
  138.  
  139. private Map parseCatchAllMessage(String description) {
  140. Map resultMap = [:]
  141. def cluster = zigbee.parse(description)
  142. log.debug cluster
  143.  
  144. if (cluster.clusterId == 0x0006 && cluster.command == 0x01){
  145. if (cluster.sourceEndpoint == 0x10)
  146. {
  147. log.debug "Its Switch one"
  148. def onoff = cluster.data[-1]
  149. if (onoff == 1)
  150. resultMap = createEvent(name: "switch1", value: "on")
  151. else if (onoff == 0)
  152. resultMap = createEvent(name: "switch1", value: "off")
  153. }
  154. else if (cluster.sourceEndpoint == 0x11)
  155. {
  156. log.debug "Its Switch two"
  157. def onoff = cluster.data[-1]
  158. if (onoff == 1)
  159. resultMap = createEvent(name: "switch2", value: "on")
  160. else if (onoff == 0)
  161. resultMap = createEvent(name: "switch2", value: "off")
  162. }
  163. }
  164.  
  165. return resultMap
  166. }
  167.  
  168. private Map parseReportAttributeMessage(String description) {
  169. Map descMap = (description - "read attr - ").split(",").inject([:]) { map, param ->
  170. def nameAndValue = param.split(":")
  171. map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
  172. }
  173. //log.debug "Desc Map: $descMap"
  174.  
  175. Map resultMap = [:]
  176.  
  177. if (descMap.cluster == "0001" && descMap.attrId == "0020") {
  178. resultMap = getBatteryResult(convertHexToInt(descMap.value / 2))
  179. }
  180.  
  181. else if (descMap.cluster == "0008" && descMap.attrId == "0000") {
  182. resultMap = createEvent(name: "switch", value: "off")
  183. }
  184. return resultMap
  185. }
  186.  
  187. def off() {
  188. log.debug "off()"
  189. sendEvent(name: "switch1", value: "off")
  190. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x0 {}"
  191. }
  192.  
  193. def on() {
  194. log.debug "on()"
  195. sendEvent(name: "switch1", value: "on")
  196. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x1 {}"
  197. }
  198. def off2() {
  199. log.debug "off2()"
  200. sendEvent(name: "switch2", value: "off")
  201. "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x0 {}"
  202. }
  203.  
  204. def on2() {
  205. log.debug "on2()"
  206. sendEvent(name: "switch2", value: "on")
  207. "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x1 {}"
  208. }
  209.  
  210. def off0() {
  211. log.debug "off0()"
  212. sendEvent(name: "switch", value: "off")
  213. "st cmd 0x${device.deviceNetworkId} 0xFF 0x0006 0x0 {}"
  214. // "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x0 {}"
  215. }
  216.  
  217. def on0() {
  218. log.debug "on0()"
  219. sendEvent(name: "switch", value: "on")
  220. "st cmd 0x${device.deviceNetworkId} 0xFF 0x0006 0x1 {}"
  221. // "st cmd 0x${device.deviceNetworkId} 0x11 0x0006 0x1 {}"
  222. }
  223. def refresh() {
  224. log.debug "refreshing"
  225. [
  226. "st rattr 0x${device.deviceNetworkId} 0x10 0x0006 0x0", "delay 1000",
  227. // "st rattr 0x${device.deviceNetworkId} 0x10 0x0006 0x0", "delay 250",
  228.  
  229. // "st rattr 0x${device.deviceNetworkId} 0x10 0x0001 0x0", "delay 250",
  230. // "st rattr 0x${device.deviceNetworkId} 0x10 0x0000 0x0",
  231. "st rattr 0x${device.deviceNetworkId} 0x11 0x0006 0x0", "delay 1000",
  232. // "st rattr 0x${device.deviceNetworkId} 0x11 0x0006 0x0", "delay 250",
  233.  
  234. // "st rattr 0x${device.deviceNetworkId} 0x11 0x0001 0x0", "delay 250",
  235. // "st rattr 0x${device.deviceNetworkId} 0x11 0x0000 0x0",
  236. ]
  237. }
  238.  
  239. private Map parseCustomMessage(String description) {
  240. def result
  241. if (description?.startsWith('on/off: ')) {
  242. if (description == 'on/off: 0')
  243. result = createEvent(name: "switch", value: "off")
  244. else if (description == 'on/off: 1')
  245. result = createEvent(name: "switch", value: "on")
  246. }
  247.  
  248. return result
  249. }
  250.  
  251. private Integer convertHexToInt(hex) {
  252. Integer.parseInt(hex,16)
  253. }
  254.  
  255. def off1() {
  256. log.debug "off()"
  257. sendEvent(name: "switch2", value: "off")
  258.  
  259. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x0 {}"
  260.  
  261.  
  262.  
  263. }
  264.  
  265. def on1() {
  266. log.debug "on()"
  267. sendEvent(name: "switch2", value: "on")
  268.  
  269. "st cmd 0x${device.deviceNetworkId} 0x10 0x0006 0x1 {}"
  270.  
  271.  
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement