Skip to content

Commit 90da94a

Browse files
committed
philips-hue: Scan for groups on light added lifecycle events
1 parent 27aa1ef commit 90da94a

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

drivers/SmartThings/philips-hue/src/fields.lua

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local Fields = {
2020
EVENT_SOURCE = "eventsource",
2121
GAMUT = "gamut",
2222
GROUPS = "groups",
23+
GROUPS_SCAN_QUEUE = "groups_scan_queue",
2324
HUE_DEVICE_ID = "hue_device_id",
2425
IPV4 = "ipv4",
2526
IS_ONLINE = "is_online",

drivers/SmartThings/philips-hue/src/handlers/lifecycle_handlers/light.lua

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local HueDeviceTypes = require "hue_device_types"
1313
local StrayDeviceHelper = require "stray_device_helper"
1414

1515
local utils = require "utils"
16+
local grouped_utils = require "utils.grouped_utils"
1617

1718
---@class LightLifecycleHandlers
1819
local LightLifecycleHandlers = {}
@@ -197,6 +198,13 @@ function LightLifecycleHandlers.added(driver, device, parent_device_id, resource
197198
command = capabilities.refresh.commands.refresh.NAME,
198199
args = {}
199200
})
201+
202+
local bridge_device = utils.get_hue_bridge_for_device(driver, device, parent_device_id)
203+
if bridge_device then
204+
grouped_utils.queue_group_scan(driver, bridge_device)
205+
else
206+
log.warn("Unable to queue group scan on device added, missing bridge device")
207+
end
200208
end
201209

202210
---@param driver HueDriver

drivers/SmartThings/philips-hue/src/utils/grouped_utils.lua

+43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local log = require "log"
22
local Fields = require "fields"
3+
local cosock = require "cosock"
34

45
--- Room or zone with the children translated from their hue device id or light resource id to
56
--- their SmartThings represented device object. The grouped light resource id is also moved into
@@ -271,4 +272,46 @@ function grouped_utils.set_field_on_group_devices(group, field, v)
271272
end
272273

273274

275+
function grouped_utils.queue_group_scan(driver, bridge_device)
276+
local queue = bridge_device:get_field(Fields.GROUPS_SCAN_QUEUE)
277+
if queue == nil then
278+
local tx, rx = cosock.channel.new()
279+
-- Set timeout to 30 seconds to allow for other queued scans to come in.
280+
rx:settimeout(30)
281+
cosock.spawn(function()
282+
while true do
283+
-- The goal here is to timeout on the receive. If we receive a message then another request
284+
-- to scan came in. This queuing is to prevent a bunch of added devices from causing to have
285+
-- to keep scanning over and over.
286+
local _, err = rx:receive()
287+
if err then
288+
-- err is most likely a timeout but break for all errs
289+
break
290+
end
291+
end
292+
293+
-- Finally do the scan
294+
local hue_device_table = build_hue_id_to_device_map(bridge_device)
295+
local api = bridge_device:get_field(Fields.BRIDGE_API)
296+
297+
if api == nil then
298+
log.warn("Bridge api is nil, unable to do group scan")
299+
-- Clear out tx for the next request
300+
bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, nil)
301+
return
302+
end
303+
304+
grouped_utils.scan_groups(driver, bridge_device, api, hue_device_table)
305+
306+
-- Clear out tx for the next request
307+
bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, nil)
308+
end)
309+
-- Set the queue to the tx
310+
bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, tx)
311+
else
312+
-- Send on the tx that another request has come in.
313+
queue:send(nil)
314+
end
315+
end
316+
274317
return grouped_utils

drivers/SmartThings/philips-hue/src/utils/hue_bridge_utils.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function hue_bridge_utils.do_bridge_network_init(driver, bridge_device, bridge_u
126126

127127
::continue::
128128
end
129-
grouped_utils.scan_groups(driver, bridge_device, bridge_api, child_device_map)
129+
grouped_utils.queue_group_scan(driver, bridge_device)
130130
end, string.format("Hue Bridge %s On Connect Task", bridge_device.label))
131131
end
132132

0 commit comments

Comments
 (0)