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

[Aqara] Humidity_temp_sensor Battery capability change #1853

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -6,7 +6,7 @@ components:
version: 1
- id: relativeHumidityMeasurement
version: 1
- id: battery
- id: batteryLevel
version: 1
- id: firmwareUpdate
version: 1
Expand All @@ -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: []
28 changes: 25 additions & 3 deletions drivers/SmartThings/zigbee-humidity-sensor/src/aqara/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,49 @@ local is_aqara_products = function(opts, driver, device)
return false
end

local function device_init(driver, device)
battery_defaults.build_linear_voltage_init(2.6, 3.0)(driver, device)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

battery_defaults is now unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused and replaced with battery_level_handler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. But the require needs to be removed. That's why luacheck failed.

Copy link
Contributor

@Kwang-Hui Kwang-Hui Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. I get it. @hjl-aqara , Could you remove line number 1?

- local battery_defaults = require "st.zigbee.defaults.battery_defaults"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just took care of it.

local function battery_level_handler(driver, device, value, zb_rx)
local voltage = value.value
local batteryLevel = "normal"
if voltage <= 25 then
batteryLevel = "critical"
elseif voltage < 28 then
batteryLevel = "warning"
end
device:emit_event(capabilities.batteryLevel.battery(batteryLevel))
end

local function device_init(driver, device)
if configuration ~= nil then
for _, attribute in ipairs(configuration) do
device:add_configured_attribute(attribute)
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())
greens marked this conversation as resolved.
Show resolved Hide resolved
end
end

local function added_handler(self, device)
device:send(cluster_base.write_manufacturer_specific_attribute(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, 10)
}
},
{
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, 26)
}
},
{
channel = "capability",
direction = "send",
message = mock_device:generate_test_message("main", capabilities.batteryLevel.battery.warning())
}
}
)

test.run_registered_tests()
Loading