Skip to content

Refactoring driver and support for PIR Motion Sensor #2093

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion drivers/SmartThings/zigbee-sensor/fingerprints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ zigbeeGeneric:
server:
- 0x0500
deviceProfileName: generic-sensor

- id: "generic-battery-sensor"
deviceLabel: "Zigbee Generic Sensor"
deviceIdentifiers:
- 0x0402
clusters:
server:
- 0x0500
- 0x0001
deviceProfileName: generic-sensor
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: generic-motion-illuminance
components:
- id: main
capabilities:
- id: motionSensor
version: 1
- id: illuminanceMeasurement
version: 1
config:
values:
- key: "illuminance.value"
range: [0, 32000]
- id: battery
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: MotionSensor
32 changes: 32 additions & 0 deletions drivers/SmartThings/zigbee-sensor/src/contact/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copyright 2025 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 defaults = require "st.zigbee.defaults"
local capabilities = require "st.capabilities"

local is_contact_sensor = function(opts, driver, device)
if device:supports_capability(capabilities.contactSensor) then
return true
end
end

local generic_contact_sensor = {
NAME = "Generic Contact Sensor",
supported_capabilities = {
capabilities.contactSensor
},
can_handle = is_contact_sensor
}
defaults.register_for_default_handlers(generic_contact_sensor, generic_contact_sensor.supported_capabilities)
return generic_contact_sensor
65 changes: 17 additions & 48 deletions drivers/SmartThings/zigbee-sensor/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local capabilities = require "st.capabilities"
local constants = require "st.zigbee.constants"
local IasZoneType = require "st.zigbee.generated.types.IasZoneType"
local device_management = require "st.zigbee.device_management"
local PowerConfiguration = clusters.PowerConfiguration

local CONTACT_SWITCH = IasZoneType.CONTACT_SWITCH
local MOTION_SENSOR = IasZoneType.MOTION_SENSOR
Expand All @@ -28,6 +29,7 @@ local ZIGBEE_GENERIC_SENSOR_PROFILE = "generic-sensor"
local ZIGBEE_GENERIC_CONTACT_SENSOR_PROFILE = "generic-contact-sensor"
local ZIGBEE_GENERIC_MOTION_SENSOR_PROFILE = "generic-motion-sensor"
local ZIGBEE_GENERIC_WATERLEAK_SENSOR_PROFILE = "generic-waterleak-sensor"
local ZIGBEE_GENERIC_MOTION_ILLUMINANCE_PROFILE = "generic-motion-illuminance"

local ZONETYPE = "ZoneType"
local IASZone = clusters.IASZone
Expand All @@ -42,6 +44,7 @@ local do_configure = function(self, device)
device:configure()
device:send(device_management.build_bind_request(device, IASZone.ID, self.environment_info.hub_zigbee_eui))
device:send(IASZone.attributes.ZoneStatus:configure_reporting(device, 30, 300, 1))
device:send(PowerConfiguration.attributes.BatteryPercentageRemaining:read(device))
end

local function info_changed(driver, device, event, args)
Expand All @@ -55,12 +58,16 @@ local function update_profile(device, zone_type)
local profile = ZIGBEE_GENERIC_SENSOR_PROFILE
if zone_type == CONTACT_SWITCH then
profile = ZIGBEE_GENERIC_CONTACT_SENSOR_PROFILE
elseif zone_type == MOTION_SENSOR then
profile = ZIGBEE_GENERIC_MOTION_SENSOR_PROFILE
elseif zone_type == WATER_SENSOR then
profile = ZIGBEE_GENERIC_WATERLEAK_SENSOR_PROFILE
elseif zone_type == MOTION_SENSOR then
profile = ZIGBEE_GENERIC_MOTION_SENSOR_PROFILE
for _, ep in ipairs(device.zigbee_endpoints) do
if device:supports_server_cluster(clusters.IlluminanceMeasurement.ID, ep.id) then
profile = ZIGBEE_GENERIC_MOTION_ILLUMINANCE_PROFILE
end
end
end

device:try_update_metadata({profile = profile})
end

