-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathinit.lua
More file actions
231 lines (215 loc) · 9.47 KB
/
init.lua
File metadata and controls
231 lines (215 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- Copyright 2025 SmartThings, Inc.
-- Licensed under the Apache License, Version 2.0
local capabilities = require "st.capabilities"
local constants = require "st.zigbee.constants"
local clusters = require "st.zigbee.zcl.clusters"
local SimpleMetering = clusters.SimpleMetering
local ElectricalMeasurement = clusters.ElectricalMeasurement
local configurations = require "configurations"
local PHASE_A_CONFIGURATION = {
{
cluster = SimpleMetering.ID,
attribute = SimpleMetering.attributes.CurrentSummationDelivered.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = SimpleMetering.attributes.CurrentSummationDelivered.base_type,
reportable_change = 0
},
{
cluster = SimpleMetering.ID,
attribute = 0x0001,
minimum_interval = 30,
maximum_interval = 120,
data_type = SimpleMetering.attributes.CurrentSummationDelivered.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.ActivePower.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.ActivePower.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSVoltage.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSVoltage.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSCurrent.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSCurrent.base_type,
reportable_change = 0
}
}
local PHASE_B_CONFIGURATION = {
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.ActivePowerPhB.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.ActivePowerPhB.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSVoltagePhB.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSVoltagePhB.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSCurrentPhB.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSCurrentPhB.base_type,
reportable_change = 0
},
}
local PHASE_C_CONFIGURATION = {
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.ActivePowerPhC.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.ActivePowerPhC.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSVoltagePhC.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSVoltagePhC.base_type,
reportable_change = 0
},
{
cluster = ElectricalMeasurement.ID,
attribute = ElectricalMeasurement.attributes.RMSCurrentPhC.ID,
minimum_interval = 30,
maximum_interval = 120,
data_type = ElectricalMeasurement.attributes.RMSCurrentPhC.base_type,
reportable_change = 0
}
}
local function energy_handler(driver, device, value, zb_rx)
local multiplier = 1
local divisor = 100
local raw_value = value.value
local raw_value_kilowatts = raw_value * multiplier/divisor
local offset = device:get_field(constants.ENERGY_METER_OFFSET) or 0
if raw_value_kilowatts < offset then
--- somehow our value has gone below the offset, so we'll reset the offset, since the device seems to have
offset = 0
device:set_field(constants.ENERGY_METER_OFFSET, offset, {persist = true})
end
raw_value_kilowatts = raw_value_kilowatts - offset
local raw_value_watts = raw_value_kilowatts*1000
local delta_tick
local last_save_ticks = device:get_field("LAST_SAVE_TICK")
if last_save_ticks == nil then last_save_ticks = 0 end
delta_tick = os.time() - last_save_ticks
-- wwst energy certification : powerConsumptionReport capability values should be updated every 15 minutes.
-- Check if 15 minutes have passed since the reporting time of current_power_consumption.
if delta_tick >= 15*60 then
local delta_energy = 0.0
local current_power_consumption = device:get_latest_state("main", capabilities.powerConsumptionReport.ID, capabilities.powerConsumptionReport.powerConsumption.NAME)
if current_power_consumption ~= nil then
delta_energy = math.max(raw_value_watts - current_power_consumption.energy, 0.0)
end
device:emit_event(capabilities.powerConsumptionReport.powerConsumption({energy = raw_value_watts, deltaEnergy = delta_energy })) -- the unit of these values should be 'Wh'
local curr_save_tick = last_save_ticks + 15*60 -- Set the time to a regular interval by adding 15 minutes to the existing last_save_ticks.
-- If the time 15 minutes from now is less than the current time, set the current time as the last time.
if curr_save_tick + 15*60 < os.time() then
curr_save_tick = os.time()
end
device:set_field("LAST_SAVE_TICK", curr_save_tick, {persist = false})
end
device:emit_event(capabilities.energyMeter.energy({value = raw_value_kilowatts, unit = "kWh"}))
end
local function generic_handler_factory(component_name, capability, multiplier, divisor, unit)
return function(driver, device, value, zb_rx)
local component = device.profile.components[component_name]
if component ~= nil then
local raw_value = value.value * multiplier / divisor
device:emit_component_event(component, capability({value = raw_value, unit = unit}))
end
end
end
local refresh = function(driver, device, cmd)
device:refresh()
end
local function resetEnergyMeter(self, device)
device:send(clusters.OnOff.server.commands.On(device))
-- Reset Power consumption
device:set_field(constants.ENERGY_METER_OFFSET, 0, {persist = true})
device:set_field("LAST_SAVE_TICK", os.time(), {persist = false})
end
local function do_configure(driver, device)
device:configure()
--device:send(device_management.build_bind_request(device, clusters.SimpleMetering.ID, driver.environment_info.hub_zigbee_eui))
--device:send(device_management.build_bind_request(device, clusters.ElectricalMeasurement.ID, driver.environment_info.hub_zigbee_eui))
device:refresh()
end
local device_init = function(self, device)
for _, attribute in ipairs(PHASE_A_CONFIGURATION) do
device:add_configured_attribute(attribute)
device:add_monitored_attribute(attribute)
end
if string.find(device:get_model(), "SDM02") or string.find(device:get_model(), "SPM02") or string.find(device:get_model(), "SDM01W") or string.find(device:get_model(), "SDM01-3Z1", 1, true) then
for _, attribute in ipairs(PHASE_B_CONFIGURATION) do
device:add_configured_attribute(attribute)
device:add_monitored_attribute(attribute)
end
end
if string.find(device:get_model(), "SPM02") or string.find(device:get_model(), "SDM01W") or string.find(device:get_model(), "SDM01-3Z1", 1, true)then
for _, attribute in ipairs(PHASE_C_CONFIGURATION) do
device:add_configured_attribute(attribute)
device:add_monitored_attribute(attribute)
end
end
end
local bituo_power_meter_handler = {
NAME = "bituo power meter handler",
lifecycle_handlers = {
init = configurations.power_reconfig_wrapper(device_init),
doConfigure = do_configure,
},
zigbee_handlers = {
attr = {
[clusters.SimpleMetering.ID] = {
[clusters.SimpleMetering.attributes.CurrentSummationDelivered.ID] = energy_handler,
[0x0001] = generic_handler_factory("TotalReverseEnergy", capabilities.energyMeter.energy, 1, 100, "kWh"),
},
[clusters.ElectricalMeasurement.ID] = {
[ElectricalMeasurement.attributes.ActivePower.ID] = generic_handler_factory("PhaseA", capabilities.powerMeter.power, 1, 1, "W"),
[ElectricalMeasurement.attributes.ActivePowerPhB.ID] = generic_handler_factory("PhaseB", capabilities.powerMeter.power, 1, 1, "W"),
[ElectricalMeasurement.attributes.ActivePowerPhC.ID] = generic_handler_factory("PhaseC", capabilities.powerMeter.power, 1, 1, "W"),
[ElectricalMeasurement.attributes.RMSVoltage.ID] = generic_handler_factory("PhaseA", capabilities.voltageMeasurement.voltage, 1, 100, "V"),
[ElectricalMeasurement.attributes.RMSVoltagePhB.ID] = generic_handler_factory("PhaseB", capabilities.voltageMeasurement.voltage, 1, 100, "V"),
[ElectricalMeasurement.attributes.RMSVoltagePhC.ID] = generic_handler_factory("PhaseC", capabilities.voltageMeasurement.voltage, 1, 100, "V"),
[ElectricalMeasurement.attributes.RMSCurrent.ID] = generic_handler_factory("PhaseA", capabilities.currentMeasurement.current, 1, 100, "A"),
[ElectricalMeasurement.attributes.RMSCurrentPhB.ID] = generic_handler_factory("PhaseB", capabilities.currentMeasurement.current, 1, 100, "A"),
[ElectricalMeasurement.attributes.RMSCurrentPhC.ID] = generic_handler_factory("PhaseC", capabilities.currentMeasurement.current, 1, 100, "A")
}
}
},
capability_handlers = {
[capabilities.refresh.ID] = {
[capabilities.refresh.commands.refresh.NAME] = refresh
},
[capabilities.energyMeter.ID] = {
[capabilities.energyMeter.commands.resetEnergyMeter.NAME] = resetEnergyMeter,
},
},
can_handle = require("bituo.can_handle"),
}
return bituo_power_meter_handler