|
| 1 | +-- Copyright 2026 SmartThings, Inc. |
| 2 | +-- Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +local test = require "integration_test" |
| 5 | +local clusters = require "st.zigbee.zcl.clusters" |
| 6 | +local capabilities = require "st.capabilities" |
| 7 | +local t_utils = require "integration_test.utils" |
| 8 | +local zigbee_test_utils = require "integration_test.zigbee_test_utils" |
| 9 | +local zb_const = require "st.zigbee.constants" |
| 10 | +local messages = require "st.zigbee.messages" |
| 11 | +local data_types = require "st.zigbee.data_types" |
| 12 | +local zcl_messages = require "st.zigbee.zcl" |
| 13 | +local report_attr = require "st.zigbee.zcl.global_commands.report_attribute" |
| 14 | + |
| 15 | +local SONOFF_PRIVATE_BUTTON_CLUSTER = 0xFC12 |
| 16 | +local SONOFF_PRIVATE_ATTR = 0x0000 |
| 17 | + |
| 18 | +local mock_device = test.mock_device.build_test_zigbee_device( |
| 19 | + { |
| 20 | + profile = t_utils.get_profile_definition("four-buttons-battery.yml"), |
| 21 | + zigbee_endpoints = { |
| 22 | + [1] = { |
| 23 | + id = 1, |
| 24 | + manufacturer = "SONOFF", |
| 25 | + model = "SNZB-01M", |
| 26 | + server_clusters = { 0x0001, 0xFC12 } |
| 27 | + }, |
| 28 | + [2] = { |
| 29 | + id = 2, |
| 30 | + manufacturer = "SONOFF", |
| 31 | + model = "SNZB-01M", |
| 32 | + server_clusters = { 0x0001, 0xFC12 } |
| 33 | + }, |
| 34 | + [3] = { |
| 35 | + id = 3, |
| 36 | + manufacturer = "SONOFF", |
| 37 | + model = "SNZB-01M", |
| 38 | + server_clusters = { 0x0001, 0xFC12 } |
| 39 | + }, |
| 40 | + [4] = { |
| 41 | + id = 4, |
| 42 | + manufacturer = "SONOFF", |
| 43 | + model = "SNZB-01M", |
| 44 | + server_clusters = { 0x0001, 0xFC12 } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +) |
| 49 | + |
| 50 | +zigbee_test_utils.prepare_zigbee_env_info() |
| 51 | + |
| 52 | +local function build_test_attr_report(device, endpoint, value) |
| 53 | + local report_body = report_attr.ReportAttribute({ |
| 54 | + report_attr.ReportAttributeAttributeRecord(SONOFF_PRIVATE_ATTR, data_types.Uint8.ID, value) |
| 55 | + }) |
| 56 | + local zclh = zcl_messages.ZclHeader({ |
| 57 | + cmd = data_types.ZCLCommandId(report_body.ID) |
| 58 | + }) |
| 59 | + local addrh = messages.AddressHeader( |
| 60 | + device:get_short_address(), |
| 61 | + endpoint, |
| 62 | + zb_const.HUB.ADDR, |
| 63 | + zb_const.HUB.ENDPOINT, |
| 64 | + zb_const.HA_PROFILE_ID, |
| 65 | + SONOFF_PRIVATE_BUTTON_CLUSTER |
| 66 | + ) |
| 67 | + local message_body = zcl_messages.ZclMessageBody({ |
| 68 | + zcl_header = zclh, |
| 69 | + zcl_body = report_body |
| 70 | + }) |
| 71 | + |
| 72 | + return messages.ZigbeeMessageRx({ |
| 73 | + address_header = addrh, |
| 74 | + body = message_body |
| 75 | + }) |
| 76 | +end |
| 77 | + |
| 78 | +local function test_init() |
| 79 | + test.mock_device.add_test_device(mock_device) |
| 80 | +end |
| 81 | + |
| 82 | +test.set_test_init_function(test_init) |
| 83 | + |
| 84 | +test.register_coroutine_test( |
| 85 | + "added lifecycle event", |
| 86 | + function() |
| 87 | + test.socket.capability:__set_channel_ordering("relaxed") |
| 88 | + test.socket.capability:__expect_send( |
| 89 | + mock_device:generate_test_message( |
| 90 | + "main", |
| 91 | + capabilities.button.supportedButtonValues({ "pushed", "double", "held", "pushed_3x" }, { visibility = { displayed = false } }) |
| 92 | + ) |
| 93 | + ) |
| 94 | + test.socket.capability:__expect_send( |
| 95 | + mock_device:generate_test_message( |
| 96 | + "main", |
| 97 | + capabilities.button.numberOfButtons({ value = 4 }, { visibility = { displayed = false } }) |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + -- Check initial events for button 1 |
| 102 | + test.socket.capability:__expect_send( |
| 103 | + mock_device:generate_test_message( |
| 104 | + "button1", |
| 105 | + capabilities.button.supportedButtonValues({ "pushed", "double", "held", "pushed_3x" }, { visibility = { displayed = false } }) |
| 106 | + ) |
| 107 | + ) |
| 108 | + test.socket.capability:__expect_send( |
| 109 | + mock_device:generate_test_message( |
| 110 | + "button1", |
| 111 | + capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } }) |
| 112 | + ) |
| 113 | + ) |
| 114 | + |
| 115 | + -- Check initial events for button 2 |
| 116 | + test.socket.capability:__expect_send( |
| 117 | + mock_device:generate_test_message( |
| 118 | + "button2", |
| 119 | + capabilities.button.supportedButtonValues({ "pushed", "double", "held", "pushed_3x" }, { visibility = { displayed = false } }) |
| 120 | + ) |
| 121 | + ) |
| 122 | + test.socket.capability:__expect_send( |
| 123 | + mock_device:generate_test_message( |
| 124 | + "button2", |
| 125 | + capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } }) |
| 126 | + ) |
| 127 | + ) |
| 128 | + |
| 129 | + -- Check initial events for button 3 |
| 130 | + test.socket.capability:__expect_send( |
| 131 | + mock_device:generate_test_message( |
| 132 | + "button3", |
| 133 | + capabilities.button.supportedButtonValues({ "pushed", "double", "held", "pushed_3x" }, { visibility = { displayed = false } }) |
| 134 | + ) |
| 135 | + ) |
| 136 | + test.socket.capability:__expect_send( |
| 137 | + mock_device:generate_test_message( |
| 138 | + "button3", |
| 139 | + capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } }) |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + -- Check initial events for button 4 |
| 144 | + test.socket.capability:__expect_send( |
| 145 | + mock_device:generate_test_message( |
| 146 | + "button4", |
| 147 | + capabilities.button.supportedButtonValues({ "pushed", "double", "held", "pushed_3x" }, { visibility = { displayed = false } }) |
| 148 | + ) |
| 149 | + ) |
| 150 | + test.socket.capability:__expect_send( |
| 151 | + mock_device:generate_test_message( |
| 152 | + "button4", |
| 153 | + capabilities.button.numberOfButtons({ value = 1 }, { visibility = { displayed = false } }) |
| 154 | + ) |
| 155 | + ) |
| 156 | + test.socket.capability:__expect_send( |
| 157 | + mock_device:generate_test_message("main", capabilities.button.button.pushed({ state_change = false })) |
| 158 | + ) |
| 159 | + |
| 160 | + test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" }) |
| 161 | + test.wait_for_events() |
| 162 | + end, |
| 163 | + { |
| 164 | + min_api_version = 17 |
| 165 | + } |
| 166 | +) |
| 167 | + |
| 168 | +test.register_coroutine_test( |
| 169 | + "Button pushed message should generate event", |
| 170 | + function() |
| 171 | + -- 0xFC12, 0x0000, 0x01 = pushed |
| 172 | + local attr_report = build_test_attr_report(mock_device, 1, data_types.Uint8(0x01)) |
| 173 | + |
| 174 | + test.socket.zigbee:__queue_receive({ mock_device.id, attr_report }) |
| 175 | + test.socket.capability:__expect_send( |
| 176 | + mock_device:generate_test_message("button1", capabilities.button.button.pushed({ state_change = true })) |
| 177 | + ) |
| 178 | + test.socket.capability:__expect_send( |
| 179 | + mock_device:generate_test_message("main", capabilities.button.button.pushed({ state_change = true })) |
| 180 | + ) |
| 181 | + end, |
| 182 | + { |
| 183 | + min_api_version = 17 |
| 184 | + } |
| 185 | +) |
| 186 | + |
| 187 | +test.register_coroutine_test( |
| 188 | + "Button double message should generate event", |
| 189 | + function() |
| 190 | + -- 0xFC12, 0x0000, 0x02 = double |
| 191 | + local attr_report = build_test_attr_report(mock_device, 1, data_types.Uint8(0x02)) |
| 192 | + |
| 193 | + test.socket.zigbee:__queue_receive({ mock_device.id, attr_report }) |
| 194 | + test.socket.capability:__expect_send( |
| 195 | + mock_device:generate_test_message("button1", capabilities.button.button.double({ state_change = true })) |
| 196 | + ) |
| 197 | + test.socket.capability:__expect_send( |
| 198 | + mock_device:generate_test_message("main", capabilities.button.button.double({ state_change = true })) |
| 199 | + ) |
| 200 | + end, |
| 201 | + { |
| 202 | + min_api_version = 17 |
| 203 | + } |
| 204 | +) |
| 205 | + |
| 206 | +test.register_coroutine_test( |
| 207 | + "Button held message should generate event", |
| 208 | + function() |
| 209 | + -- 0xFC12, 0x0000, 0x03 = held |
| 210 | + local attr_report = build_test_attr_report(mock_device, 1, data_types.Uint8(0x03)) |
| 211 | + |
| 212 | + test.socket.zigbee:__queue_receive({ mock_device.id, attr_report }) |
| 213 | + test.socket.capability:__expect_send( |
| 214 | + mock_device:generate_test_message("button1", capabilities.button.button.held({ state_change = true })) |
| 215 | + ) |
| 216 | + test.socket.capability:__expect_send( |
| 217 | + mock_device:generate_test_message("main", capabilities.button.button.held({ state_change = true })) |
| 218 | + ) |
| 219 | + end, |
| 220 | + { |
| 221 | + min_api_version = 17 |
| 222 | + } |
| 223 | +) |
| 224 | + |
| 225 | +test.register_coroutine_test( |
| 226 | + "Button pushed_3x message should generate event", |
| 227 | + function() |
| 228 | + -- 0xFC12, 0x0000, 0x04 = pushed_3x |
| 229 | + local attr_report = build_test_attr_report(mock_device, 1, data_types.Uint8(0x04)) |
| 230 | + |
| 231 | + test.socket.zigbee:__queue_receive({ mock_device.id, attr_report }) |
| 232 | + test.socket.capability:__expect_send( |
| 233 | + mock_device:generate_test_message("button1", capabilities.button.button.pushed_3x({ state_change = true })) |
| 234 | + ) |
| 235 | + test.socket.capability:__expect_send( |
| 236 | + mock_device:generate_test_message("main", capabilities.button.button.pushed_3x({ state_change = true })) |
| 237 | + ) |
| 238 | + end, |
| 239 | + { |
| 240 | + min_api_version = 17 |
| 241 | + } |
| 242 | +) |
| 243 | + |
| 244 | +test.register_coroutine_test( |
| 245 | + "Button 2 pushed message should generate event on button2 component", |
| 246 | + function() |
| 247 | + -- Endpoint 2 test |
| 248 | + local attr_report = build_test_attr_report(mock_device, 2, data_types.Uint8(0x01)) |
| 249 | + |
| 250 | + test.socket.zigbee:__queue_receive({ mock_device.id, attr_report }) |
| 251 | + test.socket.capability:__expect_send( |
| 252 | + mock_device:generate_test_message("button2", capabilities.button.button.pushed({ state_change = true })) |
| 253 | + ) |
| 254 | + test.socket.capability:__expect_send( |
| 255 | + mock_device:generate_test_message("main", capabilities.button.button.pushed({ state_change = true })) |
| 256 | + ) |
| 257 | + end, |
| 258 | + { |
| 259 | + min_api_version = 17 |
| 260 | + } |
| 261 | +) |
| 262 | + |
| 263 | +test.register_coroutine_test( |
| 264 | + "Battery percentage report should generate event", |
| 265 | + function() |
| 266 | + local battery_report = clusters.PowerConfiguration.attributes.BatteryPercentageRemaining:build_test_attr_report(mock_device, 180) |
| 267 | + |
| 268 | + test.socket.zigbee:__queue_receive({ mock_device.id, battery_report }) |
| 269 | + test.socket.capability:__expect_send( |
| 270 | + mock_device:generate_test_message("main", capabilities.battery.battery(90)) |
| 271 | + ) |
| 272 | + end, |
| 273 | + { |
| 274 | + min_api_version = 17 |
| 275 | + } |
| 276 | +) |
| 277 | + |
| 278 | +test.run_registered_tests() |
| 279 | + |
0 commit comments