Skip to content

Commit 4de1762

Browse files
authored
Merge pull request #2412 from pInksenberg/preventing-unnecessary-emitting-in-edge-driver
preventing unnecessary event emit in edge driver
2 parents 9d596dc + 70a778a commit 4de1762

File tree

44 files changed

+640
-47
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+640
-47
lines changed

drivers/SmartThings/zigbee-button/src/aqara/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local clusters = require "st.zigbee.zcl.clusters"
1717
local cluster_base = require "st.zigbee.cluster_base"
1818
local data_types = require "st.zigbee.data_types"
1919
local capabilities = require "st.capabilities"
20+
local button_utils = require "button_utils"
2021

2122

2223
local PowerConfiguration = clusters.PowerConfiguration
@@ -83,7 +84,7 @@ end
8384
local function added_handler(self, device)
8485
device:emit_event(capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }}))
8586
device:emit_event(capabilities.button.numberOfButtons({value = 1}))
86-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
87+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
8788
device:emit_event(capabilities.battery.battery(100))
8889
end
8990

drivers/SmartThings/zigbee-button/src/button_utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ button_utils.build_button_handler = function(button_name, pressed_type)
7979
end
8080
end
8181

82+
button_utils.emit_event_if_latest_state_missing = function(device, component, capability, attribute_name, value)
83+
if device:get_latest_state(component, capability.ID, attribute_name) == nil then
84+
device:emit_event(value)
85+
end
86+
end
87+
8288
return button_utils

drivers/SmartThings/zigbee-button/src/dimming-remote/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ local function added_handler(self, device)
5454
device:emit_component_event(component, capabilities.button.numberOfButtons({value = number_of_buttons}, {visibility = { displayed = false }}))
5555
end
5656
device:send(PowerConfiguration.attributes.BatteryVoltage:read(device))
57-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
57+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
5858
end
5959

6060
local function do_configure(self, device)

drivers/SmartThings/zigbee-button/src/frient/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local cluster_base = require "st.zigbee.cluster_base"
1717
local capabilities = require "st.capabilities"
1818
local battery_defaults = require "st.zigbee.defaults.battery_defaults"
1919
local data_types = require "st.zigbee.data_types"
20+
local button_utils = require "button_utils"
2021
local BasicInput = zcl_clusters.BasicInput
2122
local PowerConfiguration = zcl_clusters.PowerConfiguration
2223
local OnOff = zcl_clusters.OnOff
@@ -163,7 +164,7 @@ end
163164
local function added_handler(self, device)
164165
device:emit_event(capabilities.button.supportedButtonValues({"pushed"}, {visibility = { displayed = false }}))
165166
device:emit_event(capabilities.button.numberOfButtons({value = 1}))
166-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
167+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
167168
end
168169

169170
local function do_configure(driver, device, event, args)

drivers/SmartThings/zigbee-button/src/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local defaults = require "st.zigbee.defaults"
1818
local constants = require "st.zigbee.constants"
1919
local IASZone = (require "st.zigbee.zcl.clusters").IASZone
2020
local TemperatureMeasurement = (require "st.zigbee.zcl.clusters").TemperatureMeasurement
21+
local button_utils = require "button_utils"
2122

2223
local temperature_measurement_defaults = {
2324
MIN_TEMP = "MIN_TEMP",
@@ -109,7 +110,7 @@ end
109110
local function added_handler(self, device)
110111
device:emit_event(capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }}))
111112
device:emit_event(capabilities.button.numberOfButtons({value = 1}, {visibility = { displayed = false }}))
112-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
113+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
113114
if device:supports_server_cluster(TemperatureMeasurement.ID) then
114115
device:send(TemperatureMeasurement.attributes.MaxMeasuredValue:read(device))
115116
device:send(TemperatureMeasurement.attributes.MinMeasuredValue:read(device))

drivers/SmartThings/zigbee-button/src/iris/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262
local function added_handler(self, device)
6363
device:emit_event(capabilities.button.supportedButtonValues({"pushed", "held"}, {visibility = { displayed = false }}))
6464
device:emit_event(capabilities.button.numberOfButtons({value = 1}, {visibility = { displayed = false }}))
65-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
65+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
6666
device:send(PowerConfiguration.attributes.BatteryVoltage:read(device))
6767
end
6868

drivers/SmartThings/zigbee-button/src/pushButton/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
-- for the specific language governing permissions and limitations under the License.
2525

2626
local capabilities = require "st.capabilities"
27+
local button_utils = require "button_utils"
2728

2829
local function added_handler(self, device)
2930
device:emit_event(capabilities.button.supportedButtonValues({"pushed"}, {visibility = { displayed = false }}))
3031
device:emit_event(capabilities.button.numberOfButtons({value = 1}, {visibility = { displayed = false }}))
31-
device:emit_event(capabilities.button.button.pushed({state_change = false}))
32+
button_utils.emit_event_if_latest_state_missing(device, "main", capabilities.button, capabilities.button.button.NAME, capabilities.button.button.pushed({state_change = false}))
3233
end
3334

3435
local push_button = {

drivers/SmartThings/zigbee-button/src/test/test_aqara_button.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,34 @@ test.set_test_init_function(test_init)
6767
test.register_coroutine_test(
6868
"Handle added lifecycle -- e1",
6969
function()
70+
-- The initial button pushed event should be send during the device's first time onboarding
7071
test.socket.device_lifecycle:__queue_receive({ mock_device_e1.id, "added" })
7172
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }})))
7273
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
7374
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.button.button.pushed({state_change = false})))
7475
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.battery.battery(100)))
76+
-- Avoid sending the initial button pushed event after driver switch-over, as the switch-over event itself re-triggers the added lifecycle.
77+
test.socket.device_lifecycle:__queue_receive({ mock_device_e1.id, "added" })
78+
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }})))
79+
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
80+
test.socket.capability:__expect_send(mock_device_e1:generate_test_message("main", capabilities.battery.battery(100)))
7581
end
7682
)
7783

