Harmony Hub Integration

I see the exact same thing. However, if you wait long enough, it should synchronize. In fact, I see the exact same behavior within SmartThings.

The biggest downside I can see for this is that you cannot use the status of the Harmony Activity switch devices to trigger other actions within Hubitat as there is too much of a delay (up to 5 minutes).

Here is the section of code which appears to be called every 5 minutes (there is a runIn(5, ...) command in the second routine below. I guess you could just reduce this time to every 1 minute, but you will be hitting the Logitech server 5 times more frequently.

def poll() {
	// GET THE LIST OF ACTIVITIES
    if (state.HarmonyAccessToken) {
        def tokenParam = [auth: state.HarmonyAccessToken]
        def params = [
            uri: "https://home.myharmony.com/cloudapi/state?${toQueryString(tokenParam)}",
            headers: ["Accept": "application/json"],
            contentType: 'application/json'
        ]
        //asynchttp_v1.get('pollResponse', params)
        httpGet(params) { response -> pollResponse(response) }
        
      } else {
        log.warn "Harmony - Access token has expired"
      }
}

def pollResponse(response) {
	if (response.status != 200) {
	    log.error "Harmony - response has error: $response.errorMessage"
	    if (response.status == 401) { // token is expired
			state.remove("HarmonyAccessToken")
			log.warn "Harmony - Access token has expired"
	    }
	} else {
		def ResponseValues
		try {
			// json response already parsed into JSONElement object
			ResponseValues = response.data
		} catch (e) {
			log.error "Harmony - error parsing json from response: $e"
		}
		if (ResponseValues) {
	        def map = [:]
	        ResponseValues.hubs.each {
		        // Device-Watch relies on the Logitech Harmony Cloud to get the Device state.
		        def isAlive = it.value.status
		        def d = getChildDevice("harmony-${it.key}")
		        d?.sendEvent(name: "DeviceWatch-DeviceStatus", value: isAlive!=504? "online":"offline", displayed: false, isStateChange: true)
		        if (it.value.message == "OK") {
					map["${it.key}"] = "${it.value.response.data.currentAvActivity},${it.value.response.data.activityStatus}"
					def hub = getChildDevice("harmony-${it.key}")
					if (hub) {
						if (it.value.response.data.currentAvActivity == "-1") {
							hub.sendEvent(name: "currentActivity", value: "--", descriptionText: "There isn't any activity running", displayed: false)
						} else {
							def currentActivity
							def activityDTH = getChildDevice("harmony-${it.key}-${it.value.response.data.currentAvActivity}")
							if (activityDTH)
								currentActivity = activityDTH.device.displayName
							else
								currentActivity = getActivityName(it.value.response.data.currentAvActivity,it.key)
							hub.sendEvent(name: "currentActivity", value: currentActivity, descriptionText: "Current activity is ${currentActivity}", displayed: false)
						}
					}
	          	} else {
	            	log.trace "Harmony - error response: $it.value.message"
	          	}
        	}
	        def activities = getChildDevices()
	        def activitynotrunning = true
	        activities.each { activity ->
	            def act = activity.deviceNetworkId.split('-')
	            if (act.size() > 2) {
	                def aux = map.find { it.key == act[1] }
	                if (aux) {
	                    def aux2 = aux.value.split(',')
	                    def childDevice = getChildDevice(activity.deviceNetworkId)
	                    if ((act[2] == aux2[0]) && (aux2[1] == "1" || aux2[1] == "2")) {
                            if(childDevice?.currentSwitch != "on")
	                        	childDevice?.sendEvent(name: "switch", value: "on")
	                        if (aux2[1] == "1")
	                            runIn(5, "poll", [overwrite: true])
	                    } else {
                            if(childDevice?.currentSwitch != "off")
	                        	childDevice?.sendEvent(name: "switch", value: "off")
	                        if (aux2[1] == "3")
	                            runIn(5, "poll", [overwrite: true])
	                    }
	                }
	            }
	        }
		} else {
			log.debug "Harmony - did not get json results from response body: $response.data"
		}
	}
}
2 Likes