Skip to content

Commit

Permalink
Update bulb health when bridge health changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dljsjr committed Aug 22, 2023
1 parent 492d6ab commit c30c4e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/SmartThings/philips-hue/src/hue/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions drivers/SmartThings/philips-hue/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c30c4e1

Please sign in to comment.