Skip to content

Commit 75ef0dc

Browse files
committed
move consts to switch_utils
1 parent 754ff0b commit 75ef0dc

6 files changed

Lines changed: 32 additions & 34 deletions

File tree

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@
44
local capabilities = require "st.capabilities"
55
local clusters = require "st.zigbee.zcl.clusters"
66
local utils = require "st.utils"
7-
local KELVIN_MAX = "_max_kelvin"
8-
local KELVIN_MIN = "_min_kelvin"
9-
local MIREDS_CONVERSION_CONSTANT = 1000000
10-
local COLOR_TEMPERATURE_KELVIN_MAX = 15000
11-
local COLOR_TEMPERATURE_KELVIN_MIN = 1000
12-
local COLOR_TEMPERATURE_MIRED_MAX = utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MIN) -- 1000
13-
local COLOR_TEMPERATURE_MIRED_MIN = utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MAX) -- 67
7+
local switch_utils = require "switch_utils"
148

159
local function color_temp_min_mireds_handler(driver, device, value, zb_rx)
1610
local temp_in_mired = value.value
1711
local endpoint = zb_rx.address_header.src_endpoint.value
1812
if temp_in_mired == nil then
1913
return
2014
end
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))
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))
2317
return
2418
end
25-
local temp_in_kelvin = utils.round(MIREDS_CONVERSION_CONSTANT / temp_in_mired)
26-
device:set_field(KELVIN_MAX..endpoint, temp_in_kelvin)
27-
local min = device:get_field(KELVIN_MIN..endpoint)
19+
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)
21+
local min = device:get_field(switch_utils.KELVIN_MIN..endpoint)
2822
if min ~= nil then
2923
if temp_in_kelvin > min then
3024
device:emit_event_for_endpoint(endpoint, capabilities.colorTemperature.colorTemperatureRange({ value = {minimum = min, maximum = temp_in_kelvin}}))
@@ -40,13 +34,13 @@ local function color_temp_max_mireds_handler(driver, device, value, zb_rx)
4034
if temp_in_mired == nil then
4135
return
4236
end
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))
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))
4539
return
4640
end
47-
local temp_in_kelvin = utils.round(MIREDS_CONVERSION_CONSTANT / temp_in_mired)
48-
device:set_field(KELVIN_MIN..endpoint, temp_in_kelvin)
49-
local max = device:get_field(KELVIN_MAX..endpoint)
41+
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)
43+
local max = device:get_field(switch_utils.KELVIN_MAX..endpoint)
5044
if max ~= nil then
5145
if temp_in_kelvin < max then
5246
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: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
local capabilities = require "st.capabilities"
55
local st_utils = require "st.utils"
66
local clusters = require "st.zigbee.zcl.clusters"
7-
8-
local KELVIN_MAX = "_max_kelvin"
9-
local KELVIN_MIN = "_min_kelvin"
10-
local MIREDS_CONVERSION_CONSTANT = 1000000
11-
local COLOR_TEMPERATURE_KELVIN_MAX = 15000
12-
local COLOR_TEMPERATURE_KELVIN_MIN = 1000
13-
local COLOR_TEMPERATURE_MIRED_MAX = st_utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MIN) -- 1000
14-
local COLOR_TEMPERATURE_MIRED_MIN = st_utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MAX) -- 67
7+
local switch_utils = require "switch_utils"
158

