Skip to content

Commit

Permalink
Merge pull request #1639 from SmartThingsCommunity/feature/CHAD-13777
Browse files Browse the repository at this point in the history
CHAD-13777 Allow virtual device events to not always be a state change
  • Loading branch information
greens authored Sep 30, 2024
2 parents bf42ce6 + 13fa587 commit b2893b0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ components:
version: 1
categories:
- name: Switch
preferences:
- preferenceId: certifiedpreferences.forceStateChange
explicit: true
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ components:
version: 1
categories:
- name: Switch
preferences:
- preferenceId: certifiedpreferences.forceStateChange
explicit: true
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ components:
version: 1
categories:
- name: Switch
preferences:
- preferenceId: certifiedpreferences.forceStateChange
explicit: true
18 changes: 11 additions & 7 deletions drivers/SmartThings/virtual-switch/src/init.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
local capabilities = require "st.capabilities"
local Driver = require "st.driver"
local additional_fields = {
state_change = true
}

local function force_state_change(device)
if device.preferences["certifiedpreferences.forceStateChange"] then
return {state_change = true}
else
return nil
end
end

local function handle_set_level(driver, device, command)
if (command.args.level == 0) then
device:emit_event(capabilities.switch.switch.off(additional_fields))
device:emit_event(capabilities.switch.switch.off(force_state_change(device)))
else
device:emit_event(capabilities.switchLevel.level(command.args.level, additional_fields))
device:emit_event(capabilities.switchLevel.level(command.args.level, force_state_change(device)))
device:emit_event(capabilities.switch.switch.on())
end

end

local function handle_on(driver, device, command)
device:emit_event(capabilities.switch.switch.on(additional_fields))
device:emit_event(capabilities.switch.switch.on(force_state_change(device)))
end

local function handle_off(driver, device, command)
device:emit_event(capabilities.switch.switch.off(additional_fields))
device:emit_event(capabilities.switch.switch.off(force_state_change(device)))
end

local virtual_driver = Driver("virtual-switch", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ local capabilities = require "st.capabilities"
local t_utils = require "integration_test.utils"

local mock_simple_device = test.mock_device.build_test_generic_device(
{ profile = t_utils.get_profile_definition("virtual-dimmer-switch.yml") }
{
profile = t_utils.get_profile_definition("virtual-dimmer-switch.yml"),
preferences = { ["certifiedpreferences.forceStateChange"] = true },
}
)

local function test_init()
Expand Down Expand Up @@ -105,4 +108,22 @@ test.register_message_test(
}
)

test.register_coroutine_test(
"State change should not be true when forceStateChange is false",
function()
test.socket.device_lifecycle():__queue_receive({mock_simple_device.id, "init"})
test.socket.device_lifecycle():__queue_receive(mock_simple_device:generate_info_changed(
{
preferences = {
["certifiedpreferences.forceStateChange"] = false
}
}
))
test.wait_for_events()
test.socket.capability:__queue_receive({ mock_simple_device.id,
{ capability = "switch", component = "main", command = "on", args = {} } })
test.socket.capability:__expect_send(mock_simple_device:generate_test_message("main", capabilities.switch.switch.on()))
end
)

test.run_registered_tests()

0 comments on commit b2893b0

Please sign in to comment.