|
| 1 | +/** |
| 2 | + * Copyright 2019 SmartThings |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at: |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
| 10 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License |
| 11 | + * for the specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | +import physicalgraph.zigbee.zcl.DataType |
| 15 | + |
| 16 | +metadata { |
| 17 | + definition (name: "Zigbee Metering Plug Power Consumption Report", namespace: "smartthings", author: "SmartThings", ocfDeviceType: "oic.d.smartplug", mnmn: "SmartThings", vid: "generic-switch-power-energy") { |
| 18 | + capability "Energy Meter" |
| 19 | + capability "Power Meter" |
| 20 | + capability "Actuator" |
| 21 | + capability "Switch" |
| 22 | + capability "Refresh" |
| 23 | + capability "Health Check" |
| 24 | + capability "Sensor" |
| 25 | + capability "Configuration" |
| 26 | + capability "Power Consumption Report" |
| 27 | + |
| 28 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-B430-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS Smart Plug |
| 29 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-B530-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS Smart Plug |
| 30 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-C140-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS In-Wall Outlet |
| 31 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-B540-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS Smart Plug |
| 32 | + fingerprint manufacturer: "DAWON_DNS", model: "ST-B550-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS Smart Plug |
| 33 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-C150-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS In-Wall Outlet |
| 34 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-C250-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS In-Wall Outlet |
| 35 | + fingerprint manufacturer: "DAWON_DNS", model: "PM-B440-ZB", deviceJoinName: "Dawon Outlet" // DAWON DNS Smart Plug |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +def getATTRIBUTE_READING_INFO_SET() { 0x0000 } |
| 40 | +def getATTRIBUTE_HISTORICAL_CONSUMPTION() { 0x0400 } |
| 41 | + |
| 42 | +def parse(String description) { |
| 43 | + log.debug "description is $description" |
| 44 | + def event = zigbee.getEvent(description) |
| 45 | + def descMap = zigbee.parseDescriptionAsMap(description) |
| 46 | + |
| 47 | + if (event) { |
| 48 | + log.info "event enter:$event" |
| 49 | + if (event.name == "switch" && !descMap.isClusterSpecific && descMap.commandInt == 0x0B) { |
| 50 | + log.info "Ignoring default response with desc map: $descMap" |
| 51 | + return [:] |
| 52 | + } else if (event.name== "power") { |
| 53 | + event.value = event.value/getPowerDiv() |
| 54 | + event.unit = "W" |
| 55 | + } else if (event.name== "energy") { |
| 56 | + event.value = event.value/getEnergyDiv() |
| 57 | + event.unit = "kWh" |
| 58 | + } |
| 59 | + log.info "event outer:$event" |
| 60 | + sendEvent(event) |
| 61 | + } else { |
| 62 | + List result = [] |
| 63 | + log.debug "Desc Map: $descMap" |
| 64 | + |
| 65 | + List attrData = [[clusterInt: descMap.clusterInt ,attrInt: descMap.attrInt, value: descMap.value]] |
| 66 | + descMap.additionalAttrs.each { |
| 67 | + attrData << [clusterInt: descMap.clusterInt, attrInt: it.attrInt, value: it.value] |
| 68 | + } |
| 69 | + |
| 70 | + attrData.each { |
| 71 | + def map = [:] |
| 72 | + if (it.value && it.clusterInt == zigbee.SIMPLE_METERING_CLUSTER && it.attrInt == ATTRIBUTE_HISTORICAL_CONSUMPTION) { |
| 73 | + log.debug "power" |
| 74 | + map.name = "power" |
| 75 | + map.value = zigbee.convertHexToInt(it.value)/getPowerDiv() |
| 76 | + map.unit = "W" |
| 77 | + } |
| 78 | + else if (it.value && it.clusterInt == zigbee.SIMPLE_METERING_CLUSTER && it.attrInt == ATTRIBUTE_READING_INFO_SET) { |
| 79 | + log.debug "energy" |
| 80 | + map.name = "energy" |
| 81 | + map.value = zigbee.convertHexToInt(it.value)/getEnergyDiv() |
| 82 | + map.unit = "kWh" |
| 83 | + |
| 84 | + def currentEnergy = map.value |
| 85 | + def currentPowerConsumption = device.currentState("powerConsumption")?.value |
| 86 | + Map previousMap = currentPowerConsumption ? new groovy.json.JsonSlurper().parseText(currentPowerConsumption) : [:] |
| 87 | + def deltaEnergy = calculateDelta (currentEnergy, previousMap) |
| 88 | + Map reportMap = [:] |
| 89 | + reportMap["energy"] = currentEnergy |
| 90 | + reportMap["deltaEnergy"] = deltaEnergy |
| 91 | + sendEvent("name": "powerConsumption", "value": reportMap.encodeAsJSON(), displayed: false) |
| 92 | + } |
| 93 | + |
| 94 | + if (map) { |
| 95 | + result << createEvent(map) |
| 96 | + } |
| 97 | + log.debug "Parse returned $map" |
| 98 | + } |
| 99 | + return result |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +def off() { |
| 104 | + def cmds = zigbee.off() |
| 105 | + return cmds |
| 106 | +} |
| 107 | + |
| 108 | +def on() { |
| 109 | + def cmds = zigbee.on() |
| 110 | + return cmds |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * PING is used by Device-Watch in attempt to reach the Device |
| 115 | + * */ |
| 116 | +def ping() { |
| 117 | + return refresh() |
| 118 | +} |
| 119 | + |
| 120 | +def refresh() { |
| 121 | + log.debug "refresh" |
| 122 | + zigbee.onOffRefresh() + |
| 123 | + zigbee.electricMeasurementPowerRefresh() + |
| 124 | + zigbee.readAttribute(zigbee.SIMPLE_METERING_CLUSTER, ATTRIBUTE_READING_INFO_SET) |
| 125 | +} |
| 126 | + |
| 127 | +def configure() { |
| 128 | + // this device will send instantaneous demand and current summation delivered every 1 minute |
| 129 | + sendEvent(name: "checkInterval", value: 2 * 60 + 10 * 60, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID]) |
| 130 | + log.debug "Configuring Reporting" |
| 131 | + return refresh() + |
| 132 | + zigbee.onOffConfig() + |
| 133 | + zigbee.configureReporting(zigbee.SIMPLE_METERING_CLUSTER, ATTRIBUTE_READING_INFO_SET, DataType.UINT48, 1, 600, 1) + |
| 134 | + zigbee.electricMeasurementPowerConfig(1, 600, 1) + |
| 135 | + zigbee.simpleMeteringPowerConfig() |
| 136 | +} |
| 137 | + |
| 138 | +private int getPowerDiv() { |
| 139 | + 1 |
| 140 | +} |
| 141 | + |
| 142 | +private int getEnergyDiv() { |
| 143 | + 1000 |
| 144 | +} |
| 145 | + |
| 146 | +BigDecimal calculateDelta (BigDecimal currentEnergy, Map previousMap) { |
| 147 | + if (previousMap?.'energy' == null) { |
| 148 | + return 0; |
| 149 | + } |
| 150 | + BigDecimal lastAcumulated = BigDecimal.valueOf(previousMap ['energy']); |
| 151 | + return currentEnergy.subtract(lastAcumulated).max(BigDecimal.ZERO); |
| 152 | +} |
0 commit comments