|
1 | 1 | local test = require "integration_test.cosock_runner"
|
2 | 2 | local testenv = require "test.testenv"
|
3 | 3 |
|
| 4 | +local capabilities = require "st.capabilities" |
| 5 | +local st_utils = require "st.utils" |
| 6 | + |
| 7 | +local Consts = require "consts" |
4 | 8 | local Discovery = require "disco"
|
5 | 9 | local Fields = require "fields"
|
6 | 10 | local HueDeviceTypes = require "hue_device_types"
|
7 | 11 | local hue_utils = require "utils"
|
8 | 12 |
|
| 13 | +-- local copy of the real capability deployed for the driver. |
| 14 | +-- loaded in to the test environment in test.testenv.lua |
| 15 | +local syncCapabilityId = "samsungim.hueSyncMode" |
| 16 | +local hueSyncMode = capabilities[syncCapabilityId] |
| 17 | + |
9 | 18 | test.register_coroutine_test(
|
10 | 19 | "Test Scanning Bridge Finds Child Bulb (White Ambiance Bulb)",
|
11 | 20 | function()
|
@@ -101,6 +110,142 @@ test.register_coroutine_test(
|
101 | 110 | {}
|
102 | 111 | )
|
103 | 112 |
|
| 113 | +test.register_coroutine_test( |
| 114 | + "Test Refreshing Child Bulb Emits Events [White Ambiance Bulb: On/Off, Dimming, Color Temperature, Sync Mode]", |
| 115 | + function() |
| 116 | + local driver_under_test = test.driver_wrapper.driver_under_test |
| 117 | + |
| 118 | + test.mock_devices_api.assert_device_create_events(false) |
| 119 | + assert(testenv.mock_hue_bridge, "test init didn't create mock bridge server") |
| 120 | + |
| 121 | + local mock_bridge_st_device = testenv.create_already_onboarded_bridge_device(driver_under_test) |
| 122 | + local mock_hue_device_service = testenv.mock_hue_bridge:add_device_from_template( |
| 123 | + HueDeviceTypes.LIGHT, |
| 124 | + "test_data/templates/white-ambiance-bulb", |
| 125 | + "Test Hue White Ambiance Bulb" |
| 126 | + ) |
| 127 | + testenv.mock_hue_bridge:start() |
| 128 | + test.mock_device.add_test_device(mock_bridge_st_device) |
| 129 | + test.wait_for_events() |
| 130 | + |
| 131 | + |
| 132 | + Discovery.scan_bridge_and_update_devices( |
| 133 | + driver_under_test, |
| 134 | + mock_bridge_st_device.device_network_id |
| 135 | + ) |
| 136 | + |
| 137 | + test.wait_for_events() |
| 138 | + |
| 139 | + local devices = test.driver_wrapper.driver_under_test:get_devices() |
| 140 | + |
| 141 | + local light_device |
| 142 | + for _, device in ipairs(devices) do |
| 143 | + if device:get_field(Fields.DEVICE_TYPE) == HueDeviceTypes.LIGHT then |
| 144 | + light_device = device |
| 145 | + break |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + -- Up to this point, this has all been mostly the same as the |
| 150 | + -- previous tests's boilerplate. Coming up is the code where we |
| 151 | + -- handle the lifecycle event attribute emits. |
| 152 | + |
| 153 | + -- First we set the channel ordering to relaxed, because |
| 154 | + -- we don't want to enforce ordering. |
| 155 | + test.socket.capability:__set_channel_ordering("relaxed") |
| 156 | + |
| 157 | + -- We set up capability send expectations for all of the capabilities |
| 158 | + -- that the profile exposes because the `added` handler will emit these attributes. |
| 159 | + |
| 160 | + -- We expect an `on` because the value in the template file is on, and we |
| 161 | + -- didn't provid an override. |
| 162 | + test.socket.capability:__expect_send( |
| 163 | + light_device:generate_test_message("main", capabilities.switch.switch.on()) |
| 164 | + ) |
| 165 | + -- We expect an `100` because the value in the template file is 100. |
| 166 | + test.socket.capability:__expect_send( |
| 167 | + light_device:generate_test_message("main", capabilities.switchLevel.level(100)) |
| 168 | + ) |
| 169 | + |
| 170 | + -- We take the mirek value that's in the test data, convert it to kelvin, |
| 171 | + -- and populate the expected capability emit. We don't use the device service |
| 172 | + -- table from above because as the name indicates, it's for the root |
| 173 | + -- device service and not the light service. |
| 174 | + local min = light_device:get_field(Fields.MIN_KELVIN) or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE |
| 175 | + local kelvin = assert( math.floor( |
| 176 | + st_utils.clamp_value(hue_utils.mirek_to_kelvin(366), min, Consts.MAX_TEMP_KELVIN) |
| 177 | + )) |
| 178 | + local emit = assert(capabilities.colorTemperature.colorTemperature(kelvin)) |
| 179 | + test.socket.capability:__expect_send( |
| 180 | + light_device:generate_test_message("main", emit) |
| 181 | + ) |
| 182 | + |
| 183 | + -- We expect `"normal"` because the value in the template file is "normal". |
| 184 | + test.socket.capability:__expect_send( |
| 185 | + light_device:generate_test_message("main", hueSyncMode.mode("normal")) |
| 186 | + ) |
| 187 | + |
| 188 | + -- The added lifecycle handler will invoke the refresh handler, so we wait for the expected emits |
| 189 | + while not light_device:get_field(Fields._ADDED) do |
| 190 | + test.wait_for_events() |
| 191 | + end |
| 192 | + test.wait_for_events() |
| 193 | + |
| 194 | + -- Repeat the above for the init handler. |
| 195 | + test.socket.capability:__expect_send( |
| 196 | + light_device:generate_test_message("main", capabilities.switch.switch.on()) |
| 197 | + ) |
| 198 | + test.socket.capability:__expect_send( |
| 199 | + light_device:generate_test_message("main", capabilities.switchLevel.level(100)) |
| 200 | + ) |
| 201 | + local min = light_device:get_field(Fields.MIN_KELVIN) or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE |
| 202 | + local kelvin = assert( math.floor( |
| 203 | + st_utils.clamp_value(hue_utils.mirek_to_kelvin(366), min, Consts.MAX_TEMP_KELVIN) |
| 204 | + )) |
| 205 | + local emit = assert(capabilities.colorTemperature.colorTemperature(kelvin)) |
| 206 | + test.socket.capability:__expect_send( |
| 207 | + light_device:generate_test_message("main", emit) |
| 208 | + ) |
| 209 | + test.socket.capability:__expect_send( |
| 210 | + light_device:generate_test_message("main", hueSyncMode.mode("normal")) |
| 211 | + ) |
| 212 | + |
| 213 | + while not light_device:get_field(Fields._INIT) do |
| 214 | + test.wait_for_events() |
| 215 | + end |
| 216 | + test.wait_for_events() |
| 217 | + |
| 218 | + -- We'll queue up a receive capability command to verify that the emits happen for |
| 219 | + -- the capability command handler as well. |
| 220 | + test.socket.capability:__queue_receive( |
| 221 | + { light_device.id, |
| 222 | + { capability = "refresh", component = "main", command = "refresh", args = {} } |
| 223 | + } |
| 224 | + ) |
| 225 | + test.socket.capability:__expect_send( |
| 226 | + light_device:generate_test_message("main", capabilities.switch.switch.on()) |
| 227 | + ) |
| 228 | + test.socket.capability:__expect_send( |
| 229 | + light_device:generate_test_message("main", capabilities.switchLevel.level(100)) |
| 230 | + ) |
| 231 | + local min = light_device:get_field(Fields.MIN_KELVIN) or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE |
| 232 | + local kelvin = assert( math.floor( |
| 233 | + st_utils.clamp_value(hue_utils.mirek_to_kelvin(366), min, Consts.MAX_TEMP_KELVIN) |
| 234 | + )) |
| 235 | + local emit = assert(capabilities.colorTemperature.colorTemperature(kelvin)) |
| 236 | + test.socket.capability:__expect_send( |
| 237 | + light_device:generate_test_message("main", emit) |
| 238 | + ) |
| 239 | + |
| 240 | + test.socket.capability:__expect_send( |
| 241 | + light_device:generate_test_message("main", hueSyncMode.mode("normal")) |
| 242 | + ) |
| 243 | + |
| 244 | + test.wait_for_events() |
| 245 | + end, |
| 246 | + {} |
| 247 | +) |
| 248 | + |
104 | 249 | test.add_test_env_setup_func(testenv.driver_env_init)
|
105 | 250 | test.set_test_init_function(testenv.testenv_init)
|
106 | 251 | test.set_test_cleanup_function(testenv.testenv_cleanup)
|
|
0 commit comments