Skip to content

Commit 555e4e3

Browse files
committed
update tests, remove endpoint keying, move variables
1 parent 586c35f commit 555e4e3

6 files changed

Lines changed: 77 additions & 49 deletions

File tree

drivers/SmartThings/zigbee-switch/src/color_temp_range_handlers/init.lua

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@ local clusters = require "st.zigbee.zcl.clusters"
66
local utils = require "st.utils"
77
local switch_utils = require "switch_utils"
88

9+
-- These values are a "sanity check" to ensure that max/min values we are getting are reasonable
10+
local COLOR_TEMPERATURE_KELVIN_MAX = 15000
11+
local COLOR_TEMPERATURE_KELVIN_MIN = 1000
12+
local COLOR_TEMPERATURE_MIRED_MAX = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MIN) -- 1000 Mireds
13+
local COLOR_TEMPERATURE_MIRED_MIN = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MAX) -- 67 Mireds
14+
915
local function color_temp_min_mireds_handler(driver, device, value, zb_rx)
1016
local temp_in_mired = value.value
1117
local endpoint = zb_rx.address_header.src_endpoint.value
1218
if temp_in_mired == nil then
1319
return
1420
end
15-
if (temp_in_mired < switch_utils.COLOR_TEMPERATURE_MIRED_MIN or temp_in_mired > switch_utils.COLOR_TEMPERATURE_MIRED_MAX) then
16-
device.log.warn_with({hub_logs = true}, string.format("Device reported a color temperature %d mired outside of sane range of %.2f-%.2f", temp_in_mired, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX))
21+
if (temp_in_mired < COLOR_TEMPERATURE_MIRED_MIN or temp_in_mired > COLOR_TEMPERATURE_MIRED_MAX) then
22+
device.log.warn_with({hub_logs = true}, string.format("Device reported a color temperature %d mired outside of sane range of %.2f-%.2f", temp_in_mired, COLOR_TEMPERATURE_MIRED_MIN, COLOR_TEMPERATURE_MIRED_MAX))
1723
return
1824
end
1925
local temp_in_kelvin = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / temp_in_mired)
20-
device:set_field(switch_utils.KELVIN_MAX..endpoint, temp_in_kelvin, {persist = true})
21-
local min = device:get_field(switch_utils.KELVIN_MIN..endpoint)
26+
device:set_field(switch_utils.KELVIN_MAX, temp_in_kelvin, {persist = true})
27+
local min = device:get_field(switch_utils.KELVIN_MIN)
2228
if min ~= nil then
2329
if temp_in_kelvin > min then
2430
device:emit_event_for_endpoint(endpoint, capabilities.colorTemperature.colorTemperatureRange({ value = {minimum = min, maximum = temp_in_kelvin}}))
@@ -34,13 +40,13 @@ local function color_temp_max_mireds_handler(driver, device, value, zb_rx)
3440
if temp_in_mired == nil then
3541
return
3642
end
37-
if (temp_in_mired < switch_utils.COLOR_TEMPERATURE_MIRED_MIN or temp_in_mired > switch_utils.COLOR_TEMPERATURE_MIRED_MAX) then
38-
device.log.warn_with({hub_logs = true}, string.format("Device reported a color temperature %d mired outside of sane range of %.2f-%.2f", temp_in_mired, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX))
43+
if (temp_in_mired < COLOR_TEMPERATURE_MIRED_MIN or temp_in_mired > COLOR_TEMPERATURE_MIRED_MAX) then
44+
device.log.warn_with({hub_logs = true}, string.format("Device reported a color temperature %d mired outside of sane range of %.2f-%.2f", temp_in_mired, COLOR_TEMPERATURE_MIRED_MIN, COLOR_TEMPERATURE_MIRED_MAX))
3945
return
4046
end
4147
local temp_in_kelvin = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / temp_in_mired)
42-
device:set_field(switch_utils.KELVIN_MIN..endpoint, temp_in_kelvin, {persist = true})
43-
local max = device:get_field(switch_utils.KELVIN_MAX..endpoint)
48+
device:set_field(switch_utils.KELVIN_MIN, temp_in_kelvin, {persist = true})
49+
local max = device:get_field(switch_utils.KELVIN_MAX)
4450
if max ~= nil then
4551
if temp_in_kelvin < max then
4652
device:emit_event_for_endpoint(endpoint, capabilities.colorTemperature.colorTemperatureRange({ value = {minimum = temp_in_kelvin, maximum = max}}))

