diff --git a/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer-switch.yml b/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer-switch.yml index b62e0ae1e6..3490d307a2 100644 --- a/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer-switch.yml +++ b/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer-switch.yml @@ -8,3 +8,6 @@ components: version: 1 categories: - name: Switch +preferences: + - preferenceId: certifiedpreferences.forceStateChange + explicit: true diff --git a/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer.yml b/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer.yml index 2d5ebfb578..4784e93bb0 100644 --- a/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer.yml +++ b/drivers/SmartThings/virtual-switch/profiles/virtual-dimmer.yml @@ -6,3 +6,6 @@ components: version: 1 categories: - name: Switch +preferences: + - preferenceId: certifiedpreferences.forceStateChange + explicit: true diff --git a/drivers/SmartThings/virtual-switch/profiles/virtual-switch.yml b/drivers/SmartThings/virtual-switch/profiles/virtual-switch.yml index 4347fde8d0..f6796dd220 100644 --- a/drivers/SmartThings/virtual-switch/profiles/virtual-switch.yml +++ b/drivers/SmartThings/virtual-switch/profiles/virtual-switch.yml @@ -6,3 +6,6 @@ components: version: 1 categories: - name: Switch +preferences: + - preferenceId: certifiedpreferences.forceStateChange + explicit: true diff --git a/drivers/SmartThings/virtual-switch/src/init.lua b/drivers/SmartThings/virtual-switch/src/init.lua index c140b4eb3f..116427a6ad 100644 --- a/drivers/SmartThings/virtual-switch/src/init.lua +++ b/drivers/SmartThings/virtual-switch/src/init.lua @@ -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", { diff --git a/drivers/SmartThings/virtual-switch/src/test/test_virtual_switch.lua b/drivers/SmartThings/virtual-switch/src/test/test_virtual_switch.lua index 498c716f59..a42dfc4a1f 100644 --- a/drivers/SmartThings/virtual-switch/src/test/test_virtual_switch.lua +++ b/drivers/SmartThings/virtual-switch/src/test/test_virtual_switch.lua @@ -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() @@ -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()