|
| 1 | +-- Copyright 2026 SmartThings, Inc. |
| 2 | +-- Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +local zcl_clusters = require "st.zigbee.zcl.clusters" |
| 5 | +local capabilities = require "st.capabilities" |
| 6 | +local data_types = require "st.zigbee.data_types" |
| 7 | +local FrameCtrl = require "st.zigbee.zcl.frame_ctrl" |
| 8 | +local zcl_messages = require "st.zigbee.zcl" |
| 9 | +local messages = require "st.zigbee.messages" |
| 10 | +local zb_const = require "st.zigbee.constants" |
| 11 | +local read_attribute = require "st.zigbee.zcl.global_commands.read_attribute" |
| 12 | + |
| 13 | +local sensitivityAdjustment = capabilities["stse.sensitivityAdjustment"] |
| 14 | +local sensitivityAdjustmentCommandName = "setSensitivityAdjustment" |
| 15 | +local IASZone = zcl_clusters.IASZone |
| 16 | +local IASZone_PRIVATE_COMMAND_ID = 0xF4 |
| 17 | + |
| 18 | +local PREF_SENSITIVITY_VALUE_HIGH = 3 |
| 19 | +local PREF_SENSITIVITY_VALUE_MEDIUM = 2 |
| 20 | +local PREF_SENSITIVITY_VALUE_LOW = 1 |
| 21 | + |
| 22 | +local function send_iaszone_private_cmd(device, priv_cmd, data) |
| 23 | + local frame_ctrl = FrameCtrl(0x00) |
| 24 | + frame_ctrl:set_cluster_specific() |
| 25 | + |
| 26 | + local zclh = zcl_messages.ZclHeader({ |
| 27 | + frame_ctrl = frame_ctrl, |
| 28 | + cmd = data_types.ZCLCommandId(priv_cmd) |
| 29 | + }) |
| 30 | + |
| 31 | + local message_body = zcl_messages.ZclMessageBody({ |
| 32 | + zcl_header = zclh, |
| 33 | + zcl_body = data_types.Uint16(data) |
| 34 | + }) |
| 35 | + |
| 36 | + local addr_header = messages.AddressHeader( |
| 37 | + zb_const.HUB.ADDR, |
| 38 | + zb_const.HUB.ENDPOINT, |
| 39 | + device:get_short_address(), |
| 40 | + device:get_endpoint(IASZone.ID), |
| 41 | + zb_const.HA_PROFILE_ID, |
| 42 | + IASZone.ID |
| 43 | + ) |
| 44 | + |
| 45 | + local zigbee_msg = messages.ZigbeeMessageTx({ |
| 46 | + address_header = addr_header, |
| 47 | + body = message_body |
| 48 | + }) |
| 49 | + |
| 50 | + device:send(zigbee_msg) |
| 51 | +end |
| 52 | + |
| 53 | +local function iaszone_attr_sen_handler(driver, device, value, zb_rx) |
| 54 | + if value.value == PREF_SENSITIVITY_VALUE_HIGH then |
| 55 | + device:emit_event(sensitivityAdjustment.sensitivityAdjustment.High()) |
| 56 | + elseif value.value == PREF_SENSITIVITY_VALUE_MEDIUM then |
| 57 | + device:emit_event(sensitivityAdjustment.sensitivityAdjustment.Medium()) |
| 58 | + elseif value.value == PREF_SENSITIVITY_VALUE_LOW then |
| 59 | + device:emit_event(sensitivityAdjustment.sensitivityAdjustment.Low()) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +local function send_sensitivity_adjustment_value(device, value) |
| 64 | + device:send(IASZone.attributes.CurrentZoneSensitivityLevel:write(device, value)) |
| 65 | +end |
| 66 | + |
| 67 | +local function sensitivity_adjustment_capability_handler(driver, device, command) |
| 68 | + local sensitivity = command.args.sensitivity |
| 69 | + if sensitivity == 'High' then |
| 70 | + send_sensitivity_adjustment_value(device, PREF_SENSITIVITY_VALUE_HIGH) |
| 71 | + elseif sensitivity == 'Medium' then |
| 72 | + send_sensitivity_adjustment_value(device, PREF_SENSITIVITY_VALUE_MEDIUM) |
| 73 | + elseif sensitivity == 'Low' then |
| 74 | + send_sensitivity_adjustment_value(device, PREF_SENSITIVITY_VALUE_LOW) |
| 75 | + end |
| 76 | + device:send(IASZone.attributes.CurrentZoneSensitivityLevel:read(device)) |
| 77 | +end |
| 78 | + |
| 79 | +local function added_handler(self, device) |
| 80 | + device:emit_event(capabilities.motionSensor.motion.inactive()) |
| 81 | + device:emit_event(sensitivityAdjustment.sensitivityAdjustment.High()) |
| 82 | + device:emit_event(capabilities.battery.battery(100)) |
| 83 | + device:send(IASZone.attributes.CurrentZoneSensitivityLevel:read(device)) |
| 84 | +end |
| 85 | + |
| 86 | +local function do_configure(self, device) |
| 87 | + device:configure() |
| 88 | +end |
| 89 | + |
| 90 | +local function info_changed(driver, device, event, args) |
| 91 | + for name, value in pairs(device.preferences) do |
| 92 | + if (device.preferences[name] ~= nil and args.old_st_store.preferences[name] ~= device.preferences[name]) then |
| 93 | + if (name == "detectionfrequency") then |
| 94 | + local detectionfrequency = tonumber(device.preferences.detectionfrequency) |
| 95 | + send_iaszone_private_cmd(device, IASZone_PRIVATE_COMMAND_ID, detectionfrequency) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +local MultiIR_motion_handler = { |
| 102 | + NAME = "MultiIR motion handler", |
| 103 | + lifecycle_handlers = { |
| 104 | + added = added_handler, |
| 105 | + doConfigure = do_configure, |
| 106 | + infoChanged = info_changed |
| 107 | + }, |
| 108 | + capability_handlers = { |
| 109 | + [sensitivityAdjustment.ID] = { |
| 110 | + [sensitivityAdjustmentCommandName] = sensitivity_adjustment_capability_handler, |
| 111 | + } |
| 112 | + }, |
| 113 | + zigbee_handlers = { |
| 114 | + attr = { |
| 115 | + [IASZone.ID] = { |
| 116 | + [IASZone.attributes.CurrentZoneSensitivityLevel.ID] = iaszone_attr_sen_handler |
| 117 | + } |
| 118 | + } |
| 119 | + }, |
| 120 | + can_handle = require("MultiIR.can_handle") |
| 121 | +} |
| 122 | + |
| 123 | +return MultiIR_motion_handler |
0 commit comments