|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +# See https://github.com/project-chip/connectedhomeip/blob/master/docs/testing/python.md#defining-the-ci-test-arguments |
| 19 | +# for details about the block below. |
| 20 | +# |
| 21 | +# === BEGIN CI TEST ARGUMENTS === |
| 22 | +# test-runner-runs: run1 |
| 23 | +# test-runner-run/run1/app: ${ALL_CLUSTERS_APP} |
| 24 | +# test-runner-run/run1/factoryreset: True |
| 25 | +# test-runner-run/run1/quiet: True |
| 26 | +# test-runner-run/run1/app-args: --discriminator 1234 --KVS kvs1 --trace-to json:${TRACE_APP}.json |
| 27 | +# test-runner-run/run1/script-args: --storage-path admin_storage.json --commissioning-method on-network --discriminator 1234 --passcode 20202021 --endpoint 1 --trace-to json:${TRACE_TEST_JSON}.json --trace-to perfetto:${TRACE_TEST_PERFETTO}.perfetto |
| 28 | +# === END CI TEST ARGUMENTS === |
| 29 | + |
| 30 | +import logging |
| 31 | + |
| 32 | +import chip.clusters as Clusters |
| 33 | +from chip.interaction_model import Status |
| 34 | +from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, type_matches |
| 35 | +from mobly import asserts |
| 36 | + |
| 37 | + |
| 38 | +class TC_WHM_2_1(MatterBaseTest): |
| 39 | + |
| 40 | + def __init__(self, *args): |
| 41 | + super().__init__(*args) |
| 42 | + self.endpoint = 0 |
| 43 | + |
| 44 | + def steps_TC_WHM_2_1(self) -> list[TestStep]: |
| 45 | + steps = [ |
| 46 | + TestStep(1, "Commissioning, already done", is_commissioning=True), |
| 47 | + TestStep(2, "Read the SupportedModes attribute"), |
| 48 | + TestStep(3, "Read the CurrentMode attribute"), |
| 49 | + TestStep(4, "Send ChangeToMode command with NewMode"), |
| 50 | + TestStep(5, "Manually put the device in a state from which it will FAIL to transition"), |
| 51 | + TestStep(6, "Read CurrentMode attribute"), |
| 52 | + TestStep(7, "Send ChangeToMode command with NewMode"), |
| 53 | + TestStep(8, "Read CurrentMode attribute"), |
| 54 | + TestStep(9, "Manually put the device in a state from which it will SUCCESSFULLY transition"), |
| 55 | + TestStep(10, "Read CurrentMode attribute"), |
| 56 | + TestStep(11, "Send ChangeToMode command with NewMode"), |
| 57 | + TestStep(12, "Read CurrentMode attribute"), |
| 58 | + TestStep(13, "Send ChangeToMode command with NewMode set to an invalid mode"), |
| 59 | + TestStep(14, "Read CurrentMode attribute"), |
| 60 | + ] |
| 61 | + return steps |
| 62 | + |
| 63 | + async def read_mode_attribute_expect_success(self, endpoint, attribute): |
| 64 | + cluster = Clusters.Objects.WaterHeaterMode |
| 65 | + return await self.read_single_attribute_check_success(endpoint=endpoint, cluster=cluster, attribute=attribute) |
| 66 | + |
| 67 | + async def send_change_to_mode_cmd(self, newMode) -> Clusters.Objects.WaterHeaterMode.Commands.ChangeToModeResponse: |
| 68 | + ret = await self.send_single_cmd(cmd=Clusters.Objects.WaterHeaterMode.Commands.ChangeToMode(newMode=newMode), endpoint=self.endpoint) |
| 69 | + asserts.assert_true(type_matches(ret, Clusters.Objects.WaterHeaterMode.Commands.ChangeToModeResponse), |
| 70 | + "Unexpected return type for Water Heater Mode ChangeToMode") |
| 71 | + return ret |
| 72 | + |
| 73 | + def pics_TC_WHM_2_1(self) -> list[str]: |
| 74 | + return ["WHM.S"] |
| 75 | + |
| 76 | + @async_test_body |
| 77 | + async def test_TC_WHM_2_1(self): |
| 78 | + |
| 79 | + # Valid modes. Only ModeManual referred to in this test |
| 80 | + # ModeOff = 0 |
| 81 | + ModeManual = 1 |
| 82 | + # ModeTimed = 2 |
| 83 | + |
| 84 | + self.endpoint = self.matter_test_config.endpoint |
| 85 | + |
| 86 | + attributes = Clusters.WaterHeaterMode.Attributes |
| 87 | + |
| 88 | + self.step(1) |
| 89 | + # Commission DUT - already done |
| 90 | + |
| 91 | + self.step(2) |
| 92 | + |
| 93 | + supported_modes = await self.read_mode_attribute_expect_success(endpoint=self.endpoint, attribute=attributes.SupportedModes) |
| 94 | + |
| 95 | + logging.info(f"SupportedModes: {supported_modes}") |
| 96 | + |
| 97 | + asserts.assert_greater_equal(len(supported_modes), 2, |
| 98 | + "SupportedModes must have at least two entries!") |
| 99 | + |
| 100 | + self.step(3) |
| 101 | + |
| 102 | + old_current_mode = await self.read_mode_attribute_expect_success(endpoint=self.endpoint, attribute=attributes.CurrentMode) |
| 103 | + |
| 104 | + logging.info(f"CurrentMode: {old_current_mode}") |
| 105 | + |
| 106 | + # pick a value that's not on the list of supported modes |
| 107 | + modes = [m.mode for m in supported_modes] |
| 108 | + invalid_mode = max(modes) + 1 |
| 109 | + |
| 110 | + self.step(4) |
| 111 | + |
| 112 | + ret = await self.send_change_to_mode_cmd(newMode=old_current_mode) |
| 113 | + logging.info(f"ret.status {ret.status}") |
| 114 | + asserts.assert_equal(ret.status, Status.Success, |
| 115 | + "Changing the mode to the current mode should be a no-op") |
| 116 | + |
| 117 | + # Steps 5-9 are not performed as WHM.S.M.CAN_TEST_MODE_FAILURE is false |
| 118 | + # TODO - see issue 34565 |
| 119 | + self.step(5) |
| 120 | + self.step(6) |
| 121 | + self.step(7) |
| 122 | + self.step(8) |
| 123 | + self.step(9) |
| 124 | + |
| 125 | + self.step(10) |
| 126 | + |
| 127 | + old_current_mode = await self.read_mode_attribute_expect_success(endpoint=self.endpoint, attribute=attributes.CurrentMode) |
| 128 | + |
| 129 | + logging.info(f"CurrentMode: {old_current_mode}") |
| 130 | + |
| 131 | + self.step(11) |
| 132 | + |
| 133 | + ret = await self.send_change_to_mode_cmd(newMode=ModeManual) |
| 134 | + asserts.assert_true(ret.status == Status.Success, |
| 135 | + f"Changing to mode {ModeManual}must succeed due to the current state of the device") |
| 136 | + |
| 137 | + self.step(12) |
| 138 | + |
| 139 | + current_mode = await self.read_mode_attribute_expect_success(endpoint=self.endpoint, attribute=attributes.CurrentMode) |
| 140 | + |
| 141 | + logging.info(f"CurrentMode: {current_mode}") |
| 142 | + |
| 143 | + asserts.assert_true(current_mode == ModeManual, |
| 144 | + "CurrentMode doesn't match the argument of the successful ChangeToMode command!") |
| 145 | + |
| 146 | + self.step(13) |
| 147 | + |
| 148 | + ret = await self.send_change_to_mode_cmd(newMode=invalid_mode) |
| 149 | + logging.info(f"ret {ret}") |
| 150 | + asserts.assert_true(ret.status == Status.Failure, |
| 151 | + f"Attempt to change to invalid mode {invalid_mode} didn't fail as expected") |
| 152 | + |
| 153 | + self.step(14) |
| 154 | + |
| 155 | + current_mode = await self.read_mode_attribute_expect_success(endpoint=self.endpoint, attribute=attributes.CurrentMode) |
| 156 | + |
| 157 | + logging.info(f"CurrentMode: {current_mode}") |
| 158 | + |
| 159 | + asserts.assert_true(current_mode == ModeManual, |
| 160 | + "CurrentMode changed after failed ChangeToMode command!") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + default_matter_test_main() |
0 commit comments