169
-- Transition Time: The time that shall be taken to perform the step change, in units of 1/10ths of a second.
1710
local TRANSITION_TIME = 3 -- default: 0.3 seconds
@@ -23,6 +16,7 @@ local function step_color_temperature_by_percent_handler(driver, device, cmd)
2316
local step_percent_change = cmd.args and cmd.args.stepSize or 0
2417
if step_percent_change == 0 then return end
2518
local step_mode = step_percent_change > 0 and clusters.ColorControl.types.CcStepMode.DOWN or clusters.ColorControl.types.CcStepMode.UP
19+
2620
local color_temp_range = device:get_latest_state("main", capabilities.colorTemperature.ID, capabilities.colorTemperature.colorTemperatureRange.NAME);
2721
local min_mireds = (color_temp_range and (device:get_field(KELVIN_MIN) or st_utils.round(MIREDS_CONVERSION_CONSTANT/color_temp_range.maximum))) or COLOR_TEMPERATURE_MIRED_MIN
2822
local max_mireds = (color_temp_range and (device:get_field(KELVIN_MAX) or st_utils.round(MIREDS_CONVERSION_CONSTANT/color_temp_range.minimum))) or COLOR_TEMPERATURE_MIRED_MAX

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
-- Copyright 2025 SmartThings, Inc.
22
-- Licensed under the Apache License, Version 2.0
3+
local utils = require "st.utils"
34

45
local switch_utils = {}
56

7+
switch_utils.KELVIN_MAX = "_max_kelvin"
8+
switch_utils.KELVIN_MIN = "_min_kelvin"
9+
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
14+
615
switch_utils.emit_event_if_latest_state_missing = function(device, component, capability, attribute_name, value)
716
if device:get_latest_state(component, capability.ID, attribute_name) == nil then
817
device:emit_event(value)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local zigbee_test_utils = require "integration_test.zigbee_test_utils"
1414
local messages = require "st.zigbee.messages"
1515
local config_reporting_response = require "st.zigbee.zcl.global_commands.configure_reporting_response"
1616
local zb_const = require "st.zigbee.constants"
17+
local switch_utils = require "switch_utils"
1718
local zcl_messages = require "st.zigbee.zcl"
1819
local data_types = require "st.zigbee.data_types"
1920
local Status = require "st.zigbee.generated.types.ZclStatus"
@@ -312,7 +313,7 @@ test.register_message_test(
312313
direction = "send",
313314
message = {
314315
mock_device.id,
315-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, zb_const.COLOR_TEMPERATURE_MIRED_MIN, zb_const.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
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)
316317
},
317318
},
318319
{
@@ -328,7 +329,7 @@ test.register_message_test(
328329
direction = "send",
329330
message = {
330331
mock_device.id,
331-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 840, 3, zb_const.COLOR_TEMPERATURE_MIRED_MIN, zb_const.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
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)
332333
},
333334
},
334335
{
@@ -344,7 +345,7 @@ test.register_message_test(
344345
direction = "send",
345346
message = {
346347
mock_device.id,
347-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 467, 3, zb_const.COLOR_TEMPERATURE_MIRED_MIN, zb_const.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
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)
348349
},
349350
}
350351
}

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

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

1010
local OnOff = clusters.OnOff
@@ -341,7 +341,7 @@ test.register_message_test(
341341
direction = "send",
342342
message = {
343343
mock_device.id,
344-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, constants.COLOR_TEMPERATURE_MIRED_MIN, constants.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
344+
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)
345345
},
346346
},
347347
{
@@ -357,7 +357,7 @@ test.register_message_test(
357357
direction = "send",
358358
message = {
359359
mock_device.id,
360-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 840, 3, constants.COLOR_TEMPERATURE_MIRED_MIN, constants.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
360+
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)
361361
},
362362
},
363363
{
@@ -373,7 +373,7 @@ test.register_message_test(
373373
direction = "send",
374374
message = {
375375
mock_device.id,
376-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.UP, 467, 3, constants.COLOR_TEMPERATURE_MIRED_MIN, constants.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
376+
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)
377377
},
378378
}
379379
}

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

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

1010
local OnOff = clusters.OnOff
@@ -191,7 +191,7 @@ test.register_coroutine_test(
191191
test.socket.zigbee:__expect_send(
192192
{
193193
mock_device.id,
194-
ColorControl.server.commands.StepColorTemperature(mock_device, ColorControl.types.CcStepMode.DOWN, 187, 3, zb_const.COLOR_TEMPERATURE_MIRED_MIN, zb_const.COLOR_TEMPERATURE_MIRED_MAX, 0x01, 0x00)
194+
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)
195195
}
196196
)
197197
test.wait_for_events()

0 commit comments

Comments
 (0)