Skip to content

Commit

Permalink
Merge pull request #931 from SmartThingsCommunity/fix/hue-dont-emit-e…
Browse files Browse the repository at this point in the history
…vents-for-offline-bulbs
  • Loading branch information
dljsjr authored Oct 19, 2023
2 parents 63214e1 + 18aaccb commit b4b4fd0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
20 changes: 9 additions & 11 deletions drivers/SmartThings/philips-hue/src/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,19 @@ local function do_refresh_light(driver, light_device, conn_status_cache, light_s

local do_zigbee_request = true
local do_light_request = true
local light_online = true

if type(conn_status_cache) == "table" then
local zigbee_status = conn_status_cache[hue_device_id]
if zigbee_status ~= nil then
if zigbee_status.status and zigbee_status.status == "connected" then
if zigbee_status ~= nil and zigbee_status.status ~= nil then
do_zigbee_request = false
if zigbee_status.status == "connected" then
light_device.log.debug(string.format("Zigbee Status for %s is connected", light_device.label))
light_device:online()
do_zigbee_request = false
light_device:set_field(Fields.IS_ONLINE, true)
else
light_device.log.debug(string.format("Zigbee Status for %s is not connected", light_device.label))
light_device:set_field(Fields.IS_ONLINE, false)
light_device:offline()
do_zigbee_request = false
light_online = false
end
end
end
Expand Down Expand Up @@ -359,18 +358,19 @@ local function do_refresh_light(driver, light_device, conn_status_cache, light_s
if zigbee_svc.status and zigbee_svc.status == "connected" then
light_device.log.debug(string.format("Zigbee Status for %s is connected", light_device.label))
light_device:online()
light_device:set_field(Fields.IS_ONLINE, true)
else
light_device.log.debug(string.format("Zigbee Status for %s is not connected", light_device.label))
light_device:set_field(Fields.IS_ONLINE, false)
light_device:offline()
light_online = false
end
end
end
end
end
end

if do_light_request then
if do_light_request and light_device:get_field(Fields.IS_ONLINE) then
rest_resp, rest_err = hue_api:get_light_by_id(light_resource_id)
if rest_err ~= nil then
log.error_with({ hub_logs = true }, rest_err)
Expand All @@ -390,9 +390,7 @@ local function do_refresh_light(driver, light_device, conn_status_cache, light_s
if light_info.color ~= nil and light_info.color.gamut then
light_device:set_field(Fields.GAMUT, light_info.color.gamut_type, { persist = true })
end
if light_online then
driver.emit_light_status_events(light_device, light_info)
end
driver.emit_light_status_events(light_device, light_info)
success = true
end
end
Expand Down
1 change: 1 addition & 0 deletions drivers/SmartThings/philips-hue/src/hue/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local Fields = {
MIN_DIMMING = "mindim",
MIN_KELVIN = "mintemp",
MODEL_ID = "modelid",
IS_ONLINE = "is_online",
PARENT_DEVICE_ID = "parent_device_id_local",
RESOURCE_ID = "rid",
WRAPPED_HUE = "_wrapped_hue"
Expand Down
18 changes: 15 additions & 3 deletions drivers/SmartThings/philips-hue/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ local function emit_light_status_events(light_device, light)
if light.status == "connected" then
light_device.log.info_with({hub_logs=true}, "Light status event, marking device online")
light_device:online()
light_device:set_field(Fields.IS_ONLINE, true)
elseif light.status == "connectivity_issue" then
light_device.log.info_with({hub_logs=true}, "Light status event, marking device offline")
light_device:set_field(Fields.IS_ONLINE, false)
light_device:offline()
return
end
end

if light_device:get_field(Fields.IS_ONLINE) ~= true then
return
end

if light.mode then
light_device:emit_event(hueSyncMode.mode(light.mode))
end
Expand Down Expand Up @@ -707,7 +713,9 @@ local function do_bridge_network_init(driver, device, bridge_url, api_key)
local bridge_api = device:get_field(Fields.BRIDGE_API)
cosock.spawn(function()
local child_device_map = {}
for _, device_record in ipairs(device:get_child_list()) do
local children = device:get_child_list()
device.log.debug(string.format("Scanning connectivity of %s child devices", #children))
for _, device_record in ipairs(children) 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
Expand Down Expand Up @@ -750,10 +758,12 @@ local function do_bridge_network_init(driver, device, bridge_url, api_key)
local child_device = child_device_map[hue_device_id]
if child_device then
if status.status == "connected" then
child_device.log.trace("Marking Online after SSE Reconnect")
child_device.log.info_with({hub_logs=true}, "Marking Online after SSE Reconnect")
child_device:online()
child_device:set_field(Fields.IS_ONLINE, true)
elseif status.status == "connectivity_issue" then
child_device.log.trace("Marking Offline after SSE Reconnect")
child_device.log.info_with({hub_logs=true}, "Marking Offline after SSE Reconnect")
child_device:set_field(Fields.IS_ONLINE, false)
child_device:offline()
end
end
Expand All @@ -767,6 +777,7 @@ local function do_bridge_network_init(driver, device, bridge_url, api_key)
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:set_field(Fields.IS_ONLINE, false)
device_record:offline()
end

Expand Down Expand Up @@ -822,6 +833,7 @@ local function do_bridge_network_init(driver, device, bridge_url, api_key)
(device.label or device.device_network_id or device.id or "unknown bridge")
)
)
light_device:set_field(Fields.IS_ONLINE, false)
light_device:offline()
end
end
Expand Down

0 comments on commit b4b4fd0

Please sign in to comment.