Skip to content

Commit 535962c

Browse files
committed
add: statelessSwitchLevelStep and statelessColorTemperatureStep handling
1 parent f5f1479 commit 535962c

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

  • drivers/SmartThings/zigbee-switch/src/stateless_handlers

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,44 @@
22
-- Licensed under the Apache License, Version 2.0
33

44
local capabilities = require "st.capabilities"
5+
local st_utils = require "st.utils"
6+
local clusters = require "st.zigbee.zcl.clusters"
57
local log = require "log"
68

9+
-- The following definitions should be pulled from the `st.zigbee.constants` module once those changes land
10+
local KELVIN_MAX = "_max_kelvin"
11+
local KELVIN_MIN = "_min_kelvin"
12+
local MIREDS_CONVERSION_CONSTANT = 1000000
13+
local COLOR_TEMPERATURE_KELVIN_MAX = 15000
14+
local COLOR_TEMPERATURE_KELVIN_MIN = 1000
15+
local COLOR_TEMPERATURE_MIRED_MAX = st_utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MIN) -- 1000
16+
local COLOR_TEMPERATURE_MIRED_MIN = st_utils.round(MIREDS_CONVERSION_CONSTANT/COLOR_TEMPERATURE_KELVIN_MAX) -- 67
17+
18+
-- Transition Time: The time that shall be taken to perform the step change, in units of 1/10ths of a second.
19+
local TRANSITION_TIME = 3 -- default: 0.3 seconds
20+
-- Options Mask & Override: Indicates which options are being overriden by the Level/ColorControl cluster commands
21+
local OPTIONS_MASK = 0x01 -- default: The `ExecuteIfOff` option is overriden
22+
local IGNORE_COMMAND_IF_OFF = 0x00 -- default: the command will not be executed if the device is off
23+
724
local function step_color_temperature_by_percent_handler(driver, device, cmd)
8-
log.info_with({ hub_logs = true }, string.format("handling stepColorTemperatureByPercent command for device %s with args: %s", device.id, cmd.args))
25+
local step_percent_change = cmd.args and cmd.args.stepSize or 0
26+
if step_percent_change == 0 then return end
27+
local step_mode = step_percent_change > 0 and (clusters.ColorControl.types.CcStepMode and clusters.ColorControl.types.CcStepMode.DOWN or 3) or (clusters.ColorControl.types.CcStepMode and clusters.ColorControl.types.CcStepMode.UP or 1)
28+
local min_mireds = device:get_field(KELVIN_MIN) or COLOR_TEMPERATURE_MIRED_MIN -- default min mireds
29+
local max_mireds = device:get_field(KELVIN_MAX) or COLOR_TEMPERATURE_MIRED_MAX -- default max mireds
30+
local step_size_in_mireds = st_utils.round((max_mireds - min_mireds) * (math.abs(step_percent_change)/100.0))
31+
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))
932
end
1033

1134
local function step_level_handler(driver, device, cmd)
12-
log.info_with({ hub_logs = true }, string.format("handling stepLevel command for device %s with args: %s", device.id, cmd.args))
35+
local step_size = st_utils.round((cmd.args and cmd.args.stepSize or 0)/100.0 * 254)
36+
if step_size == 0 then return end
37+
local step_mode = step_size > 0 and clusters.Level.types.MoveStepMode.UP or clusters.Level.types.MoveStepMode.DOWN
38+
device:send(clusters.Level.server.commands.Step(device, step_mode, math.abs(step_size), TRANSITION_TIME, OPTIONS_MASK, IGNORE_COMMAND_IF_OFF))
1339
end
1440

1541
local stateless_handlers = {
16-
Name = "Stateless Step Handlers",
42+
Name = "Zigbee Stateless Step Handlers",
1743
capability_handlers = {
1844
[capabilities.statelessColorTemperatureStep.ID] = {
1945
[capabilities.statelessColorTemperatureStep.commands.stepColorTemperatureByPercent.NAME] = step_color_temperature_by_percent_handler,

0 commit comments

Comments
 (0)