diff --git a/drivers/SmartThings/philips-hue/src/handlers.lua b/drivers/SmartThings/philips-hue/src/handlers.lua index ab1fe7ad51..7c89b134b2 100644 --- a/drivers/SmartThings/philips-hue/src/handlers.lua +++ b/drivers/SmartThings/philips-hue/src/handlers.lua @@ -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 @@ -359,10 +358,11 @@ 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 @@ -370,7 +370,7 @@ local function do_refresh_light(driver, light_device, conn_status_cache, light_s 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) @@ -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 diff --git a/drivers/SmartThings/philips-hue/src/hue/fields.lua b/drivers/SmartThings/philips-hue/src/hue/fields.lua index 01ce3224d8..9ea0ad10af 100644 --- a/drivers/SmartThings/philips-hue/src/hue/fields.lua +++ b/drivers/SmartThings/philips-hue/src/hue/fields.lua @@ -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" diff --git a/drivers/SmartThings/philips-hue/src/init.lua b/drivers/SmartThings/philips-hue/src/init.lua index 5d7ed71f5f..b6cb02a2be 100644 --- a/drivers/SmartThings/philips-hue/src/init.lua +++ b/drivers/SmartThings/philips-hue/src/init.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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