1+ -- Copyright 2025 SmartThings
2+ --
3+ -- Licensed under the Apache License, Version 2.0 (the "License");
4+ -- you may not use this file except in compliance with the License.
5+ -- You may obtain a copy of the License at
6+ --
7+ -- http://www.apache.org/licenses/LICENSE-2.0
8+ --
9+ -- Unless required by applicable law or agreed to in writing, software
10+ -- distributed under the License is distributed on an "AS IS" BASIS,
11+ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ -- See the License for the specific language governing permissions and
13+ -- limitations under the License.
14+
15+ -- Mock out globals
16+ local test = require " integration_test"
17+ local zigbee_test_utils = require " integration_test.zigbee_test_utils"
18+ local t_utils = require " integration_test.utils"
19+
20+ local clusters = require " st.zigbee.zcl.clusters"
21+ local PowerConfiguration = clusters .PowerConfiguration
22+ local DoorLock = clusters .DoorLock
23+ local Alarm = clusters .Alarms
24+ local capabilities = require " st.capabilities"
25+
26+ local json = require " st.json"
27+
28+ local mock_datastore = require " integration_test.mock_env_datastore"
29+
30+ local mock_device = test .mock_device .build_test_zigbee_device (
31+ {
32+ profile = t_utils .get_profile_definition (" base-lock.yml" ),
33+ data = {
34+ lockCodes = json .encode ({
35+ [" 1" ] = " Zach" ,
36+ [" 2" ] = " Steven"
37+ }),
38+ }
39+ }
40+ )
41+
42+ zigbee_test_utils .prepare_zigbee_env_info ()
43+ local function test_init ()end
44+
45+ test .set_test_init_function (test_init )
46+
47+ test .register_coroutine_test (
48+ " Device called 'migrate' command" ,
49+ function ()
50+ test .mock_device .add_test_device (mock_device )
51+ test .socket .device_lifecycle :__queue_receive ({ mock_device .id , " added" })
52+ test .socket .zigbee :__expect_send ({ mock_device .id , PowerConfiguration .attributes .BatteryPercentageRemaining :read (mock_device ) })
53+ test .socket .zigbee :__expect_send ({ mock_device .id , DoorLock .attributes .LockState :read (mock_device ) })
54+ test .socket .zigbee :__expect_send ({ mock_device .id , Alarm .attributes .AlarmCount :read (mock_device ) })
55+ test .wait_for_events ()
56+ -- Validate lockCodes field
57+ mock_datastore .__assert_device_store_contains (mock_device .id , " lockCodes" , { [" 1" ] = " Zach" , [" 2" ] = " Steven" })
58+ -- Validate state cache
59+ mock_datastore .__assert_device_store_contains (mock_device .id , " __state_cache" ,
60+ {
61+ main = {
62+ lockCodes = {
63+ lockCodes = {value = json .encode ({ [" 1" ] = " Zach" , [" 2" ] = " Steven" }) }
64+ }
65+ }
66+ }
67+ )
68+ -- Validate migration complete flag
69+ mock_datastore .__assert_device_store_contains (mock_device .id , " migrationComplete" , true )
70+
71+ -- Set min/max code length attributes
72+ test .socket .zigbee :__queue_receive ({ mock_device .id , DoorLock .attributes .MinPINCodeLength :build_test_attr_report (mock_device , 5 ) })
73+ test .socket .zigbee :__queue_receive ({ mock_device .id , DoorLock .attributes .MaxPINCodeLength :build_test_attr_report (mock_device , 10 ) })
74+ test .socket .zigbee :__queue_receive ({ mock_device .id , DoorLock .attributes .NumberOfPINUsersSupported :build_test_attr_report (mock_device , 4 ) })
75+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCodes .minCodeLength (5 , { visibility = { displayed = false } })))
76+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCodes .maxCodeLength (10 , { visibility = { displayed = false } })))
77+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCodes .maxCodes (4 , { visibility = { displayed = false } })))
78+
79+ -- Validate `migrate` command functionality.
80+ test .socket .capability :__queue_receive ({ mock_device .id , { capability = capabilities .lockCodes .ID , command = " migrate" , args = {} } })
81+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCredentials .minPinCodeLen (5 , { visibility = { displayed = false } })))
82+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCredentials .maxPinCodeLen (10 , { visibility = { displayed = false } })))
83+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCredentials .pinUsersSupported (4 , { visibility = { displayed = false } })))
84+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCredentials .credentials ({{credentialIndex = 1 , credentialType = " pin" , userIndex = 1 }, {credentialIndex = 2 , credentialType = " pin" , userIndex = 2 }}, { visibility = { displayed = false } })))
85+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCredentials .supportedCredentials ({" pin" }, { visibility = { displayed = false } })))
86+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockUsers .users ({{userIndex = 1 , userName = " Zach" , userType = " guest" }, {userIndex = 2 , userName = " Steven" , userType = " guest" }}, { visibility = { displayed = false } })))
87+ test .socket .capability :__expect_send ( mock_device :generate_test_message (" main" , capabilities .lockCodes .migrated (true , { visibility = { displayed = false } })))
88+ test .wait_for_events ()
89+ end
90+ )
91+
92+ test .run_registered_tests ()
0 commit comments