drivers/SmartThings/zigbee-switch/src/stateless_handlers/init.lua

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,33 @@ local st_utils = require "st.utils"
66
local clusters = require "st.zigbee.zcl.clusters"
77
local switch_utils = require "switch_utils"
88

9+
-- These values are the config bounds in the default profiles
10+
local DEFAULT_KELVIN_MIN = 2200 -- 455 Mireds
11+
local DEFAULT_KELVIN_MAX = 6500 -- 154 Mireds
12+
913
-- Transition Time: The time that shall be taken to perform the step change, in units of 1/10ths of a second.
1014
local TRANSITION_TIME = 3 -- default: 0.3 seconds
15+
1116
-- Options Mask & Override: Indicates which options are being overriden by the Level/ColorControl cluster commands
1217
local OPTIONS_MASK = 0x01 -- default: The `ExecuteIfOff` option is overriden
1318
local IGNORE_COMMAND_IF_OFF = 0x00 -- default: the command will not be executed if the device is off
1419

1520
local function step_color_temperature_by_percent_handler(driver, device, cmd)
1621
local step_percent_change = cmd.args and cmd.args.stepSize or 0
1722
if step_percent_change == 0 then return end
18-
local step_mode = step_percent_change > 0 and clusters.ColorControl.types.CcStepMode.DOWN or clusters.ColorControl.types.CcStepMode.UP
19-
20-
local color_temp_range = device:get_latest_state("main", capabilities.colorTemperature.ID, capabilities.colorTemperature.colorTemperatureRange.NAME);
21-
local kelvin_min = device:get_field(switch_utils.KELVIN_MIN);
22-
local kelvin_max = device:get_field(switch_utils.KELVIN_MAX);
23-
24-
local min_mireds
25-
local max_mireds
26-
if color_temp_range then
27-
-- First tier: use color_temp_range if available
28-
min_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / color_temp_range.maximum)
29-
max_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / color_temp_range.minimum)
30-
elseif kelvin_min and kelvin_max then
31-
-- Second tier: use device values if available
32-
min_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / kelvin_max)
33-
max_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / kelvin_min)
34-
else
35-
-- Third tier: use defaults
36-
min_mireds = switch_utils.COLOR_TEMPERATURE_MIRED_MIN
37-
max_mireds = switch_utils.COLOR_TEMPERATURE_MIRED_MAX
38-
end
39-
23+
-- Reminder, stepSize > 0 == Kelvin UP == Mireds DOWN. stepSize < 0 == Kelvin DOWN == Mireds UP
24+
local step_mode = (step_percent_change > 0) and clusters.ColorControl.types.CcStepMode.DOWN or clusters.ColorControl.types.CcStepMode.UP
25+
-- Convert given or default kelvin max and min into mireds
26+
local min_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / (device:get_field(switch_utils.KELVIN_MAX) or DEFAULT_KELVIN_MAX))
27+
local max_mireds = st_utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT / (device:get_field(switch_utils.KELVIN_MIN) or DEFAULT_KELVIN_MIN))
4028
local step_size_in_mireds = st_utils.round((max_mireds - min_mireds) * (math.abs(step_percent_change)/100.0))
4129
device:send(clusters.ColorControl.server.commands.StepColorTemperature(device, step_mode, step_size_in_mireds, TRANSITION_TIME, min_mireds, max_mireds, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF))
4230
end
4331

4432
local function step_level_handler(driver, device, cmd)
4533
local step_size = st_utils.round((cmd.args and cmd.args.stepSize or 0)/100.0 * 254)
4634
if step_size == 0 then return end
47-
local step_mode = step_size > 0 and clusters.Level.types.MoveStepMode.UP or clusters.Level.types.MoveStepMode.DOWN
35+
local step_mode = (step_size > 0) and clusters.Level.types.MoveStepMode.UP or clusters.Level.types.MoveStepMode.DOWN
4836
device:send(clusters.Level.server.commands.Step(device, step_mode, math.abs(step_size), TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF))
4937
end
5038

drivers/SmartThings/zigbee-switch/src/switch_utils.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ local switch_utils = {}
66

