Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batteryLevel capability to the matter lock driver #1608

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: base-lock-batteryLevel
components:
- id: main
capabilities:
- id: lock
version: 1
config:
values:
- key: "lock.value"
enabledValues:
- locked
- unlocked
- not fully locked
- id: lockCodes
version: 1
- id: tamperAlert
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: SmartLock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: lock-lockalarm-batteryLevel
components:
- id: main
capabilities:
- id: lock
version: 1
config:
values:
- key: "lock.value"
enabledValues:
- locked
- unlocked
- not fully locked
- id: lockAlarm
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: SmartLock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: lock-nocodes-notamper-batteryLevel
components:
- id: main
capabilities:
- id: lock
version: 1
config:
values:
- key: "lock.value"
enabledValues:
- locked
- unlocked
- not fully locked
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: SmartLock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: lock-without-codes-batteryLevel
components:
- id: main
capabilities:
- id: lock
version: 1
config:
values:
- key: "lock.value"
enabledValues:
- locked
- unlocked
- not fully locked
- id: tamperAlert
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: SmartLock
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: nonfunctional-lock-batteryLevel
components:
- id: main
capabilities:
- id: lockCodes
version: 1
- id: tamperAlert
version: 1
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: SmartLock
13 changes: 13 additions & 0 deletions drivers/SmartThings/matter-lock/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ local function handle_battery_percent_remaining(driver, device, ib, response)
end
end

local function handle_battery_charge_level(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 max_pin_code_len_handler(driver, device, ib, response)
device:emit_event(capabilities.lockCodes.maxCodeLength(ib.data.value, {visibility = {displayed = false}}))
end
Expand Down Expand Up @@ -573,6 +583,7 @@ local matter_lock_driver = {
},
[PowerSource.ID] = {
[PowerSource.attributes.BatPercentRemaining.ID] = handle_battery_percent_remaining,
[PowerSource.attributes.BatChargeLevel.ID] = handle_battery_charge_level,
},
},
event = {
Expand All @@ -593,6 +604,7 @@ local matter_lock_driver = {
subscribed_attributes = {
[capabilities.lock.ID] = {DoorLock.attributes.LockState},
[capabilities.battery.ID] = {PowerSource.attributes.BatPercentRemaining},
[capabilities.batteryLevel.ID] = {PowerSource.attributes.BatChargeLevel},
},
subscribed_events = {
[capabilities.tamperAlert.ID] = {DoorLock.events.DoorLockAlarm, DoorLock.events.LockOperation},
Expand All @@ -618,6 +630,7 @@ local matter_lock_driver = {
capabilities.lockCodes,
capabilities.tamperAlert,
capabilities.battery,
capabilities.batteryLevel,
},
sub_drivers = {
require("aqara-lock"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- Copyright 2024 SmartThings
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

local test = require "integration_test"
local capabilities = require "st.capabilities"
test.add_package_capability("lockAlarm.yml")
local t_utils = require "integration_test.utils"
local clusters = require "st.matter.clusters"

local mock_device_record = {
profile = t_utils.get_profile_definition("lock-nocodes-notamper-batteryLevel.yml"),
manufacturer_info = {vendor_id = 0x101D, product_id = 0x1},
endpoints = {
{
endpoint_id = 2,
clusters = {
{cluster_id = clusters.Basic.ID, cluster_type = "SERVER"},
},
device_types = {
device_type_id = 0x0016, device_type_revision = 1, -- RootNode
}
},
{
endpoint_id = 10,
clusters = {
{cluster_id = clusters.DoorLock.ID, cluster_type = "SERVER", feature_map = 0x0000},
{cluster_id = clusters.PowerSource.ID, cluster_type = "SERVER", feature_map = 0x0002},
},
},
},
}
local mock_device = test.mock_device.build_test_matter_device(mock_device_record)


local function test_init()
local subscribe_request = clusters.DoorLock.attributes.LockState:subscribe(mock_device)
subscribe_request:merge(clusters.PowerSource.attributes.BatChargeLevel:subscribe(mock_device))
test.socket["matter"]:__expect_send({mock_device.id, subscribe_request})
test.mock_device.add_test_device(mock_device)
end
test.set_test_init_function(test_init)

test.register_message_test(
"Handle BatChargeLevel capability handling with batteryLevel.", {
{
channel = "matter",
hcarter-775 marked this conversation as resolved.
Show resolved Hide resolved
direction = "receive",
message = {
mock_device.id,
clusters.PowerSource.attributes.BatChargeLevel:build_test_report_data(
mock_device, 10, clusters.PowerSource.types.BatChargeLevelEnum.CRITICAL
),
},
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.critical()),
},
{
channel = "matter",
direction = "receive",
message = {
mock_device.id,
clusters.PowerSource.attributes.BatChargeLevel:build_test_report_data(
mock_device, 10, clusters.PowerSource.types.BatChargeLevelEnum.WARNING
),
},
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.warning()),
},
{
channel = "matter",
direction = "receive",
message = {
mock_device.id,
clusters.PowerSource.attributes.BatChargeLevel:build_test_report_data(
mock_device, 10, clusters.PowerSource.types.BatChargeLevelEnum.OK
),
},
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.normal()),
},
}
)

test.run_registered_tests()
Loading