@@ -26,19 +26,56 @@ local DOMAIN = "local"
2626--- @class HueDiscovery
2727--- @field public api_keys table<string,string>
2828--- @field public disco_api_instances table<string,PhilipsHueApi>
29- --- @field public device_state_disco_cache table<string,table<string,any>>
29+ --- @field public device_state_disco_cache_by_bridge table<string,table<string,table<string, any> >>
3030--- @field public ServiceType string
3131--- @field public Domain string
3232--- @field public discovery_active boolean
3333local HueDiscovery = {
3434 api_keys = {},
3535 disco_api_instances = {},
36- device_state_disco_cache = {},
36+ device_state_disco_cache_by_bridge = {},
3737 ServiceType = SERVICE_TYPE ,
3838 Domain = DOMAIN ,
3939 discovery_active = false
4040}
4141
42+ --- The cached state of the devices on one bridge, keyed by Hue resource ID (and, for some device
43+ --- types, also by v1 ID), creating it if needed. The cache is kept per bridge because a Hue Bridge
44+ --- Pro keeps the resource IDs of the devices migrated to it, so two bridges can report state for the
45+ --- same resource ID, and a device must only ever be set up from its own bridge's state.
46+ --- @param bridge_network_id string ? the bridge ' s device network ID
47+ --- @return table<string,table<string,any>> ? cache nil if no bridge network ID was given
48+ function HueDiscovery .state_cache_for_bridge (bridge_network_id )
49+ if not bridge_network_id then return nil end
50+ local cache = HueDiscovery .device_state_disco_cache_by_bridge [bridge_network_id ]
51+ if not cache then
52+ cache = {}
53+ HueDiscovery .device_state_disco_cache_by_bridge [bridge_network_id ] = cache
54+ end
55+ return cache
56+ end
57+
58+ --- The state cache of the bridge a child device belongs to (see `state_cache_for_bridge`).
59+ --- @param driver HueDriver
60+ --- @param device HueDevice
61+ --- @param parent_device_id string ? the device ' s parent, if known, as for `utils.get_hue_bridge_for_device`
62+ --- @return table<string,table<string,any>> ? cache nil if the device ' s bridge isn' t known
63+ function HueDiscovery .state_cache_for_device (driver , device , parent_device_id )
64+ local bridge = utils .get_hue_bridge_for_device (driver , device , parent_device_id , true )
65+ return bridge and HueDiscovery .state_cache_for_bridge (bridge .device_network_id )
66+ end
67+
68+ --- The cached state of resource `rid` on the bridge `device` belongs to.
69+ --- @param driver HueDriver
70+ --- @param device HueDevice
71+ --- @param rid string ?
72+ --- @param parent_device_id string ?
73+ --- @return table<string,any> ?
74+ function HueDiscovery .cached_state_for_device (driver , device , rid , parent_device_id )
75+ local cache = rid and HueDiscovery .state_cache_for_device (driver , device , parent_device_id )
76+ return cache and cache [rid ]
77+ end
78+
4279-- Lazy-load the discovered device handlers so we only load the code we need
4380--- @type table<string,DiscoveredChildDeviceHandler>
4481local discovered_device_handlers = utils .lazy_handler_loader (" disco" )
@@ -251,7 +288,10 @@ function HueDiscovery.search_bridge_for_supported_devices(driver, bridge_network
251288 for _ , device in ipairs (driver :get_devices ()) do
252289 -- We're only interested in processing child/non-bridge devices here.
253290 if utils .is_bridge (driver , device ) then goto continue end
254- local not_known_to_bridge = device_is_joined_to_bridge [device :get_field (Fields .HUE_DEVICE_ID ) or " " ]
291+ -- A device without a Hue device ID has not completed `added` (or lost its stored fields), so
292+ -- there is nothing to compare against the bridge. Never delete a device on that basis.
293+ if not device :get_field (Fields .HUE_DEVICE_ID ) then goto continue end
294+ local not_known_to_bridge = device_is_joined_to_bridge [device :get_field (Fields .HUE_DEVICE_ID )]
255295 local parent_device_id = device .parent_device_id or device :get_field (Fields .PARENT_DEVICE_ID ) or " "
256296 local parent_bridge_device = utils .get_hue_bridge_for_device (driver , device , parent_device_id )
257297 local is_child_of_bridge = parent_bridge_device and (parent_bridge_device :get_field (Fields .BRIDGE_ID ) == bridge_network_id )
@@ -357,7 +397,7 @@ function HueDiscovery.handle_discovered_child_device(driver, bridge_network_id,
357397 api_instance ,
358398 primary_services ,
359399 device_info ,
360- HueDiscovery .device_state_disco_cache ,
400+ HueDiscovery .state_cache_for_bridge ( bridge_network_id ) ,
361401 driver .try_create_device
362402 )
363403end
@@ -411,7 +451,7 @@ function HueDiscovery.do_mdns_scan(driver)
411451 (rest_err or " unexpected nil in error position" )
412452 )
413453 )
414- return
454+ goto continue
415455 end
416456 -- '-' and ':' or '::' are the accepted separators used for MAC address segments, so
417457 -- we strip thoes out and make the string uppercase
@@ -421,7 +461,7 @@ function HueDiscovery.do_mdns_scan(driver)
421461 " No MAC address in returned Bridge Info for IP %s." , ip_addr
422462 )
423463 )
424- return
464+ goto continue
425465 end
426466
427467 bridge_info .ip = ip_addr
0 commit comments