Skip to content

Commit

Permalink
Support batteryLevel in matter-switch and matter-window-covering
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolas-deboom committed Dec 10, 2024
1 parent 061a1d0 commit c30b955
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ local function test_init_humidity_battery()
test.mock_device.add_test_device(mock_device_humidity_battery)

test.socket.device_lifecycle:__queue_receive({ mock_device_humidity_battery.id, "added" })
local read_attribute_list = clusters.PowerSource.attributes.AttributeList:read()
test.socket.matter:__expect_send({mock_device_humidity_battery.id, read_attribute_list})
test.socket.device_lifecycle:__queue_receive({ mock_device_humidity_battery.id, "doConfigure" })
mock_device_humidity_battery:expect_metadata_update({ profile = "humidity-batteryLevel" })
mock_device_humidity_battery:expect_metadata_update({ provisioning_state = "PROVISIONED" })
local read_attribute_list = clusters.PowerSource.attributes.AttributeList:read()
test.socket.matter:__expect_send({mock_device_humidity_battery.id, read_attribute_list})
end

local function test_init_humidity_no_battery()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: 2-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: 3-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: 4-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button4
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: 5-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button4
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button5
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: 6-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button4
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button5
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button6
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: 7-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button4
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button5
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button6
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button7
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: 8-button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: RemoteController
- id: button2
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button3
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button4
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button5
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button6
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button7
capabilities:
- id: button
version: 1
categories:
- name: RemoteController
- id: button8
capabilities:
- id: button
version: 1
categories:
- name: RemoteController

14 changes: 14 additions & 0 deletions drivers/SmartThings/matter-switch/profiles/button-batteryLevel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: button-batteryLevel
components:
- id: main
capabilities:
- id: button
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: Button
50 changes: 42 additions & 8 deletions drivers/SmartThings/matter-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local MatterDriver = require "st.matter.driver"
local lua_socket = require "socket"
local utils = require "st.utils"
local device_lib = require "st.device"
local im = require "st.matter.interaction_model"

local MOST_RECENT_TEMP = "mostRecentTemp"
local RECEIVED_X = "receivedX"
Expand Down Expand Up @@ -580,25 +581,31 @@ local function initialize_switch(driver, device)
device:try_update_metadata({profile = profile_name})
device:set_field(DEFERRED_CONFIGURE, true)
elseif #button_eps > 0 then
local battery_feature_eps = device:get_endpoints(clusters.PowerSource.ID, {feature_bitmap = clusters.PowerSource.types.PowerSourceFeature.BATTERY})
local battery_support = false
if device.manufacturer_info.vendor_id ~= HUE_MANUFACTURER_ID and
#device:get_endpoints(clusters.PowerSource.ID, {feature_bitmap = clusters.PowerSource.types.PowerSourceFeature.BATTERY}) > 0 then
if device.manufacturer_info.vendor_id ~= HUE_MANUFACTURER_ID and #battery_feature_eps > 0 then
battery_support = true
end
if #button_eps > 1 and tbl_contains(STATIC_BUTTON_PROFILE_SUPPORTED, #button_eps) then
if tbl_contains(STATIC_BUTTON_PROFILE_SUPPORTED, #button_eps) then
if battery_support then
profile_name = string.format("%d-button-battery", #button_eps)
profile_name = "button-batteryLevel"
else
profile_name = string.format("%d-button", #button_eps)
profile_name = "button"
end

if #button_eps > 1 then
profile_name = string.format("%d-", #button_eps) .. profile_name
end
elseif not battery_support then
-- a battery-less button/remote
profile_name = "button"
end

if profile_name then
device:try_update_metadata({profile = profile_name})
device:set_field(DEFERRED_CONFIGURE, true)
if battery_support then
local attribute_list_read = im.InteractionRequest(im.InteractionRequest.RequestType.READ, {})
attribute_list_read:merge(clusters.PowerSource.attributes.AttributeList:read())
device:send(attribute_list_read)
end
else
configure_buttons(device)
end
Expand Down Expand Up @@ -1092,6 +1099,31 @@ local function battery_percent_remaining_attr_handler(driver, device, ib, respon
end
end

local function battery_charge_level_attr_handler(driver, device, ib, response)
if ib.data.value == clusters.PowerSource.types.BatChargeLevelEnum.OK then
device:emit_event(capabilities.batteryLevel.battery.normal())
elseif ib.data.value == clusters.PowerSource.types.BatChargeLevelEnum.WARNING then
device:emit_event(capabilities.batteryLevel.battery.warning())
elseif ib.data.value == clusters.PowerSource.types.BatChargeLevelEnum.CRITICAL then
device:emit_event(capabilities.batteryLevel.battery.critical())
end
end

local function power_source_attribute_list_handler(driver, device, ib, response)
for _, attr in ipairs(ib.data.elements) do
-- Re-profile the device if BatPercentRemaining (Attribute ID 0x0C) is present.
if attr.value == 0x0C then
local button_eps = device:get_endpoints(clusters.Switch.ID, {feature_bitmap=clusters.Switch.types.SwitchFeature.MOMENTARY_SWITCH})
local profile_name = "button-battery"
if #button_eps > 1 then
profile_name = string.format("%d-", #button_eps) .. profile_name
end
device:try_update_metadata({ profile = profile_name })
return
end
end
end

local function max_press_handler(driver, device, ib, response)
local max = ib.data.value or 1 --get max number of presses
device.log.debug("Device supports "..max.." presses")
Expand Down Expand Up @@ -1182,6 +1214,8 @@ local matter_driver_template = {
[clusters.ValveConfigurationAndControl.attributes.CurrentLevel.ID] = valve_level_attr_handler
},
[clusters.PowerSource.ID] = {
[clusters.PowerSource.attributes.AttributeList.ID] = power_source_attribute_list_handler,
[clusters.PowerSource.attributes.BatChargeLevel.ID] = battery_charge_level_attr_handler,
[clusters.PowerSource.attributes.BatPercentRemaining.ID] = battery_percent_remaining_attr_handler,
},
[clusters.Switch.ID] = {
Expand Down
Loading

0 comments on commit c30b955

Please sign in to comment.