7884
test.register_coroutine_test(
7985
"Handle added lifecycle -- t1",
8086
function()
87+
-- The initial button pushed event should be send during the device's first time onboarding
8188
test.socket.device_lifecycle:__queue_receive({ mock_device_t1.id, "added" })
8289
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }})))
8390
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
8491
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.button.button.pushed({state_change = false})))
8592
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.battery.battery(100)))
93+
-- Avoid sending the initial button pushed event after driver switch-over, as the switch-over event itself re-triggers the added lifecycle.
94+
test.socket.device_lifecycle:__queue_receive({ mock_device_t1.id, "added" })
95+
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed","held","double"}, {visibility = { displayed = false }})))
96+
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
97+
test.socket.capability:__expect_send(mock_device_t1:generate_test_message("main", capabilities.battery.battery(100)))
8698
end
8799
)
88100

drivers/SmartThings/zigbee-button/src/test/test_dimming_remote.lua

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ test.register_coroutine_test(
176176
test.register_coroutine_test(
177177
"added lifecycle event",
178178
function()
179+
-- The initial button pushed event should be send during the device's first time onboarding
179180
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" })
180181
test.socket.capability:__set_channel_ordering("relaxed")
181182
test.socket.capability:__expect_send(
@@ -221,7 +222,49 @@ test.register_coroutine_test(
221222
attribute_id = "button", state = { value = "pushed" }
222223
}
223224
})
224-
225+
test.socket.zigbee:__expect_send({
226+
mock_device.id,
227+
PowerConfiguration.attributes.BatteryVoltage:read(mock_device)
228+
})
229+
-- Avoid sending the initial button pushed event after driver switch-over, as the switch-over event itself re-triggers the added lifecycle.
230+
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" })
231+
test.socket.capability:__set_channel_ordering("relaxed")
232+
test.socket.capability:__expect_send(
233+
mock_device:generate_test_message(
234+
"main",
235+
capabilities.button.supportedButtonValues({ "pushed", "held" }, { visibility = { displayed = false } })
236+
)
237+
)
238+
test.socket.capability:__expect_send(
239+
mock_device:generate_test_message(
240+
"main",
241+
capabilities.button.numberOfButtons({ value = 2 }, { visibility = { displayed = false } })
242+
)
243+
)
244+
test.socket.capability:__expect_send(
245+
mock_device:generate_test_message(
246+
"button1",
247+
capabilities.button.supportedButtonValues({ "pushed", "held" }, { visibility = { displayed = false } })
248+
)
249+
)
250+
test.socket.capability:__expect_send(
251+
mock_device:generate_test_message(
252+
"button1",
253+
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
254+
)
255+
)
256+
test.socket.capability:__expect_send(
257+
mock_device:generate_test_message(
258+
"button2",
259+
capabilities.button.supportedButtonValues({ "pushed", "held" }, { visibility = { displayed = false } })
260+
)
261+
)
262+
test.socket.capability:__expect_send(
263+
mock_device:generate_test_message(
264+
"button2",
265+
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
266+
)
267+
)
225268
test.socket.zigbee:__expect_send({
226269
mock_device.id,
227270
PowerConfiguration.attributes.BatteryVoltage:read(mock_device)

drivers/SmartThings/zigbee-button/src/test/test_frient_button.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,23 @@ test.register_coroutine_test(
165165
[15] = 0,
166166
[10] = 0
167167
}
168+
-- The initial button pushed event should be send during the device's first time onboarding
168169
test.socket.device_lifecycle:__queue_receive({mock_device.id, "added"})
169170
test.socket.capability:__expect_send(mock_device:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed"}, {visibility = { displayed = false }})))
170171
test.socket.capability:__expect_send(mock_device:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
171172
test.socket.capability:__expect_send(mock_device:generate_test_message("main", button_attr.pushed({ state_change = false})))
172173
test.wait_for_events()
173174

174-
175-
for voltage, batt_perc in pairs(battery_table) do
176-
test.socket.zigbee:__queue_receive({ mock_device.id, PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, voltage) })
177-
test.socket.capability:__expect_send( mock_device:generate_test_message("main", capabilities.battery.battery(batt_perc)) )
178-
179-
end
175+
for voltage, batt_perc in pairs(battery_table) do
176+
test.socket.zigbee:__queue_receive({ mock_device.id, PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, voltage) })
177+
test.socket.capability:__expect_send( mock_device:generate_test_message("main", capabilities.battery.battery(batt_perc)))
178+
end
179+
test.wait_for_events()
180+
-- Avoid sending the button pushed contactSensor event after driver switch-over, as the switch-over event itself re-triggers the added lifecycle.
181+
test.socket.device_lifecycle:__queue_receive({mock_device.id, "added"})
182+
test.socket.capability:__expect_send(mock_device:generate_test_message("main", capabilities.button.supportedButtonValues({"pushed"}, {visibility = { displayed = false }})))
183+
test.socket.capability:__expect_send(mock_device:generate_test_message("main", capabilities.button.numberOfButtons({value = 1})))
184+
test.wait_for_events()
180185
end
181186
)
182187

0 commit comments

Comments
 (0)