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 78709e3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
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
50 changes: 49 additions & 1 deletion drivers/SmartThings/philips-hue/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ light_added = function(driver, device, parent_device_id, resource_id)
device:set_field(Fields._ADDED, true, { persist = true })

driver.light_id_to_device[device_light_resource_id] = device

device:online()
-- the refresh handler adds lights that don't have a fully initialized bridge to a queue.
handlers.refresh_handler(driver, device)
end
Expand All @@ -700,10 +702,57 @@ 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()

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:",
device.label
)
)
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.onerror = function()
log.error_with({ hub_logs = true }, string.format("Hue Bridge \"%s\" Event Source Error", device.label))

for _, device_record in ipairs(device:get_child_list()) do
device_record:offline()
end

device:offline()
end

Expand Down Expand Up @@ -920,7 +969,6 @@ init_light = function(driver, device)
handlers.refresh_handler(driver, device)
device:set_field(Fields._REFRESH_AFTER_INIT, false, { persist = true })
end
device:online()
end

---@param driver HueDriver
Expand Down

0 comments on commit 78709e3

Please sign in to comment.