@@ -29,74 +29,113 @@ local MFG_CODE = 0x115F
2929local MULTISTATE_INPUT_CLUSTER_ID = 0x0012
3030local PRESENT_ATTRIBUTE_ID = 0x0055
3131
32+ local COMP_LIST = { " button1" , " button2" , " all" }
3233local FINGERPRINTS = {
33- { mfr = " LUMI" , model = " lumi.remote.b1acn02" },
34- { mfr = " LUMI" , model = " lumi.remote.acn003" }
34+ [" lumi.remote.b1acn02" ] = { mfr = " LUMI" , btn_cnt = 1 },
35+ [" lumi.remote.acn003" ] = { mfr = " LUMI" , btn_cnt = 1 },
36+ [" lumi.remote.b186acn03" ] = { mfr = " LUMI" , btn_cnt = 1 },
37+ [" lumi.remote.b286acn03" ] = { mfr = " LUMI" , btn_cnt = 3 }
3538}
3639
3740local configuration = {
38- {
39- cluster = MULTISTATE_INPUT_CLUSTER_ID ,
40- attribute = PRESENT_ATTRIBUTE_ID ,
41- minimum_interval = 3 ,
42- maximum_interval = 7200 ,
43- data_type = data_types .Uint16 ,
44- reportable_change = 1
45- },
46- {
47- cluster = PowerConfiguration .ID ,
48- attribute = PowerConfiguration .attributes .BatteryVoltage .ID ,
49- minimum_interval = 30 ,
50- maximum_interval = 3600 ,
51- data_type = PowerConfiguration .attributes .BatteryVoltage .base_type ,
52- reportable_change = 1
53- }
41+ {
42+ cluster = MULTISTATE_INPUT_CLUSTER_ID ,
43+ attribute = PRESENT_ATTRIBUTE_ID ,
44+ minimum_interval = 3 ,
45+ maximum_interval = 7200 ,
46+ data_type = data_types .Uint16 ,
47+ reportable_change = 1
48+ },
49+ {
50+ cluster = PowerConfiguration .ID ,
51+ attribute = PowerConfiguration .attributes .BatteryVoltage .ID ,
52+ minimum_interval = 30 ,
53+ maximum_interval = 3600 ,
54+ data_type = PowerConfiguration .attributes .BatteryVoltage .base_type ,
55+ reportable_change = 1
56+ }
5457}
5558
5659local function present_value_attr_handler (driver , device , value , zb_rx )
57- if value .value == 1 then
58- device :emit_event (capabilities .button .button .pushed ({state_change = true }))
59- elseif value .value == 2 then
60- device :emit_event (capabilities .button .button .double ({state_change = true }))
61- elseif value .value == 0 then
62- device :emit_event (capabilities .button .button .held ({state_change = true }))
63- end
60+ local end_point = zb_rx .address_header .src_endpoint .value
61+ local btn_evt_cnt = FINGERPRINTS [device :get_model ()].btn_cnt or 1
62+ local evt = capabilities .button .button .held ({ state_change = true })
63+ if value .value == 1 then
64+ evt = capabilities .button .button .pushed ({ state_change = true })
65+ elseif value .value == 2 then
66+ evt = capabilities .button .button .double ({ state_change = true })
67+ end
68+ device :emit_event (evt )
69+ if btn_evt_cnt > 1 then
70+ device :emit_component_event (device .profile .components [COMP_LIST [end_point ]], evt )
71+ end
72+ end
73+ local function battery_level_handler (driver , device , value , zb_rx )
74+ local voltage = value .value
75+ local batteryLevel = " normal"
76+ if voltage <= 25 then
77+ batteryLevel = " critical"
78+ elseif voltage < 28 then
79+ batteryLevel = " warning"
80+ end
81+ device :emit_event (capabilities .batteryLevel .battery (batteryLevel ))
6482end
6583
6684local is_aqara_products = function (opts , driver , device )
67- for _ , fingerprint in ipairs (FINGERPRINTS ) do
68- if device :get_manufacturer () == fingerprint .mfr and device :get_model () == fingerprint .model then
69- return true
70- end
71- end
72- return false
85+ local isAqaraProducts = false
86+ if FINGERPRINTS [device :get_model ()] and FINGERPRINTS [device :get_model ()].mfr == device :get_manufacturer () then
87+ isAqaraProducts = true
88+ end
89+ return isAqaraProducts
7390end
7491
7592local function device_init (driver , device )
76- battery_defaults .build_linear_voltage_init (2.6 , 3.0 )(driver , device )
77- if configuration ~= nil then
78- for _ , attribute in ipairs (configuration ) do
79- device :add_configured_attribute (attribute )
80- end
93+ battery_defaults .build_linear_voltage_init (2.6 , 3.0 )(driver , device )
94+ if configuration ~= nil then
95+ for _ , attribute in ipairs (configuration ) do
96+ device :add_configured_attribute (attribute )
8197 end
98+ end
8299end
83100
84101local function added_handler (self , device )
85- device :emit_event (capabilities .button .supportedButtonValues ({" pushed" ," held" ," double" }, {visibility = { displayed = false }}))
86- device :emit_event (capabilities .button .numberOfButtons ({value = 1 }))
87- button_utils .emit_event_if_latest_state_missing (device , " main" , capabilities .button , capabilities .button .button .NAME , capabilities .button .button .pushed ({state_change = false }))
88- device :emit_event (capabilities .battery .battery (100 ))
102+ local btn_evt_cnt = FINGERPRINTS [device :get_model ()].btn_cnt or 1
103+
104+ device :emit_event (capabilities .button .supportedButtonValues ({ " pushed" , " held" , " double" },
105+ { visibility = { displayed = false } }))
106+ device :emit_event (capabilities .button .numberOfButtons ({ value = 1 }))
107+ button_utils .emit_event_if_latest_state_missing (device , " main" , capabilities .button , capabilities .button .button .NAME ,
108+ capabilities .button .button .pushed ({ state_change = false }))
109+ device :emit_event (capabilities .batteryLevel .battery .normal ())
110+ device :emit_event (capabilities .batteryLevel .type (" CR2032" ))
111+ device :emit_event (capabilities .batteryLevel .quantity (1 ))
112+
113+ if btn_evt_cnt > 1 then
114+ for i = 1 , btn_evt_cnt do
115+ device :emit_component_event (device .profile .components [COMP_LIST [i ]],
116+ capabilities .button .supportedButtonValues ({ " pushed" , " held" , " double" },
117+ { visibility = { displayed = false } }))
118+ device :emit_component_event (device .profile .components [COMP_LIST [i ]],
119+ capabilities .button .numberOfButtons ({ value = 1 }))
120+ device :emit_component_event (device .profile .components [COMP_LIST [i ]],
121+ capabilities .button .button .pushed ({ state_change = false }))
122+ button_utils .emit_event_if_latest_state_missing (device , COMP_LIST [i ], capabilities .button ,
123+ capabilities .button .button .NAME , capabilities .button .button .pushed ({ state_change = false }))
124+ end
125+ end
89126end
90127
91128local function do_configure (driver , device )
129+ local ATTR_ID = PRIVATE_ATTRIBUTE_ID_T1
130+ local cmd_value = 1
131+
92132 device :configure ()
93- if device :get_model () == " lumi.remote.b1acn02" then
94- device :send (cluster_base .write_manufacturer_specific_attribute (device ,
95- PRIVATE_CLUSTER_ID , PRIVATE_ATTRIBUTE_ID_T1 , MFG_CODE , data_types .Uint8 , 1 ))
96- elseif device :get_model () == " lumi.remote.acn003" then
97- device :send (cluster_base .write_manufacturer_specific_attribute (device ,
98- PRIVATE_CLUSTER_ID , PRIVATE_ATTRIBUTE_ID_E1 , MFG_CODE , data_types .Uint8 , 2 ))
133+ if device :get_model () == " lumi.remote.acn003" then
134+ ATTR_ID = PRIVATE_ATTRIBUTE_ID_E1
135+ cmd_value = 2
99136 end
137+ device :send (cluster_base .write_manufacturer_specific_attribute (device ,
138+ PRIVATE_CLUSTER_ID , ATTR_ID , MFG_CODE , data_types .Uint8 , cmd_value ))
100139 -- when the wireless switch T1 accesses the network, the gateway sends
101140 -- private attribute 0009 to make the device no longer distinguish
102141 -- between the standard gateway and the aqara gateway.
@@ -105,20 +144,23 @@ local function do_configure(driver, device)
105144end
106145
107146local aqara_wireless_switch_handler = {
108- NAME = " Aqara Wireless Switch Handler" ,
109- lifecycle_handlers = {
110- init = device_init ,
111- added = added_handler ,
112- doConfigure = do_configure
113- },
114- zigbee_handlers = {
115- attr = {
116- [MULTISTATE_INPUT_CLUSTER_ID ] = {
117- [PRESENT_ATTRIBUTE_ID ] = present_value_attr_handler
118- }
147+ NAME = " Aqara Wireless Switch Handler" ,
148+ lifecycle_handlers = {
149+ init = device_init ,
150+ added = added_handler ,
151+ doConfigure = do_configure
152+ },
153+ zigbee_handlers = {
154+ attr = {
155+ [MULTISTATE_INPUT_CLUSTER_ID ] = {
156+ [PRESENT_ATTRIBUTE_ID ] = present_value_attr_handler
157+ },
158+ [PowerConfiguration .ID ] = {
159+ [PowerConfiguration .attributes .BatteryVoltage .ID ] = battery_level_handler
119160 }
120- },
121- can_handle = is_aqara_products
161+ }
162+ },
163+ can_handle = is_aqara_products
122164}
123165
124166return aqara_wireless_switch_handler
0 commit comments