77
switch_utils.KELVIN_MAX = "_max_kelvin"
88
switch_utils.KELVIN_MIN = "_min_kelvin"
9+
910
switch_utils.MIREDS_CONVERSION_CONSTANT = 1000000
10-
switch_utils.COLOR_TEMPERATURE_KELVIN_MAX = 15000
11-
switch_utils.COLOR_TEMPERATURE_KELVIN_MIN = 1000
12-
switch_utils.COLOR_TEMPERATURE_MIRED_MAX = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT/switch_utils.COLOR_TEMPERATURE_KELVIN_MIN) -- 1000
13-
switch_utils.COLOR_TEMPERATURE_MIRED_MIN = utils.round(switch_utils.MIREDS_CONVERSION_CONSTANT/switch_utils.COLOR_TEMPERATURE_KELVIN_MAX) -- 67
1411

1512
switch_utils.emit_event_if_latest_state_missing = function(device, component, capability, attribute_name, value)
1613
if device:get_latest_state(component, capability.ID, attribute_name) == nil then

drivers/SmartThings/zigbee-switch/src/test/test_all_capability_zigbee_bulb.lua

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ test.register_message_test(
297297
}
298298
)
299299

300+
local DEFAULT_MIRED_MAX = 455
301+
local DEFAULT_MIRED_MIN = 154
302+
local TRANSITION_TIME = 3
303+
local OPTIONS_MASK = 0x01
304+
local IGNORE_COMMAND_IF_OFF = 0x00
305+
300306
test.register_message_test(
301307
"Step ColorTemperature command test",
302308
{
@@ -313,7 +319,7 @@ test.register_message_test(
313319
direction = "send",
314320
message = {
315321
mock_device.id,
316-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
322+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 60, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
317323
},
318324
},
319325
{
@@ -329,7 +335,7 @@ test.register_message_test(
329335
direction = "send",
330336
message = {
331337
mock_device.id,
332-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 840, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
338+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 271, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
333339
},
334340
},
335341
{
@@ -345,7 +351,7 @@ test.register_message_test(
345351
direction = "send",
346352
message = {
347353
mock_device.id,
348-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 467, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
354+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 151, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
349355
},
350356
}
351357
},
@@ -370,7 +376,7 @@ test.register_message_test(
370376
direction = "send",
371377
message = {
372378
mock_device.id,
373-
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.UP, 64, 3, 0x01, 0x00)
379+
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.UP, 64, TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
374380
},
375381
},
376382
{
@@ -386,7 +392,7 @@ test.register_message_test(
386392
direction = "send",
387393
message = {
388394
mock_device.id,
389-
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.DOWN, 127, 3, 0x01, 0x00)
395+
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.DOWN, 127, TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
390396
}
391397
},
392398
{
@@ -402,7 +408,7 @@ test.register_message_test(
402408
direction = "send",
403409
message = {
404410
mock_device.id,
405-
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.UP, 254, 3, 0x01, 0x00)
411+
Level.server.commands.Step(mock_device, Level.types.MoveStepMode.UP, 254, TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
406412
}
407413
}
408414
},

drivers/SmartThings/zigbee-switch/src/test/test_rgbw_bulb.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ test.register_coroutine_test(
316316
}
317317
)
318318

319+
local DEFAULT_MIRED_MAX = 455
320+
local DEFAULT_MIRED_MIN = 154
321+
local TRANSITION_TIME = 3
322+
local OPTIONS_MASK = 0x01
323+
local IGNORE_COMMAND_IF_OFF = 0x00
324+
319325
test.register_message_test(
320326
"Step ColorTemperature command test",
321327
{
@@ -332,7 +338,7 @@ test.register_message_test(
332338
direction = "send",
333339
message = {
334340
mock_device.id,
335-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
341+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 60, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
336342
},
337343
},
338344
{
@@ -348,7 +354,7 @@ test.register_message_test(
348354
direction = "send",
349355
message = {
350356
mock_device.id,
351-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 840, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
357+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 271, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
352358
},
353359
},
354360
{
@@ -364,7 +370,7 @@ test.register_message_test(
364370
direction = "send",
365371
message = {
366372
mock_device.id,
367-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 467, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
373+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 151, TRANSITION_TIME, DEFAULT_MIRED_MIN, DEFAULT_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
368374
},
369375
}
370376
},

drivers/SmartThings/zigbee-switch/src/test/test_sengled_color_temp_bulb.lua

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
local test = require "integration_test"
55
local t_utils = require "integration_test.utils"
66
local clusters = require "st.zigbee.zcl.clusters"
7-
local switch_utils = require "switch_utils"
7+
local capabilities = require "st.capabilities"
88
local zigbee_test_utils = require "integration_test.zigbee_test_utils"
99

1010
local OnOff = clusters.OnOff
1111
local Level = clusters.Level
1212
local ColorControl = clusters.ColorControl
13+
local REPORTED_MIRED_MIN = 160
14+
local REPORTED_MIRED_MAX = 370
1315

1416
local mock_device = test.mock_device.build_test_zigbee_device(
1517
{ profile = t_utils.get_profile_definition("color-temp-bulb.yml"),
@@ -125,6 +127,7 @@ test.register_message_test(
125127
test.register_coroutine_test(
126128
"Set Color Temperature command test",
127129
function()
130+
test.socket.zigbee:__set_channel_ordering("relaxed")
128131
test.timer.__create_and_queue_test_time_advance_timer(1, "oneshot")
129132
test.socket.capability:__queue_receive({mock_device.id, { capability = "colorTemperature", component = "main", command = "setColorTemperature", args = { 200 } } })
130133

@@ -152,19 +155,41 @@ test.register_coroutine_test(
152155
}
153156
)
154157

158+
local TRANSITION_TIME = 3
159+
local OPTIONS_MASK = 0x01
160+
local IGNORE_COMMAND_IF_OFF = 0x00
161+
155162
test.register_coroutine_test(
156163
"Step Color Temperature command test",
157164
function()
158165
test.timer.__create_and_queue_test_time_advance_timer(1, "oneshot")
159-
test.socket.capability:__queue_receive({mock_device.id, { capability = "statelessColorTemperatureStep", component = "main", command = "stepColorTemperatureByPercent", args = { 20 } } })
166+
-- Report non-default range values to verify subsequent step commands do not use defaults.
167+
test.socket.zigbee:__queue_receive({mock_device.id, ColorControl.attributes.ColorTempPhysicalMaxMireds:build_test_attr_report(mock_device, REPORTED_MIRED_MAX)})
168+
test.socket.zigbee:__queue_receive({mock_device.id, ColorControl.attributes.ColorTempPhysicalMinMireds:build_test_attr_report(mock_device, REPORTED_MIRED_MIN)})
169+
test.socket.capability:__expect_send(mock_device:generate_test_message("main", capabilities.colorTemperature.colorTemperatureRange({minimum = 2703, maximum = 6250})))
170+
test.wait_for_events()
160171

172+
test.socket.capability:__queue_receive({mock_device.id, { capability = "statelessColorTemperatureStep", component = "main", command = "stepColorTemperatureByPercent", args = { 20 } } })
161173
test.socket.zigbee:__expect_send(
162174
{
163175
mock_device.id,
164-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, switch_utils.COLOR_TEMPERATURE_MIRED_MIN, switch_utils.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
176+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 42, TRANSITION_TIME, REPORTED_MIRED_MIN, REPORTED_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
177+
}
178+
)
179+
test.socket.capability:__queue_receive({mock_device.id, { capability = "statelessColorTemperatureStep", component = "main", command = "stepColorTemperatureByPercent", args = { 90 } } })
180+
test.socket.zigbee:__expect_send(
181+
{
182+
mock_device.id,
183+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 189, TRANSITION_TIME, REPORTED_MIRED_MIN, REPORTED_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
184+
}
185+
)
186+
test.socket.capability:__queue_receive({mock_device.id, { capability = "statelessColorTemperatureStep", component = "main", command = "stepColorTemperatureByPercent", args = { -50 } } })
187+
test.socket.zigbee:__expect_send(
188+
{
189+
mock_device.id,
190+
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 105, TRANSITION_TIME, REPORTED_MIRED_MIN, REPORTED_MIRED_MAX, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
165191
}
166192
)
167-
test.wait_for_events()
168193
end,
169194
{
170195
min_api_version = 19
@@ -180,7 +205,7 @@ test.register_coroutine_test(
180205
test.socket.zigbee:__expect_send(
181206
{
182207
mock_device.id,
183-
Level.commands.Step(mock_device, Level.types.MoveStepMode.UP, 64, 3, 0x01, 0x00)
208+
Level.commands.Step(mock_device, Level.types.MoveStepMode.UP, 64, TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF)
184209
}
185210
)
186211
test.wait_for_events()

0 commit comments

Comments
 (0)