Skip to content

Commit bbf4e10

Browse files
author
West_Zhao
committed
Use defaults for Sonoff battery and added lifecycle
- Remove custom battery attribute handler and added lifecycle in Sonoff subdriver - Align Sonoff tests with default added behavior and keep battery report coverage
1 parent 4658fb7 commit bbf4e10

2 files changed

Lines changed: 4 additions & 52 deletions

File tree

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ test.set_test_init_function(test_init)
6363
test.register_coroutine_test(
6464
"added lifecycle event",
6565
function()
66+
test.socket.capability:__set_channel_ordering("relaxed")
6667
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" })
6768

6869
-- Check initial events for button 1
@@ -78,9 +79,6 @@ test.register_coroutine_test(
7879
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
7980
)
8081
)
81-
test.socket.capability:__expect_send(
82-
mock_device:generate_test_message("button1", capabilities.button.button.pushed({ state_change = false }))
83-
)
8482

8583
-- Check initial events for button 2
8684
test.socket.capability:__expect_send(
@@ -95,9 +93,6 @@ test.register_coroutine_test(
9593
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
9694
)
9795
)
98-
test.socket.capability:__expect_send(
99-
mock_device:generate_test_message("button2", capabilities.button.button.pushed({ state_change = false }))
100-
)
10196

10297
-- Check initial events for button 3
10398
test.socket.capability:__expect_send(
@@ -112,9 +107,6 @@ test.register_coroutine_test(
112107
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
113108
)
114109
)
115-
test.socket.capability:__expect_send(
116-
mock_device:generate_test_message("button3", capabilities.button.button.pushed({ state_change = false }))
117-
)
118110

119111
-- Check initial events for button 4
120112
test.socket.capability:__expect_send(
@@ -129,21 +121,7 @@ test.register_coroutine_test(
129121
capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } })
130122
)
131123
)
132-
test.socket.capability:__expect_send(
133-
mock_device:generate_test_message("button4", capabilities.button.button.pushed({ state_change = false }))
134-
)
135-
136-
test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" })
137-
test.socket.zigbee:__expect_send({
138-
mock_device.id,
139-
clusters.PowerConfiguration.attributes.BatteryPercentageRemaining:configure_reporting(mock_device, 30, 21600, 1)
140-
})
141-
test.socket.zigbee:__expect_send({
142-
mock_device.id,
143-
zigbee_test_utils.build_bind_request(mock_device, zigbee_test_utils.mock_hub_eui, clusters.PowerConfiguration.ID)
144-
})
145-
146-
mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" })
124+
test.wait_for_events()
147125
end
148126
)
149127

@@ -247,9 +225,7 @@ test.register_coroutine_test(
247225
test.register_coroutine_test(
248226
"Battery percentage report should generate event",
249227
function()
250-
-- 0x0001 PowerConfiguration, 0x0021 BatteryPercentageRemaining
251-
-- Driver logic: math.floor(value / 2)
252-
local battery_report = clusters.PowerConfiguration.attributes.BatteryPercentageRemaining:build_test_attr_report(mock_device, 180) -- 180/2 = 90%
228+
local battery_report = clusters.PowerConfiguration.attributes.BatteryPercentageRemaining:build_test_attr_report(mock_device, 180)
253229

254230
test.socket.zigbee:__queue_receive({ mock_device.id, battery_report })
255231
test.socket.capability:__expect_send(

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
-- limitations under the License.
1414

1515
local capabilities = require "st.capabilities"
16-
local clusters = require "st.zigbee.zcl.clusters"
1716
local log = require "log"
1817

1918
local SONOFF_CLUSTER_ID = 0xFC12
2019
local SONOFF_ATTR_ID = 0x0000
21-
local BatteryPercentageRemaining = clusters.PowerConfiguration.attributes.BatteryPercentageRemaining
2220

2321
local EVENT_MAP = {
2422
[0x01] = capabilities.button.button.pushed,
@@ -31,11 +29,6 @@ local function can_handle(opts, driver, device, ...)
3129
return device:get_manufacturer() == "SONOFF"
3230
end
3331

34-
local function battery_attr_handler(driver, device, value, zb_rx)
35-
local percent = math.floor((value.value or 0) / 2)
36-
device:emit_event(capabilities.battery.battery(percent))
37-
end
38-
3932
local function sonoff_attr_handler(driver, device, value, zb_rx)
4033
local attr_val = value.value
4134
local endpoint = zb_rx.address_header.src_endpoint.value
@@ -53,32 +46,15 @@ local function sonoff_attr_handler(driver, device, value, zb_rx)
5346
end
5447
end
5548

56-
local function added_handler(self, device)
57-
device:configure()
58-
for _, comp in pairs(device.profile.components) do
59-
if comp.id ~= "main" then
60-
device:emit_component_event(comp, capabilities.button.supportedButtonValues({"pushed", "double", "held", "pushed_3x"}, {visibility = { displayed = false }}))
61-
device:emit_component_event(comp, capabilities.button.numberOfButtons({value = 1}, {visibility = { displayed = false }}))
62-
device:emit_component_event(comp, capabilities.button.button.pushed({state_change = false}))
63-
end
64-
end
65-
end
66-
6749
local sonoff_handler = {
6850
NAME = "SONOFF Multi-Button Handler",
6951
zigbee_handlers = {
7052
attr = {
7153
[SONOFF_CLUSTER_ID] = {
7254
[SONOFF_ATTR_ID] = sonoff_attr_handler
73-
},
74-
[clusters.PowerConfiguration.ID] = {
75-
[BatteryPercentageRemaining.ID] = battery_attr_handler
76-
},
55+
}
7756
}
7857
},
79-
lifecycle_handlers = {
80-
added = added_handler
81-
},
8258
can_handle = can_handle
8359
}
8460

0 commit comments

Comments
 (0)