Skip to content

Commit

Permalink
more correct fix
Browse files Browse the repository at this point in the history
  • Loading branch information
greens committed May 16, 2023
1 parent f98df62 commit 6c1475c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 37 deletions.
42 changes: 14 additions & 28 deletions drivers/SmartThings/zwave-switch/src/fibaro-single-switch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.

local ButtonDefaults = require "st.zwave.defaults.button"
local EnergyMeterDefaults = require "st.zwave.defaults.energyMeter"
local PowerMeterDefaults = require "st.zwave.defaults.powerMeter"
local SwitchDefaults = require "st.zwave.defaults.switch"
--- @type st.zwave.CommandClass
local cc = require "st.zwave.CommandClass"
--- @type st.zwave.CommandClass.CentralScene
local CentralScene = (require "st.zwave.CommandClass.CentralScene")({ version=1 })
--- @type st.zwave.CommandClass.Meter
local Meter = (require "st.zwave.CommandClass.Meter")({ version=3 })
local SwitchDefaults = require "st.zwave.defaults.switch"
--- @type st.zwave.CommandClass.SwitchBinary
local SwitchBinary = (require "st.zwave.CommandClass.SwitchBinary")({ version = 2 })

Expand All @@ -31,6 +24,14 @@ local FIBARO_SINGLE_SWITCH_FINGERPRINTS = {
{mfr = 0x010F, prod = 0x0403, model = 0x3000} -- Fibaro Switch
}

local function endpoint_to_component(device, ep)
return "main"
end

local function switch_binary_report_handler(self, device, cmd)
SwitchDefaults.zwave_handlers[cc.SWITCH_BINARY][SwitchBinary.REPORT](self, device, cmd)
end

local function can_handle_fibaro_single_switch(opts, driver, device, ...)
for _, fingerprint in ipairs(FIBARO_SINGLE_SWITCH_FINGERPRINTS) do
if device:id_match(fingerprint.mfr, fingerprint.prod, fingerprint.model) then
Expand All @@ -40,31 +41,16 @@ local function can_handle_fibaro_single_switch(opts, driver, device, ...)
return false
end

local function central_scene_notification_handler(self, device, cmd)
ButtonDefaults.zwave_handlers[cc.CENTRAL_SCENE][CentralScene.NOTIFICATION](self, device, cmd)
end

local function meter_report_handler(self, device, cmd)
if cmd.args.scale == Meter.scale.electric_meter.KILOWATT_HOURS then
EnergyMeterDefaults.zwave_handlers[cc.METER][Meter.REPORT](self, device, cmd)
elseif cmd.args.scale == Meter.scale.electric_meter.WATTS then
PowerMeterDefaults.zwave_handlers[cc.METER][Meter.REPORT](self, device, cmd)
end
end

local function switch_binary_report_handler(self, device, cmd)
SwitchDefaults.zwave_handlers[cc.SWITCH_BINARY][SwitchBinary.REPORT](self, device, cmd)
local function device_init(driver, device)
device:set_endpoint_to_component_fn(endpoint_to_component)
end

local fibaro_single_switch = {
NAME = "fibaro single switch",
lifecycle_handlers = {
init = device_init
},
zwave_handlers = {
[cc.CENTRAL_SCENE] = {
[CentralScene.NOTIFICATION] = central_scene_notification_handler
},
[cc.METER] = {
[Meter.REPORT] = meter_report_handler
},
[cc.SWITCH_BINARY] = {
[SwitchBinary.REPORT] = switch_binary_report_handler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test.register_message_test(
)

test.register_message_test(
"Switch Binary report ON_ENABLE from source channel 2 should be discarded",
"Switch Binary report ON_ENABLE from source channel 2 should be treated as from main",
{
{
channel = "device_lifecycle",
Expand All @@ -104,6 +104,11 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.switch.switch.on())
}
}
)
Expand Down Expand Up @@ -139,7 +144,7 @@ test.register_message_test(
)

test.register_message_test(
"Switch Binary report OFF_DISABLE from source channel 2 should be discarded",
"Switch Binary report OFF_DISABLE from source channel 2 should be treated as from main",
{
{
channel = "device_lifecycle",
Expand All @@ -164,6 +169,11 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.switch.switch.off())
}
}
)
Expand Down Expand Up @@ -196,7 +206,7 @@ test.register_message_test(
)

test.register_message_test(
"Central Scene notification KEY_PRESSED_1_TIME attribute from source channel 2 should be discared",
"Central Scene notification KEY_PRESSED_1_TIME attribute from source channel 2 should treated as from main",
{
{
channel = "zwave",
Expand All @@ -217,6 +227,12 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.button.button.pushed({
state_change = true }))
}
}
)
Expand All @@ -242,7 +258,7 @@ test.register_message_test(
)

test.register_message_test(
"Central Scene notification KEY_RELEASED attribute from source channel 2 should be discared",
"Central Scene notification KEY_RELEASED attribute from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -263,6 +279,12 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.button.button.held({
state_change = true }))
}
}
)
Expand All @@ -288,7 +310,7 @@ test.register_message_test(
)

test.register_message_test(
"Central Scene notification KEY_HELD_DOWN attribute from source channel 2 should be discared",
"Central Scene notification KEY_HELD_DOWN attribute from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -309,6 +331,12 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.button.button.down_hold({
state_change = true }))
}
}
)
Expand All @@ -334,7 +362,7 @@ test.register_message_test(
)

test.register_message_test(
"Central Scene notification KEY_PRESSED_2_TIMES attribute from source channel 2 should be discared",
"Central Scene notification KEY_PRESSED_2_TIMES attribute from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -355,6 +383,12 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.button.button.double({
state_change = true }))
}
}
)
Expand All @@ -380,7 +414,7 @@ test.register_message_test(
)

test.register_message_test(
"Central Scene notification KEY_PRESSED_3_TIMES attribute from source channel 2 should be discared",
"Central Scene notification KEY_PRESSED_3_TIMES attribute from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -401,6 +435,12 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.button.button.pushed_3x({
state_change = true }))
}
}
)
Expand All @@ -425,7 +465,7 @@ test.register_message_test(
)

test.register_message_test(
"Energy meter report from source channel 2 should be discarded",
"Energy meter report from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -447,6 +487,11 @@ test.register_message_test(
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.energyMeter.energy({ value = 5, unit = "kWh" }))
}
}
)

Expand All @@ -470,7 +515,7 @@ test.register_message_test(
)

test.register_message_test(
"Power meter report from source channel 2 should be discarded",
"Power meter report from source channel 2 should be treated as from main",
{
{
channel = "zwave",
Expand All @@ -491,6 +536,11 @@ test.register_message_test(
)
)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.powerMeter.power({ value = 5, unit = "W" }))
}
}
)
Expand Down

0 comments on commit 6c1475c

Please sign in to comment.