diff --git a/drivers/SmartThings/philips-hue/src/hue/api.lua b/drivers/SmartThings/philips-hue/src/hue/api.lua index 3099609bd0..8a9e7aebb7 100644 --- a/drivers/SmartThings/philips-hue/src/hue/api.lua +++ b/drivers/SmartThings/philips-hue/src/hue/api.lua @@ -250,6 +250,8 @@ function PhilipsHueApi:get_lights() return do_get(self, "/clip/v2/resource/light function PhilipsHueApi:get_devices() return do_get(self, "/clip/v2/resource/device") end +function PhilipsHueApi:get_connectivity_status() return do_get(self, "/clip/v2/resource/zigbee_connectivity") end + function PhilipsHueApi:get_rooms() return do_get(self, "/clip/v2/resource/room") end function PhilipsHueApi:get_light_by_id(id) diff --git a/drivers/SmartThings/philips-hue/src/init.lua b/drivers/SmartThings/philips-hue/src/init.lua index 0cd4bdc61c..6263ebab48 100644 --- a/drivers/SmartThings/philips-hue/src/init.lua +++ b/drivers/SmartThings/philips-hue/src/init.lua @@ -700,11 +700,48 @@ local function do_bridge_network_init(driver, device, bridge_url, api_key) log.info_with({ hub_logs = true }, string.format("Event Source Connection for Hue Bridge \"%s\" established, marking online", device.label)) device:online() + + end eventsource.onerror = function() log.error_with({ hub_logs = true }, string.format("Hue Bridge \"%s\" Event Source Error", device.label)) + device:offline() + local bridge_api = device:get_field(Fields.BRIDGE_API) + local child_device_map = {} + for _, device_record in ipairs(device:get_child_list()) do + local hue_device_id = device_record:get_field(Fields.HUE_DEVICE_ID) + if hue_device_id ~= nil then + child_device_map[hue_device_id] = device_record + end + end + + local connectivity_status, rest_err = bridge_api:get_connectivity_status() + if rest_err ~= nil then + log.error(string.format("Couldn't query Hue Bridge %s for zigbee connectivity status for child devices: %s", device.label, rest_err)) + return + end + + if connectivity_status.errors and #connectivity_status.errors > 0 then + log.error(string.format("Hue Bridge %s replied with the following error message(s) when querying child device connectivity status:")) + for _, err in ipairs(connectivity_status.errors) do + log.error(string.format("--- %s", err)) + end + return + end + + for _, status in ipairs(connectivity_status.data) do + local hue_device_id = (status.owner and status.owner.rid) or "" + local child_device = child_device_map[hue_device_id] + if child_device then + if status.status and status.status == "connected" then + child_device:online() + else + child_device:offline() + end + end + end end eventsource.onmessage = function(msg)