Skip to content

generic_zigbee_sensor_improvement #2093

New issue

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

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

Already on GitHub? # 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
18 changes: 17 additions & 1 deletion drivers/SmartThings/zigbee-sensor/fingerprints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ zigbeeGeneric:
server:
- 0x0500
deviceProfileName: generic-sensor

- id: "generic-battery-sensor"
deviceLabel: "Zigbee Generic Sensor"
deviceIdentifiers:
- 0x0402
clusters:
server:
- 0x0500
- 0x0001
deviceProfileName: generic-sensor
- id: "generic-button-sensor"
deviceLabel: "Zigbee Generic Button"
clusters:
server:
- 0x0500
- 0xfe00 # EZVIZ private cluster
- 0xfe05 # EZVIZ private cluster
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

we usually capitalize the letters in our hex

deviceProfileName: generic-remote-control
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: generic-remote-control
components:
- id: main
capabilities:
- id: button
version: 1
- id: battery
version: 1
- id: firmwareUpdate
version: 1
- id: refresh
version: 1
categories:
- name: Button
67 changes: 67 additions & 0 deletions drivers/SmartThings/zigbee-sensor/src/contact/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- 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 clusters = require "st.zigbee.zcl.clusters"
local capabilities = require "st.capabilities"

local IASZone = clusters.IASZone

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

local generate_event_from_zone_status = function(driver, device, zone_status, zb_rx)
local event
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
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
Comment on lines +26 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

are we overwriting the defaults somewhere that makes it necessary to redefine them here?



local generic_contact_sensor = {
NAME = "Generic Contact Sensor",
zigbee_handlers = {
attr = {
[IASZone.ID] = {
[IASZone.attributes.ZoneStatus.ID] = ias_zone_status_attr_handler
}
},
cluster = {
[IASZone.ID] = {
[IASZone.client.commands.ZoneStatusChangeNotification.ID] = ias_zone_status_change_handler
}
}
},
can_handle = is_contact_sensor
}

return generic_contact_sensor
70 changes: 70 additions & 0 deletions drivers/SmartThings/zigbee-sensor/src/ezviz/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- 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 capabilities = require "st.capabilities"
local defaults = require "st.zigbee.defaults"

local EZVIZ_PRIVATE_CLUSTER = 0xFE05
local EZVIZ_PRIVATE_ATTRIBUTE = 0x0000

local EZVIZ_MFR = "EZVIZ"

local is_ezviz_button = function(opts, driver, device)
if device:supports_capability(capabilities.button) and device:get_manufacturer() == EZVIZ_MFR then
return true
end
end

-- We need an empty added here to override the added in root init.lua, because we already knows its profile
local device_added = function(self, device)
end

local ezviz_private_cluster_button_handler = function(driver, device, zb_rx)
local event
local additional_fields = {
state_change = true
}
if zb_rx.value == 0x01 then
event = capabilities.button.button.pushed(additional_fields)
elseif zb_rx.value == 0x02 then
event = capabilities.button.button.double(additional_fields)
elseif zb_rx.value == 0x03 then
event = capabilities.button.button.held(additional_fields)
end
if event ~= nil then
device:emit_event(event)
end
end

local ezviz_button_handler = {
NAME = "Ezviz Button",
supported_capabilities = {
capabilities.battery,
capabilities.button,
capabilities.refresh
},
zigbee_handlers = {
attr = {
[EZVIZ_PRIVATE_CLUSTER] = {
[EZVIZ_PRIVATE_ATTRIBUTE] = ezviz_private_cluster_button_handler
}
}
},
lifecycle_handlers = {
added = device_added
},
can_handle = is_ezviz_button
}
defaults.register_for_default_handlers(ezviz_button_handler, ezviz_button_handler.supported_capabilities)
return ezviz_button_handler
69 changes: 21 additions & 48 deletions drivers/SmartThings/zigbee-sensor/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ local device_management = require "st.zigbee.device_management"
local CONTACT_SWITCH = IasZoneType.CONTACT_SWITCH
local MOTION_SENSOR = IasZoneType.MOTION_SENSOR
local WATER_SENSOR = IasZoneType.WATER_SENSOR
local REMOTE_CONTROL = IasZoneType.REMOTE_CONTROL

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 ZIGBEE_GENERIC_REMOTE_CONTROL_PROFILE = "generic-remote-control"

local ZONETYPE = "ZoneType"
local IASZone = clusters.IASZone
Expand Down Expand Up @@ -55,12 +58,18 @@ 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 == REMOTE_CONTROL then
profile = ZIGBEE_GENERIC_REMOTE_CONTROL_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 +79,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 +88,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 +97,14 @@ local zigbee_generic_sensor_template = {
doConfigure = do_configure,
infoChanged = info_changed
},
sub_drivers = {
require("contact"),
require("motion"),
require("waterleak"),
require("motion-illuminance"),
require("meian"),
require("ezviz")
},
ias_zone_configuration_method = constants.IAS_ZONE_CONFIGURE_TYPE.AUTO_ENROLL_RESPONSE
}

Expand Down
Loading
Loading