|
14 | 14 |
|
15 | 15 | local clusters = require "st.zigbee.zcl.clusters" |
16 | 16 | local constants = require "st.zigbee.constants" |
| 17 | +local capabilities = require "st.capabilities" |
| 18 | +local device_def = require "st.device" |
17 | 19 |
|
18 | 20 | local ColorControl = clusters.ColorControl |
19 | 21 | local IASZone = clusters.IASZone |
| 22 | +local Status = require "st.zigbee.generated.types.ZclStatus" |
| 23 | + |
| 24 | +local CONFIGURATION_VERSION_KEY = "_configuration_version" |
| 25 | +local CONFIGURATION_ATTEMPTED = "_reconfiguration_attempted" |
20 | 26 |
|
21 | 27 | local devices = { |
22 | 28 | IKEA_RGB_BULB = { |
@@ -60,8 +66,94 @@ local devices = { |
60 | 66 | } |
61 | 67 | } |
62 | 68 |
|
| 69 | + |
63 | 70 | local configurations = {} |
64 | 71 |
|
| 72 | +local active_power_configuration = { |
| 73 | + cluster = clusters.ElectricalMeasurement.ID, |
| 74 | + attribute = clusters.ElectricalMeasurement.attributes.ActivePower.ID, |
| 75 | + minimum_interval = 5, |
| 76 | + maximum_interval = 3600, |
| 77 | + data_type = clusters.ElectricalMeasurement.attributes.ActivePower.base_type, |
| 78 | + reportable_change = 5 |
| 79 | +} |
| 80 | + |
| 81 | +local instantaneous_demand_configuration = { |
| 82 | + cluster = clusters.SimpleMetering.ID, |
| 83 | + attribute = clusters.SimpleMetering.attributes.InstantaneousDemand.ID, |
| 84 | + minimum_interval = 5, |
| 85 | + maximum_interval = 3600, |
| 86 | + data_type = clusters.SimpleMetering.attributes.InstantaneousDemand.base_type, |
| 87 | + reportable_change = 5 |
| 88 | +} |
| 89 | + |
| 90 | +configurations.check_and_reconfig_devices = function(driver) |
| 91 | + for device_id, device in pairs(driver.device_cache) do |
| 92 | + local config_version = device:get_field(CONFIGURATION_VERSION_KEY) |
| 93 | + if config_version == nil or config_version < driver.current_config_version then |
| 94 | + if device:supports_capability(capabilities.powerMeter) then |
| 95 | + if device:supports_server_cluster(clusters.ElectricalMeasurement.ID) then |
| 96 | + -- Increase minimum reporting interval to 5 seconds |
| 97 | + device:send(clusters.ElectricalMeasurement.attributes.ActivePower:configure_reporting(device, 5, 600, 5)) |
| 98 | + device:add_configured_attribute(active_power_configuration) |
| 99 | + end |
| 100 | + if device:supports_server_cluster(clusters.SimpleMetering.ID) then |
| 101 | + -- Increase minimum reporting interval to 5 seconds |
| 102 | + device:send(clusters.SimpleMetering.attributes.InstantaneousDemand:configure_reporting(device, 5, 600, 5)) |
| 103 | + device:add_configured_attribute(instantaneous_demand_configuration) |
| 104 | + end |
| 105 | + end |
| 106 | + device:set_field(CONFIGURATION_ATTEMPTED, true, {persist = true}) |
| 107 | + end |
| 108 | + end |
| 109 | + driver._reconfig_timer = nil |
| 110 | +end |
| 111 | + |
| 112 | +configurations.handle_reporting_config_response = function(driver, device, zb_mess) |
| 113 | + local dev = device |
| 114 | + local find_child_fn = device:get_field(device_def.FIND_CHILD_KEY) |
| 115 | + if find_child_fn ~= nil then |
| 116 | + local child = find_child_fn(device, zb_mess.address_header.src_endpoint.value) |
| 117 | + if child ~= nil then |
| 118 | + dev = child |
| 119 | + end |
| 120 | + end |
| 121 | + if dev:get_field(CONFIGURATION_ATTEMPTED) == true then |
| 122 | + if zb_mess.body.zcl_body.global_status ~= nil and zb_mess.body.zcl_body.global_status.value == Status.SUCCESS then |
| 123 | + dev:set_field(CONFIGURATION_VERSION_KEY, driver.current_config_version, {persist = true}) |
| 124 | + elseif zb_mess.body.zcl_body.config_records ~= nil then |
| 125 | + local config_records = zb_mess.body.zcl_body.config_records |
| 126 | + for _, record in ipairs(config_records) do |
| 127 | + if zb_mess.address_header.cluster.value == clusters.SimpleMetering.ID then |
| 128 | + if record.attr_id.value == clusters.SimpleMetering.attributes.InstantaneousDemand.ID |
| 129 | + and record.status.value == Status.SUCCESS then |
| 130 | + dev:set_field(CONFIGURATION_VERSION_KEY, driver.current_config_version, {persist = true}) |
| 131 | + end |
| 132 | + elseif zb_mess.address_header.cluster.value == clusters.ElectricalMeasurement.ID then |
| 133 | + if record.attr_id.value == clusters.ElectricalMeasurement.attributes.ActivePower.ID |
| 134 | + and record.status.value == Status.SUCCESS then |
| 135 | + dev:set_field(CONFIGURATION_VERSION_KEY, driver.current_config_version, {persist = true}) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +configurations.power_reconfig_wrapper = function(orig_function) |
| 145 | + local new_init = function(driver, device) |
| 146 | + local config_version = device:get_field(CONFIGURATION_VERSION_KEY) |
| 147 | + if config_version == nil or config_version < driver.current_config_version then |
| 148 | + if driver._reconfig_timer == nil then |
| 149 | + driver._reconfig_timer = driver:call_with_delay(5*60, configurations.check_and_reconfig_devices, "reconfig_power_devices") |
| 150 | + end |
| 151 | + end |
| 152 | + orig_function(driver, device) |
| 153 | + end |
| 154 | + return new_init |
| 155 | +end |
| 156 | + |
65 | 157 | configurations.get_device_configuration = function(zigbee_device) |
66 | 158 | for _, device in pairs(devices) do |
67 | 159 | for _, fingerprint in pairs(device.FINGERPRINTS) do |
|
0 commit comments