|
| 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 ElectricalMeasurement = clusters.ElectricalMeasurement |
| 7 | +local SimpleMetering = clusters.SimpleMetering |
| 8 | +local capabilities = require "st.capabilities" |
| 9 | +local zigbee_test_utils = require "integration_test.zigbee_test_utils" |
| 10 | +local t_utils = require "integration_test.utils" |
| 11 | +local cluster_base = require "st.zigbee.cluster_base" |
| 12 | +local data_types = require "st.zigbee.data_types" |
| 13 | + |
| 14 | + |
| 15 | +-- Mock out globals |
| 16 | +local mock_device = test.mock_device.build_test_zigbee_device({ |
| 17 | + profile = t_utils.get_profile_definition("power-meter-1p.yml"), |
| 18 | + zigbee_endpoints = { |
| 19 | + [1] = { |
| 20 | + id = 1, |
| 21 | + manufacturer = "Zemismart", |
| 22 | + model = "SPM01-1Z2", |
| 23 | + server_clusters = {SimpleMetering.ID, ElectricalMeasurement.ID} |
| 24 | + } |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +zigbee_test_utils.prepare_zigbee_env_info() |
| 29 | + |
| 30 | +local function test_init() |
| 31 | + test.mock_device.add_test_device(mock_device) |
| 32 | + zigbee_test_utils.init_noop_health_check_timer() |
| 33 | +end |
| 34 | + |
| 35 | +test.set_test_init_function(test_init) |
| 36 | + |
| 37 | +test.register_coroutine_test( |
| 38 | + "SimpleMetering event should be handled by powerConsumptionReport capability", |
| 39 | + function() |
| 40 | + test.timer.__create_and_queue_test_time_advance_timer(15*60, "oneshot") |
| 41 | + -- #1 : 15 minutes have passed |
| 42 | + test.mock_time.advance_time(15*60) |
| 43 | + test.socket.zigbee:__queue_receive({ |
| 44 | + mock_device.id, |
| 45 | + SimpleMetering.attributes.CurrentSummationDelivered:build_test_attr_report(mock_device,150) |
| 46 | + }) |
| 47 | + test.socket.capability:__expect_send( |
| 48 | + mock_device:generate_test_message("main", capabilities.powerConsumptionReport.powerConsumption({energy = 1500.0, deltaEnergy = 0.0 })) |
| 49 | + ) |
| 50 | + test.socket.capability:__expect_send( |
| 51 | + mock_device:generate_test_message("main", capabilities.energyMeter.energy({value = 1.5, unit = "kWh"})) |
| 52 | + ) |
| 53 | + -- #2 : Not even 15 minutes passed |
| 54 | + test.wait_for_events() |
| 55 | + test.mock_time.advance_time(1*60) |
| 56 | + test.socket.zigbee:__queue_receive({ |
| 57 | + mock_device.id, |
| 58 | + SimpleMetering.attributes.CurrentSummationDelivered:build_test_attr_report(mock_device,170) |
| 59 | + }) |
| 60 | + test.socket.capability:__expect_send( |
| 61 | + mock_device:generate_test_message("main", capabilities.energyMeter.energy({value = 1.7, unit = "kWh"})) |
| 62 | + ) |
| 63 | + -- #3 : 15 minutes have passed |
| 64 | + test.wait_for_events() |
| 65 | + test.mock_time.advance_time(14*60) |
| 66 | + test.socket.zigbee:__queue_receive({ |
| 67 | + mock_device.id, |
| 68 | + SimpleMetering.attributes.CurrentSummationDelivered:build_test_attr_report(mock_device,200) |
| 69 | + }) |
| 70 | + test.socket.capability:__expect_send( |
| 71 | + mock_device:generate_test_message("main", capabilities.powerConsumptionReport.powerConsumption({energy = 2000.0, deltaEnergy = 500.0 })) |
| 72 | + ) |
| 73 | + test.socket.capability:__expect_send( |
| 74 | + mock_device:generate_test_message("main", capabilities.energyMeter.energy({value = 2.0, unit = "kWh"})) |
| 75 | + ) |
| 76 | + end |
| 77 | +) |
| 78 | + |
| 79 | +test.register_message_test( |
| 80 | + "ActivePower Report should be handled. Sensor value is in W, capability attribute value is in hectowatts", |
| 81 | + { |
| 82 | + { |
| 83 | + channel = "zigbee", |
| 84 | + direction = "receive", |
| 85 | + message = { mock_device.id, ElectricalMeasurement.attributes.ActivePower:build_test_attr_report(mock_device, |
| 86 | + 27) }, |
| 87 | + }, |
| 88 | + { |
| 89 | + channel = "capability", |
| 90 | + direction = "send", |
| 91 | + message = mock_device:generate_test_message("PhaseA", capabilities.powerMeter.power({ value = 27.0, unit = "W" })) |
| 92 | + } |
| 93 | + } |
| 94 | +) |
| 95 | + |
| 96 | +test.register_message_test( |
| 97 | + "RMSCurrent Report for PhaseA should be handled", |
| 98 | + { |
| 99 | + { |
| 100 | + channel = "zigbee", |
| 101 | + direction = "receive", |
| 102 | + message = { mock_device.id, ElectricalMeasurement.attributes.RMSCurrent:build_test_attr_report(mock_device, |
| 103 | + 34) }, |
| 104 | + }, |
| 105 | + { |
| 106 | + channel = "capability", |
| 107 | + direction = "send", |
| 108 | + message = mock_device:generate_test_message("PhaseA", capabilities.currentMeasurement.current({ value = 0.34, unit = "A" })) |
| 109 | + } |
| 110 | + } |
| 111 | +) |
| 112 | + |
| 113 | +test.register_message_test( |
| 114 | + "RMSVoltage Report for PhaseA should be handled", |
| 115 | + { |
| 116 | + { |
| 117 | + channel = "zigbee", |
| 118 | + direction = "receive", |
| 119 | + message = { mock_device.id, ElectricalMeasurement.attributes.RMSVoltage:build_test_attr_report(mock_device, |
| 120 | + 22000) }, |
| 121 | + }, |
| 122 | + { |
| 123 | + channel = "capability", |
| 124 | + direction = "send", |
| 125 | + message = mock_device:generate_test_message("PhaseA", capabilities.voltageMeasurement.voltage({ value = 220.0, unit = "V" })) |
| 126 | + } |
| 127 | + } |
| 128 | +) |
| 129 | + |
| 130 | +test.register_coroutine_test( |
| 131 | + "Device configure lifecycle event should configure device properly", |
| 132 | + function() |
| 133 | + test.socket.zigbee:__set_channel_ordering("relaxed") |
| 134 | + test.socket.device_lifecycle:__queue_receive({ mock_device.id, "doConfigure" }) |
| 135 | + test.socket.zigbee:__expect_send({ |
| 136 | + mock_device.id, |
| 137 | + zigbee_test_utils.build_bind_request(mock_device, zigbee_test_utils.mock_hub_eui, SimpleMetering.ID) |
| 138 | + }) |
| 139 | + test.socket.zigbee:__expect_send({ |
| 140 | + mock_device.id, |
| 141 | + zigbee_test_utils.build_bind_request(mock_device, zigbee_test_utils.mock_hub_eui, ElectricalMeasurement.ID) |
| 142 | + }) |
| 143 | + test.socket.zigbee:__expect_send({ |
| 144 | + mock_device.id, |
| 145 | + SimpleMetering.attributes.CurrentSummationDelivered:configure_reporting(mock_device, 30, 120, 0) |
| 146 | + }) |
| 147 | + test.socket.zigbee:__expect_send({ |
| 148 | + mock_device.id, |
| 149 | + ElectricalMeasurement.attributes.ActivePower:configure_reporting(mock_device, 30, 120, 0) |
| 150 | + }) |
| 151 | + test.socket.zigbee:__expect_send({ |
| 152 | + mock_device.id, |
| 153 | + ElectricalMeasurement.attributes.RMSVoltage:configure_reporting(mock_device, 30, 120, 0) |
| 154 | + }) |
| 155 | + test.socket.zigbee:__expect_send({ |
| 156 | + mock_device.id, |
| 157 | + ElectricalMeasurement.attributes.RMSCurrent:configure_reporting(mock_device, 30, 120, 0) |
| 158 | + }) |
| 159 | + test.socket.zigbee:__expect_send({ |
| 160 | + mock_device.id, |
| 161 | + cluster_base.configure_reporting(mock_device, data_types.ClusterId(SimpleMetering.ID), data_types.AttributeId(0x0001), data_types.ZigbeeDataType(SimpleMetering.attributes.CurrentSummationDelivered.base_type.ID), 30, 120, 0) |
| 162 | + }) |
| 163 | + test.socket.zigbee:__expect_send({ |
| 164 | + mock_device.id, |
| 165 | + ElectricalMeasurement.attributes.ACPowerDivisor:configure_reporting(mock_device, 1, 43200, 1) |
| 166 | + }) |
| 167 | + test.socket.zigbee:__expect_send({ |
| 168 | + mock_device.id, |
| 169 | + SimpleMetering.attributes.InstantaneousDemand:configure_reporting(mock_device, 5, 3600, 5) |
| 170 | + }) |
| 171 | + test.socket.zigbee:__expect_send({ |
| 172 | + mock_device.id, |
| 173 | + ElectricalMeasurement.attributes.ACPowerMultiplier:configure_reporting(mock_device, 1, 43200, 1) |
| 174 | + }) |
| 175 | + test.socket.zigbee:__expect_send({ |
| 176 | + mock_device.id, |
| 177 | + SimpleMetering.attributes.InstantaneousDemand:read(mock_device) |
| 178 | + }) |
| 179 | + test.socket.zigbee:__expect_send({ |
| 180 | + mock_device.id, |
| 181 | + cluster_base.read_attribute(mock_device, data_types.ClusterId(SimpleMetering.ID), data_types.AttributeId(0x0001)) |
| 182 | + }) |
| 183 | + test.socket.zigbee:__expect_send({ |
| 184 | + mock_device.id, |
| 185 | + SimpleMetering.attributes.CurrentSummationDelivered:read(mock_device) |
| 186 | + }) |
| 187 | + test.socket.zigbee:__expect_send({ |
| 188 | + mock_device.id, |
| 189 | + ElectricalMeasurement.attributes.ActivePower:read(mock_device) |
| 190 | + }) |
| 191 | + test.socket.zigbee:__expect_send({ |
| 192 | + mock_device.id, |
| 193 | + ElectricalMeasurement.attributes.RMSVoltage:read(mock_device) |
| 194 | + }) |
| 195 | + test.socket.zigbee:__expect_send({ |
| 196 | + mock_device.id, |
| 197 | + ElectricalMeasurement.attributes.RMSCurrent:read(mock_device) |
| 198 | + }) |
| 199 | + test.socket.zigbee:__expect_send({ |
| 200 | + mock_device.id, |
| 201 | + ElectricalMeasurement.attributes.ACPowerMultiplier:read(mock_device) |
| 202 | + }) |
| 203 | + test.socket.zigbee:__expect_send({ |
| 204 | + mock_device.id, |
| 205 | + ElectricalMeasurement.attributes.ACPowerDivisor:read(mock_device) |
| 206 | + }) |
| 207 | + mock_device:expect_metadata_update({ provisioning_state = "PROVISIONED" }) |
| 208 | + end |
| 209 | +) |
| 210 | + |
| 211 | +test.run_registered_tests() |
0 commit comments