Skip to content

Commit

Permalink
Merge pull request SmartThingsCommunity#63 from SmartThingsCommunity/…
Browse files Browse the repository at this point in the history
…bugfix/CHAD-8368

CHAD-8368 Emit all component button events from main
  • Loading branch information
greens authored May 18, 2022
2 parents 2bc0002 + f8fbde8 commit d8e448b
Show file tree
Hide file tree
Showing 32 changed files with 334 additions and 296 deletions.
21 changes: 21 additions & 0 deletions drivers/SmartThings/zigbee-button/src/button_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,31 @@ button_utils.send_pushed_or_held_button_event_if_applicable = function(device, b
local component = device.profile.components[button_name]
if component ~= nil then
device:emit_component_event(component, event)
if button_name ~= "main" then
device:emit_event(event)
end
else
log.warn("Attempted to emit button event for non-existing component: " .. button_name)
end
end
end

button_utils.build_button_handler = function(button_name, pressed_type)
return function(driver, device, zb_rx)
local additional_fields = {
state_change = true
}
local event = pressed_type(additional_fields)
local comp = device.profile.components[button_name]
if comp ~= nil then
device:emit_component_event(comp, event)
if button_name ~= "main" then
device:emit_event(event)
end
else
log.warn("Attempted to emit button event for unknown button: " .. button_name)
end
end
end

return button_utils
15 changes: 3 additions & 12 deletions drivers/SmartThings/zigbee-button/src/dimming-remote/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,14 @@ local function button_released_handler(self, device, value, zb_rx)
button_utils.send_pushed_or_held_button_event_if_applicable(device, 2)
end

local function button_pressed_handler(button_number)
return function(self, device, value, zb_rx)
local additional_fields = {state_change = true}
local button_name = "button" .. button_number
local event = capabilities.button.button.pushed(additional_fields)
local component = device.profile.components[button_name]
device:emit_component_event(component, event)
end
end

local function added_handler(self, device)
for _, component in pairs(device.profile.components) do
local number_of_buttons = component.id == "main" and 2 or 1
device:emit_component_event(component, capabilities.button.supportedButtonValues({"pushed", "held"}))
device:emit_component_event(component, capabilities.button.numberOfButtons({value = number_of_buttons}))
end
device:send(PowerConfiguration.attributes.BatteryVoltage:read(device))
device:emit_event(capabilities.button.button.pushed({state_change = false}))
end

local function do_configure(self, device)
Expand All @@ -88,8 +79,8 @@ local dimming_remote = {
[Level.server.commands.Stop.ID] = button_released_handler
},
[OnOff.ID] = {
[OnOff.server.commands.Off.ID] = button_pressed_handler(2),
[OnOff.server.commands.On.ID] = button_pressed_handler(1)
[OnOff.server.commands.Off.ID] = button_utils.build_button_handler("button2", capabilities.button.button.pushed),
[OnOff.server.commands.On.ID] = button_utils.build_button_handler("button1", capabilities.button.button.pushed)
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions drivers/SmartThings/zigbee-button/src/frient/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ local battery_table = {
[0.00] = 0
}

local function present_value_attr_handler(ZigbeeDriver, ZigbeeDevice, value, zb_rx)
local function present_value_attr_handler(driver, device, value, zb_rx)
local event
local additional_fields = {
state_change = true
}
if value.value == true then
event = capabilities.button.button.pushed(additional_fields)
ZigbeeDevice:emit_event(event)
device:emit_event(event)
end
end

Expand All @@ -52,6 +52,7 @@ end
local function added_handler(self, device)
device:emit_event(capabilities.button.supportedButtonValues({"pushed"}))
device:emit_event(capabilities.button.numberOfButtons({value = 1}))
device:emit_event(capabilities.button.button.pushed({state_change = false}))
end

local configure_handler = function(self, device)
Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/zigbee-button/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ local generate_event_from_zone_status = function(driver, device, zone_status, zb
device:emit_event_for_endpoint(
zb_rx.address_header.src_endpoint.value,
event)
if device:get_component_id_for_endpoint(zb_rx.address_header.src_endpoint.value) ~= "main" then
device:emit_event(event)
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions drivers/SmartThings/zigbee-button/src/iris/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local PowerConfiguration = zcl_clusters.PowerConfiguration
local IASZone = zcl_clusters.IASZone
local device_management = require "st.zigbee.device_management"
local battery_defaults = require "st.zigbee.defaults.battery_defaults"
local button_utils = require "button_utils"

local IRIS_BUTTON_FINGERPRINTS = {
{ mfr = "CentraLite", model = "3455-L" },
Expand All @@ -34,8 +35,6 @@ local function can_handle_iris_button(opts, driver, device, ...)
return false
end

local button_utils = require "button_utils"

local function button_pressed_handler(self, device, value, zb_rx)
button_utils.init_button_press(device)
end
Expand Down Expand Up @@ -63,6 +62,7 @@ end
local function added_handler(self, device)
device:emit_event(capabilities.button.supportedButtonValues({"pushed", "held"}))
device:emit_event(capabilities.button.numberOfButtons({value = 1}))
device:emit_event(capabilities.button.button.pushed({state_change = false}))
device:send(PowerConfiguration.attributes.BatteryVoltage:read(device))
end

Expand Down
1 change: 1 addition & 0 deletions drivers/SmartThings/zigbee-button/src/pushButton/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local capabilities = require "st.capabilities"
local function added_handler(self, device)
device:emit_event(capabilities.button.supportedButtonValues({"pushed"}))
device:emit_event(capabilities.button.numberOfButtons({value = 1}))
device:emit_event(capabilities.button.button.pushed({state_change = false}))
end

local push_button = {
Expand Down
Binary file modified drivers/SmartThings/zigbee-button/src/test/test_aduro_button.lua
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified drivers/SmartThings/zigbee-button/src/test/test_heiman_button.lua
Binary file not shown.
21 changes: 20 additions & 1 deletion drivers/SmartThings/zigbee-button/src/test/test_ikea_on_off.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,30 @@ test.register_coroutine_test(
test.socket.capability:__expect_send(
mock_device:generate_test_message("button1", (button_attr.pushed({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.pushed({ state_change = true }))
)
test.socket.zigbee:__queue_receive({ mock_device.id, OnOff.server.commands.On.build_test_rx(mock_device) })
test.socket.capability:__expect_send(
mock_device:generate_test_message("button2", (button_attr.pushed({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.pushed({ state_change = true }))
)
test.socket.zigbee:__queue_receive({ mock_device.id, Level.server.commands.Move.build_test_rx(mock_device) })
test.socket.capability:__expect_send(
mock_device:generate_test_message("button1", (button_attr.held({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.held({ state_change = true }))
)
test.socket.zigbee:__queue_receive({ mock_device.id, Level.server.commands.MoveWithOnOff.build_test_rx(mock_device) })
test.socket.capability:__expect_send(
mock_device:generate_test_message("button2", (button_attr.held({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.held({ state_change = true }))
)
end
)

Expand Down Expand Up @@ -116,7 +128,7 @@ test.register_coroutine_test(
test.register_coroutine_test(
"ZDO Message handler and adding hub to group",
function()
local binding_table = mgmt_bind_response.BindingTableListRecord("jÀþÿ^ÏÐ", 0x01, 0x0006, 0x01, 0xB9F2)
local binding_table = mgmt_bind_response.BindingTableListRecord("\x6A\x9D\xC0\xFE\xFF\x5E\xCF\xD0", 0x01, 0x0006, 0x01, 0xB9F2)
local response = mgmt_bind_response.MgmtBindResponse({
status = 0x00,
total_binding_table_entry_count = 0x01,
Expand Down Expand Up @@ -170,6 +182,13 @@ test.register_coroutine_test(
})
end
end
test.socket.capability:__expect_send({
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed" }
}
})

test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" })
test.socket.zigbee:__expect_send({
Expand Down
Binary file modified drivers/SmartThings/zigbee-button/src/test/test_ikea_open_close.lua
Binary file not shown.
Binary file not shown.
Binary file modified drivers/SmartThings/zigbee-button/src/test/test_iris_button.lua
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ test.register_coroutine_test(
}
}
)
test.socket.capability:__expect_send({
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed" }
}
})
test.wait_for_events()

test.mock_time.advance_time(50000) -- Battery has a max reporting interval of 21600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ local PowerConfiguration = clusters.PowerConfiguration
local TemperatureMeasurement = clusters.TemperatureMeasurement
local IasEnrollResponseCode = IASZone.types.EnrollResponseCode

local button_attr = capabilities.button.button

local mock_device = test.mock_device.build_test_zigbee_device(
{
profile = t_utils.get_profile_definition("one-button-temp-battery.yml"),
Expand Down Expand Up @@ -129,6 +127,13 @@ test.register_coroutine_test(
attribute_id = "numberOfButtons", state = { value = 1 }
}
})
test.socket.capability:__expect_send({
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed" }
}
})
-- test.socket.zigbee:__expect_send({
-- mock_device.id,
-- PowerConfiguration.attributes.BatteryPercentageRemaining:read(mock_device)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ test.register_coroutine_test(
test.socket.capability:__expect_send(
mock_device:generate_test_message("button1", (button_attr.pushed({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.pushed({ state_change = true }))
)
test.socket.zigbee:__queue_receive({ mock_device.id, WindowCovering.server.commands.DownOrClose.build_test_rx(mock_device) })
test.socket.capability:__expect_send(
mock_device:generate_test_message("button3", (button_attr.pushed({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.pushed({ state_change = true }))
)
test.socket.zigbee:__queue_receive({ mock_device.id, WindowCovering.server.commands.Stop.build_test_rx(mock_device) })
test.socket.capability:__expect_send(
mock_device:generate_test_message("button2", (button_attr.pushed({ state_change = true })))
)
test.socket.capability:__expect_send(
mock_device:generate_test_message("main", button_attr.pushed({ state_change = true }))
)
end
)

Expand Down Expand Up @@ -116,7 +125,7 @@ test.register_coroutine_test(
test.register_coroutine_test(
"ZDO Message handler and adding hub to group",
function()
local binding_table = mgmt_bind_response.BindingTableListRecord("jÀþÿ^ÏÐ", 0x01, 0x0006, 0x01, 0xB9F2)
local binding_table = mgmt_bind_response.BindingTableListRecord("\x6A\x9D\xC0\xFE\xFF\x5E\xCF\xD0", 0x01, 0x0006, 0x01, 0xB9F2)
local response = mgmt_bind_response.MgmtBindResponse({
status = 0x00,
total_binding_table_entry_count = 0x01,
Expand Down Expand Up @@ -170,6 +179,13 @@ test.register_coroutine_test(
})
end
end
test.socket.capability:__expect_send({
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed" }
}
})

test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" })
test.wait_for_events()
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ test.register_coroutine_test(
}
)
test.socket.capability:__expect_send({
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed"}
}
mock_device.id,
{
capability_id = "button", component_id = "main",
attribute_id = "button", state = { value = "pushed" }
}
})
test.wait_for_events()

Expand Down
Loading

0 comments on commit d8e448b

Please sign in to comment.