@@ -5,36 +5,63 @@ local device_lib = require "st.device"
55local clusters = require " st.zigbee.zcl.clusters"
66local configurationMap = require " configurations"
77
8- local function set_up_zll_polling (driver , device )
9- local INFREQUENT_POLL_COUNTER = " _infrequent_poll_counter"
10- local function poll ()
11- local infrequent_counter = device :get_field (INFREQUENT_POLL_COUNTER ) or 1
12- if infrequent_counter == 12 then
13- -- do a full refresh once an hour
14- device :refresh ()
15- infrequent_counter = 0
16- else
17- -- Read On/Off every poll
18- for _ , ep in pairs (device .zigbee_endpoints ) do
19- if device :supports_server_cluster (clusters .OnOff .ID , ep .id ) then
20- device :send (clusters .OnOff .attributes .OnOff :read (device ):to_endpoint (ep .id ))
21- end
8+ local INFREQUENT_POLL_COUNTER = " _infrequent_poll_counter"
9+ local ZLL_POLL_TIMER = " _zll_poll_timer"
10+
11+ local function do_zll_poll (device )
12+ if device == nil or type (device .get_field ) ~= " function" then
13+ return
14+ end
15+
16+ local infrequent_counter = device :get_field (INFREQUENT_POLL_COUNTER ) or 1
17+ if infrequent_counter == 12 then
18+ -- do a full refresh once an hour
19+ device :refresh ()
20+ infrequent_counter = 0
21+ else
22+ -- Read On/Off every poll
23+ for _ , ep in pairs (device .zigbee_endpoints ) do
24+ if device :supports_server_cluster (clusters .OnOff .ID , ep .id ) then
25+ device :send (clusters .OnOff .attributes .OnOff :read (device ):to_endpoint (ep .id ))
2226 end
23- infrequent_counter = infrequent_counter + 1
2427 end
25- device : set_field ( INFREQUENT_POLL_COUNTER , infrequent_counter )
28+ infrequent_counter = infrequent_counter + 1
2629 end
30+ device :set_field (INFREQUENT_POLL_COUNTER , infrequent_counter )
31+ end
2732
33+ local function set_up_zll_polling (driver , device )
2834 -- only set this up for non-child devices
29- if device .network_type == device_lib .NETWORK_TYPE_ZIGBEE then
30- device .thread :call_on_schedule (5 * 60 , poll , " zll_polling" )
35+ if device .network_type ~= device_lib .NETWORK_TYPE_ZIGBEE then
36+ return
37+ end
38+
39+ -- should never happen, but defensive check
40+ local existing_timer = device :get_field (ZLL_POLL_TIMER )
41+ if existing_timer ~= nil then
42+ device .thread :cancel_timer (existing_timer )
43+ end
44+
45+ local timer = device .thread :call_on_schedule (5 * 60 , function ()
46+ do_zll_poll (device )
47+ end , " zll_polling" )
48+
49+ device :set_field (ZLL_POLL_TIMER , timer )
50+ end
51+
52+ local function remove_zll_polling (driver , device )
53+ local existing_timer = device :get_field (ZLL_POLL_TIMER )
54+ if existing_timer ~= nil then
55+ device .thread :cancel_timer (existing_timer )
56+ device :set_field (ZLL_POLL_TIMER , nil )
3157 end
3258end
3359
3460local ZLL_polling = {
3561 NAME = " ZLL Polling" ,
3662 lifecycle_handlers = {
37- init = configurationMap .reconfig_wrapper (set_up_zll_polling )
63+ init = configurationMap .reconfig_wrapper (set_up_zll_polling ),
64+ removed = remove_zll_polling
3865 },
3966 can_handle = require (" zll-polling.can_handle" ),
4067}
0 commit comments