Expand All @@ -70,44 +77,6 @@ local ias_zone_type_attr_handler = function (driver, device, attr_val)
update_profile(device, attr_val.value)
end

-- since we don't have button devices using IASZone, the driver here is remaining to be updated
local generate_event_from_zone_status = function(driver, device, zone_status, zb_rx)
local type = device:get_field(ZONETYPE)
local event
if type == CONTACT_SWITCH then
if zone_status:is_alarm1_set() or zone_status:is_alarm2_set() then
event = capabilities.contactSensor.contact.open()
else
event = capabilities.contactSensor.contact.closed()
end
elseif type == MOTION_SENSOR then
if zone_status:is_alarm1_set() or zone_status:is_alarm2_set() then
event = capabilities.motionSensor.motion.active()
else
event = capabilities.motionSensor.motion.inactive()
end
elseif type == WATER_SENSOR then
if zone_status:is_alarm1_set() then
event = capabilities.waterSensor.water.wet()
else
event = capabilities.waterSensor.water.dry()
end
end
if event ~= nil then
device:emit_event_for_endpoint(
zb_rx.address_header.src_endpoint.value,
event)
end
end

local ias_zone_status_attr_handler = function(driver, device, zone_status, zb_rx)
generate_event_from_zone_status(driver, device, zone_status, zb_rx)
end

local ias_zone_status_change_handler = function(driver, device, zb_rx)
generate_event_from_zone_status(driver, device, zb_rx.body.zcl_body.zone_status, zb_rx)
end

local zigbee_generic_sensor_template = {
supported_capabilities = {
capabilities.battery,
Expand All @@ -117,13 +86,7 @@ local zigbee_generic_sensor_template = {
zigbee_handlers = {
attr = {
[IASZone.ID] = {
[IASZone.attributes.ZoneType.ID] = ias_zone_type_attr_handler,
[IASZone.attributes.ZoneStatus.ID] = ias_zone_status_attr_handler
}
},
cluster = {
[IASZone.ID] = {
[IASZone.client.commands.ZoneStatusChangeNotification.ID] = ias_zone_status_change_handler
[IASZone.attributes.ZoneType.ID] = ias_zone_type_attr_handler
}
}
},
Expand All @@ -132,6 +95,12 @@ local zigbee_generic_sensor_template = {
doConfigure = do_configure,
infoChanged = info_changed
},
sub_drivers = {
require("contact"),
require("motion"),
require("waterleak"),
require("motion-illuminance")
},
ias_zone_configuration_method = constants.IAS_ZONE_CONFIGURE_TYPE.AUTO_ENROLL_RESPONSE
}

Expand Down
33 changes: 33 additions & 0 deletions drivers/SmartThings/zigbee-sensor/src/motion-illuminance/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Copyright 2025 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 defaults = require "st.zigbee.defaults"
local capabilities = require "st.capabilities"

local is_motion_illuminance = function(opts, driver, device)
if device:supports_capability(capabilities.motionSensor) and device:supports_capability(capabilities.illuminanceMeasurement) then
return true
end
end

local generic_motion_illuminance = {
NAME = "Generic Motion illuminance",
supported_capabilities = {
capabilities.illuminanceMeasurement,
capabilities.motionSensor
},
can_handle = is_motion_illuminance
}
defaults.register_for_default_handlers(generic_motion_illuminance, generic_motion_illuminance.supported_capabilities)
return generic_motion_illuminance
32 changes: 32 additions & 0 deletions drivers/SmartThings/zigbee-sensor/src/motion/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copyright 2025 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 defaults = require "st.zigbee.defaults"
local capabilities = require "st.capabilities"

local is_motion_sensor = function(opts, driver, device)
if device:supports_capability(capabilities.motionSensor) and not device:supports_capability(capabilities.illuminanceMeasurement) then
return true
end
end

local generic_motion_sensor = {
NAME = "Generic Motion Sensor",
supported_capabilities = {
capabilities.motionSensor
},
can_handle = is_motion_sensor
}
defaults.register_for_default_handlers(generic_motion_sensor, generic_motion_sensor.supported_capabilities)
return generic_motion_sensor
Loading
Loading