From a70b104fd1f7bf0ec8ec12ed9b1ba137d7e9987f Mon Sep 17 00:00:00 2001 From: hjl Date: Tue, 7 Jan 2025 15:31:28 +0900 Subject: [PATCH 1/4] Replace Battery capability into BatteryLevel capability --- .../profiles/humidity-temp-battery-aqara.yml | 47 +++++++++++++-- .../zigbee-humidity-sensor/src/aqara/init.lua | 26 +++++++- .../src/test/test_aqara_sensor.lua | 59 +++++++++++++++++++ 3 files changed, 127 insertions(+), 5 deletions(-) diff --git a/drivers/SmartThings/zigbee-humidity-sensor/profiles/humidity-temp-battery-aqara.yml b/drivers/SmartThings/zigbee-humidity-sensor/profiles/humidity-temp-battery-aqara.yml index d0121f8f02..f97a646078 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/profiles/humidity-temp-battery-aqara.yml +++ b/drivers/SmartThings/zigbee-humidity-sensor/profiles/humidity-temp-battery-aqara.yml @@ -6,7 +6,7 @@ components: version: 1 - id: relativeHumidityMeasurement version: 1 - - id: battery + - id: batteryLevel version: 1 - id: firmwareUpdate version: 1 @@ -19,6 +19,45 @@ preferences: explicit: true - preferenceId: humidityOffset explicit: true -metadata: - mnmn: SolutionsEngineering - vid: SmartThings-smartthings-Aqara_Humidity_Temperature +deviceConfig: + dashboard: + states: + - component: main + capability: temperatureMeasurement + version: 1 + group: main + composite: true + - component: main + capability: relativeHumidityMeasurement + version: 1 + group: main + values: + - label: " {{humidity.value}} {{humidity.unit}}" + composite: true + actions: [] + basicPlus: [] + detailView: + - component: main + capability: temperatureMeasurement + version: 1 + - component: main + capability: relativeHumidityMeasurement + version: 1 + - component: main + capability: batteryLevel + version: 1 + - component: main + capability: refresh + version: 1 + automation: + conditions: + - component: main + capability: temperatureMeasurement + version: 1 + - component: main + capability: relativeHumidityMeasurement + version: 1 + - component: main + capability: batteryLevel + version: 1 + actions: [] diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua index dab7edf0cd..27c69f41a0 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua @@ -53,6 +53,17 @@ local is_aqara_products = function(opts, driver, device) return false end +local function battery_level_handler(driver, device, value, zb_rx) + local voltage = value.value + local batteryLevel = "normal" + if voltage == 0 then + batteryLevel = "critical" + elseif voltage < 28 then + batteryLevel = "warning" + end + device:emit_event(capabilities.batteryLevel.battery(batteryLevel)) +end + local function device_init(driver, device) battery_defaults.build_linear_voltage_init(2.6, 3.0)(driver, device) @@ -62,6 +73,12 @@ local function device_init(driver, device) device:add_monitored_attribute(attribute) end end + + local batt_level = device:get_latest_state("main", capabilities.batteryLevel.ID, capabilities.batteryLevel.battery + .NAME) or nil + if batt_level == nil then + device:emit_event(capabilities.batteryLevel.battery.normal()) + end end local function added_handler(self, device) @@ -69,11 +86,18 @@ local function added_handler(self, device) PRIVATE_CLUSTER_ID, PRIVATE_ATTRIBUTE_ID, MFG_CODE, data_types.Uint8, 1)) device:emit_event(capabilities.temperatureMeasurement.temperature({ value = 0, unit = "C" })) device:emit_event(capabilities.relativeHumidityMeasurement.humidity(0)) - device:emit_event(capabilities.battery.battery(100)) + device:emit_event(capabilities.batteryLevel.battery("normal")) end local aqara_humidity_handler = { NAME = "Aqara Humidity Handler", + zigbee_handlers = { + attr = { + [PowerConfiguration.ID] = { + [PowerConfiguration.attributes.BatteryVoltage.ID] = battery_level_handler + } + } + }, lifecycle_handlers = { init = device_init, added = added_handler diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua index 2231489167..6053b0c302 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua @@ -39,6 +39,8 @@ local mock_device = test.mock_device.build_test_zigbee_device( zigbee_test_utils.prepare_zigbee_env_info() local function test_init() + test.socket.capability:__expect_send(mock_device:generate_test_message("main", + capabilities.batteryLevel.battery.normal())) test.mock_device.add_test_device(mock_device) end @@ -188,4 +190,61 @@ test.register_coroutine_test( end ) +test.register_message_test( + "BatteryVoltage report should be handled(normal)", + { + { + channel = "zigbee", + direction = "receive", + message = { + mock_device.id, + PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 30) + } + }, + { + channel = "capability", + direction = "send", + message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.normal()) + } + } +) + +test.register_message_test( + "BatteryVoltage report should be handled(critical)", + { + { + channel = "zigbee", + direction = "receive", + message = { + mock_device.id, + PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 0) + } + }, + { + channel = "capability", + direction = "send", + message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.critical()) + } + } +) + +test.register_message_test( + "BatteryVoltage report should be handled(warning)", + { + { + channel = "zigbee", + direction = "receive", + message = { + mock_device.id, + PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 20) + } + }, + { + channel = "capability", + direction = "send", + message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.warning()) + } + } +) + test.run_registered_tests() From 7656ca1e267fd70520a427147e49052d83360121 Mon Sep 17 00:00:00 2001 From: hjl Date: Tue, 7 Jan 2025 16:53:33 +0900 Subject: [PATCH 2/4] Remove unnessessary voltage_init statement --- drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua index 27c69f41a0..c4f7520b44 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua @@ -65,8 +65,6 @@ local function battery_level_handler(driver, device, value, zb_rx) end local function device_init(driver, device) - battery_defaults.build_linear_voltage_init(2.6, 3.0)(driver, device) - if configuration ~= nil then for _, attribute in ipairs(configuration) do device:add_configured_attribute(attribute) From 2ae902dfddc7a6604735e60e509f7da70fda3a21 Mon Sep 17 00:00:00 2001 From: hjl Date: Wed, 8 Jan 2025 08:36:49 +0900 Subject: [PATCH 3/4] Fix battery level voltage condition --- drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua | 2 +- .../zigbee-humidity-sensor/src/test/test_aqara_sensor.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua index c4f7520b44..4d6a5ff685 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua @@ -56,7 +56,7 @@ end local function battery_level_handler(driver, device, value, zb_rx) local voltage = value.value local batteryLevel = "normal" - if voltage == 0 then + if voltage <= 25 then batteryLevel = "critical" elseif voltage < 28 then batteryLevel = "warning" diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua index 6053b0c302..104c36c3e5 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/test/test_aqara_sensor.lua @@ -217,7 +217,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 0) + PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 10) } }, { @@ -236,7 +236,7 @@ test.register_message_test( direction = "receive", message = { mock_device.id, - PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 20) + PowerConfiguration.attributes.BatteryVoltage:build_test_attr_report(mock_device, 26) } }, { From 7775fb9f9a043b890762621150f86c8a911115ed Mon Sep 17 00:00:00 2001 From: hjl Date: Thu, 9 Jan 2025 08:28:05 +0900 Subject: [PATCH 4/4] Remove unused require field --- drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua index 4d6a5ff685..e712f7711f 100644 --- a/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua +++ b/drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua @@ -1,4 +1,3 @@ -local battery_defaults = require "st.zigbee.defaults.battery_defaults" local clusters = require "st.zigbee.zcl.clusters" local cluster_base = require "st.zigbee.cluster_base" local data_types = require "st.zigbee.data_types"