From 32b4914ba0b1440a78ff469afad1d1522db11ad2 Mon Sep 17 00:00:00 2001 From: Juan Pablo Risso Date: Tue, 29 Sep 2015 17:16:55 -0400 Subject: [PATCH] Hue - Fix bulb discovery --- .../hue-connect.src/hue-connect.groovy | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/smartapps/smartthings/hue-connect.src/hue-connect.groovy b/smartapps/smartthings/hue-connect.src/hue-connect.groovy index 9833438..d68a753 100644 --- a/smartapps/smartthings/hue-connect.src/hue-connect.groovy +++ b/smartapps/smartthings/hue-connect.src/hue-connect.groovy @@ -68,6 +68,7 @@ def bridgeDiscovery(params=[:]) log.trace "Cleaning old bridges memory" state.bridges = [:] state.bridgeRefreshCount = 0 + app.updateSetting("selectedHue", "") } subscribe(location, null, locationHandler, [filterEvents:false]) @@ -131,17 +132,24 @@ def bulbDiscovery() { state.bulbRefreshCount = bulbRefreshCount + 1 def refreshInterval = 3 state.inBulbDiscovery = true + def bridge = null + if (selectedHue) { + bridge = getChildDevice(selectedHue) + subscribe(bridge, "bulbList", bulbListData) + } state.bridgeRefreshCount = 0 - def options = bulbsDiscovered() ?: [] - def numFound = options.size() ?: 0 - + def bulboptions = bulbsDiscovered() ?: [:] + def numFound = bulboptions.size() ?: 0 + if (numFound == 0) + app.updateSetting("selectedBulbs", "") + if((bulbRefreshCount % 3) == 0) { discoverHueBulbs() } return dynamicPage(name:"bulbDiscovery", title:"Bulb Discovery Started!", nextPage:"", refreshInterval:refreshInterval, install:true, uninstall: true) { section("Please wait while we discover your Hue Bulbs. Discovery can take five minutes or more, so sit back and relax! Select your device below once discovered.") { - input "selectedBulbs", "enum", required:false, title:"Select Hue Bulbs (${numFound} found)", multiple:true, options:options + input "selectedBulbs", "enum", required:false, title:"Select Hue Bulbs (${numFound} found)", multiple:true, options:bulboptions } section { def title = getBridgeIP() ? "Hue bridge (${getBridgeIP()})" : "Find bridges" @@ -223,10 +231,14 @@ Map bulbsDiscovered() { bulbmap["${key}"] = value } } - bulbmap + return bulbmap } -def getHueBulbs() { +def bulbListData(evt) { + state.bulbs = evt.jsonData +} + +Map getHueBulbs() { state.bulbs = state.bulbs ?: [:] } @@ -252,7 +264,10 @@ def updated() { def initialize() { log.debug "Initializing" + unsubscribe(bridge) state.inBulbDiscovery = false + state.bridgeRefreshCount = 0 + state.bulbRefreshCount = 0 if (selectedHue) { addBridge() addBulbs() @@ -276,9 +291,8 @@ def uninstalled(){ // Handles events to add new bulbs def bulbListHandler(hub, data = "") { def msg = "Bulbs list not processed. Only while in settings menu." - log.trace "Here: $hub, $data" - if (state.inBulbDiscovery) { - def bulbs = [:] + def bulbs = [:] + if (state.inBulbDiscovery) { def logg = "" log.trace "Adding bulbs to state..." state.bridgeProcessedLightList = true @@ -287,15 +301,18 @@ def bulbListHandler(hub, data = "") { if (v instanceof Map) bulbs[k] = [id: k, name: v.name, type: v.type, hub:hub] } - state.bulbs = bulbs - msg = "${bulbs.size()} bulbs found. $state.bulbs" - } + } + def bridge = null + if (selectedHue) + bridge = getChildDevice(selectedHue) + bridge.sendEvent(name: "bulbList", value: hub, data: bulbs, isStateChange: true, displayed: false) + msg = "${bulbs.size()} bulbs found. ${bulbs}" return msg } def addBulbs() { def bulbs = getHueBulbs() - selectedBulbs.each { dni -> + selectedBulbs?.each { dni -> def d = getChildDevice(dni) if(!d) { def newHueBulb @@ -413,8 +430,11 @@ def locationHandler(evt) { } } } - } else { - networkAddress = d.latestState('networkAddress').stringValue + } else { + if (d.getDeviceDataByName("networkAddress")) + networkAddress = d.getDeviceDataByName("networkAddress") + else + networkAddress = d.latestState('networkAddress').stringValue log.trace "Host: $host - $networkAddress" if(host != networkAddress) { log.debug "Device's port or ip changed for device $d..." @@ -422,7 +442,8 @@ def locationHandler(evt) { dstate.port = port dstate.name = "Philips hue ($ip)" d.sendEvent(name:"networkAddress", value: host) - } + d.updateDataValue("networkAddress", host) + } } } }