Merge pull request #161 from SmartThingsCommunity/master

Merge Master -> Staging
This commit is contained in:
Juan Pablo Risso
2015-10-01 11:02:20 -04:00

View File

@@ -68,6 +68,7 @@ def bridgeDiscovery(params=[:])
log.trace "Cleaning old bridges memory" log.trace "Cleaning old bridges memory"
state.bridges = [:] state.bridges = [:]
state.bridgeRefreshCount = 0 state.bridgeRefreshCount = 0
app.updateSetting("selectedHue", "")
} }
subscribe(location, null, locationHandler, [filterEvents:false]) subscribe(location, null, locationHandler, [filterEvents:false])
@@ -131,17 +132,24 @@ def bulbDiscovery() {
state.bulbRefreshCount = bulbRefreshCount + 1 state.bulbRefreshCount = bulbRefreshCount + 1
def refreshInterval = 3 def refreshInterval = 3
state.inBulbDiscovery = true state.inBulbDiscovery = true
def bridge = null
if (selectedHue) {
bridge = getChildDevice(selectedHue)
subscribe(bridge, "bulbList", bulbListData)
}
state.bridgeRefreshCount = 0 state.bridgeRefreshCount = 0
def options = bulbsDiscovered() ?: [] def bulboptions = bulbsDiscovered() ?: [:]
def numFound = options.size() ?: 0 def numFound = bulboptions.size() ?: 0
if (numFound == 0)
app.updateSetting("selectedBulbs", "")
if((bulbRefreshCount % 3) == 0) { if((bulbRefreshCount % 3) == 0) {
discoverHueBulbs() discoverHueBulbs()
} }
return dynamicPage(name:"bulbDiscovery", title:"Bulb Discovery Started!", nextPage:"", refreshInterval:refreshInterval, install:true, uninstall: true) { 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.") { 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 { section {
def title = getBridgeIP() ? "Hue bridge (${getBridgeIP()})" : "Find bridges" def title = getBridgeIP() ? "Hue bridge (${getBridgeIP()})" : "Find bridges"
@@ -223,10 +231,14 @@ Map bulbsDiscovered() {
bulbmap["${key}"] = value bulbmap["${key}"] = value
} }
} }
bulbmap return bulbmap
} }
def getHueBulbs() { def bulbListData(evt) {
state.bulbs = evt.jsonData
}
Map getHueBulbs() {
state.bulbs = state.bulbs ?: [:] state.bulbs = state.bulbs ?: [:]
} }
@@ -252,7 +264,10 @@ def updated() {
def initialize() { def initialize() {
log.debug "Initializing" log.debug "Initializing"
unsubscribe(bridge)
state.inBulbDiscovery = false state.inBulbDiscovery = false
state.bridgeRefreshCount = 0
state.bulbRefreshCount = 0
if (selectedHue) { if (selectedHue) {
addBridge() addBridge()
addBulbs() addBulbs()
@@ -276,9 +291,8 @@ def uninstalled(){
// Handles events to add new bulbs // Handles events to add new bulbs
def bulbListHandler(hub, data = "") { def bulbListHandler(hub, data = "") {
def msg = "Bulbs list not processed. Only while in settings menu." def msg = "Bulbs list not processed. Only while in settings menu."
log.trace "Here: $hub, $data" def bulbs = [:]
if (state.inBulbDiscovery) { if (state.inBulbDiscovery) {
def bulbs = [:]
def logg = "" def logg = ""
log.trace "Adding bulbs to state..." log.trace "Adding bulbs to state..."
state.bridgeProcessedLightList = true state.bridgeProcessedLightList = true
@@ -287,15 +301,18 @@ def bulbListHandler(hub, data = "") {
if (v instanceof Map) if (v instanceof Map)
bulbs[k] = [id: k, name: v.name, type: v.type, hub:hub] 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 return msg
} }
def addBulbs() { def addBulbs() {
def bulbs = getHueBulbs() def bulbs = getHueBulbs()
selectedBulbs.each { dni -> selectedBulbs?.each { dni ->
def d = getChildDevice(dni) def d = getChildDevice(dni)
if(!d) { if(!d) {
def newHueBulb def newHueBulb
@@ -413,8 +430,11 @@ def locationHandler(evt) {
} }
} }
} }
} else { } else {
networkAddress = d.latestState('networkAddress').stringValue if (d.getDeviceDataByName("networkAddress"))
networkAddress = d.getDeviceDataByName("networkAddress")
else
networkAddress = d.latestState('networkAddress').stringValue
log.trace "Host: $host - $networkAddress" log.trace "Host: $host - $networkAddress"
if(host != networkAddress) { if(host != networkAddress) {
log.debug "Device's port or ip changed for device $d..." log.debug "Device's port or ip changed for device $d..."
@@ -422,7 +442,8 @@ def locationHandler(evt) {
dstate.port = port dstate.port = port
dstate.name = "Philips hue ($ip)" dstate.name = "Philips hue ($ip)"
d.sendEvent(name:"networkAddress", value: host) d.sendEvent(name:"networkAddress", value: host)
} d.updateDataValue("networkAddress", host)
}
} }
} }
} }