mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-04-12 14:23:07 +01:00
Fix Ecobee access_token doesn't get refresh after it's expired
This commit is contained in:
@@ -64,3 +64,8 @@ void poll() {
|
|||||||
log.debug "Executing 'poll' using parent SmartApp"
|
log.debug "Executing 'poll' using parent SmartApp"
|
||||||
parent.pollChildren(this)
|
parent.pollChildren(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//generate custom mobile activity feeds event
|
||||||
|
def generateActivityFeedsEvent(notificationMessage) {
|
||||||
|
sendEvent(name: "notificationMessage", value: "$device.displayName $notificationMessage", descriptionText: "$device.displayName $notificationMessage", displayed: true)
|
||||||
|
}
|
||||||
|
|||||||
@@ -676,3 +676,8 @@ def generateStatusEvent() {
|
|||||||
log.debug "Generate Status Event = ${statusText}"
|
log.debug "Generate Status Event = ${statusText}"
|
||||||
sendEvent("name":"thermostatStatus", "value":statusText, "description":statusText, displayed: true)
|
sendEvent("name":"thermostatStatus", "value":statusText, "description":statusText, displayed: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//generate custom mobile activity feeds event
|
||||||
|
def generateActivityFeedsEvent(notificationMessage) {
|
||||||
|
sendEvent(name: "notificationMessage", value: "$device.displayName $notificationMessage", descriptionText: "$device.displayName $notificationMessage", displayed: true)
|
||||||
|
}
|
||||||
|
|||||||
@@ -236,6 +236,7 @@ def getEcobeeThermostats() {
|
|||||||
]
|
]
|
||||||
|
|
||||||
def stats = [:]
|
def stats = [:]
|
||||||
|
try {
|
||||||
httpGet(deviceListParams) { resp ->
|
httpGet(deviceListParams) { resp ->
|
||||||
|
|
||||||
if (resp.status == 200) {
|
if (resp.status == 200) {
|
||||||
@@ -257,6 +258,10 @@ def getEcobeeThermostats() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch(Exception e) {
|
||||||
|
log.debug "___exception getEcobeeThermostats(): " + e
|
||||||
|
refreshAuthToken()
|
||||||
|
}
|
||||||
atomicState.thermostats = stats
|
atomicState.thermostats = stats
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
@@ -338,22 +343,19 @@ def initialize() {
|
|||||||
|
|
||||||
atomicState.thermostatData = [:] //reset Map to store thermostat data
|
atomicState.thermostatData = [:] //reset Map to store thermostat data
|
||||||
|
|
||||||
|
//send activity feeds to tell that device is connected
|
||||||
|
def notificationMessage = "is connected to SmartThings"
|
||||||
|
sendActivityFeeds(notificationMessage)
|
||||||
|
state.timeSendPush = null
|
||||||
|
|
||||||
pollHandler() //first time polling data data from thermostat
|
pollHandler() //first time polling data data from thermostat
|
||||||
|
|
||||||
//automatically update devices status every 5 mins
|
//automatically update devices status every 5 mins
|
||||||
runEvery5Minutes("poll")
|
runEvery5Minutes("poll")
|
||||||
|
|
||||||
}
|
//since access_token expires every 2 hours
|
||||||
|
runEvery1Hour("refreshAuthToken")
|
||||||
|
|
||||||
def uninstalled() {
|
|
||||||
log.info("Uninstalling, removing child devices...")
|
|
||||||
removeChildDevices(getChildDevices())
|
|
||||||
}
|
|
||||||
|
|
||||||
private removeChildDevices(delete) {
|
|
||||||
delete.each {
|
|
||||||
deleteChildDevice(it.deviceNetworkId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def pollHandler() {
|
def pollHandler() {
|
||||||
@@ -594,6 +596,7 @@ private refreshAuthToken() {
|
|||||||
|
|
||||||
log.debug refreshParams
|
log.debug refreshParams
|
||||||
|
|
||||||
|
def notificationMessage = "is disconnected from SmartThings, because the access credential changed or was lost. Please go to the Ecobee (Connect) SmartApp and re-enter your account login credentials."
|
||||||
//changed to httpPost
|
//changed to httpPost
|
||||||
try {
|
try {
|
||||||
def jsonMap
|
def jsonMap
|
||||||
@@ -635,7 +638,10 @@ private refreshAuthToken() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
log.debug "caught exception refreshing auth token: " + e
|
log.error "refreshAuthToken() >> Error: e.statusCode ${e.statusCode}"
|
||||||
|
if (e.statusCode == 401) {
|
||||||
|
sendPushAndFeeds(notificationMessage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -727,7 +733,7 @@ def sendJson(child = null, String jsonBody) {
|
|||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
log.debug "Exception Sending Json: " + e
|
log.debug "Exception Sending Json: " + e
|
||||||
debugEvent ("Exception Sending JSON: " + e)
|
debugEvent ("Exception Sending JSON: " + e)
|
||||||
// debugEventFromParent(child, "Exception Sending JSON: " + e)
|
refreshAuthToken()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -761,3 +767,28 @@ def debugEvent(message, displayEvent = false) {
|
|||||||
def debugEventFromParent(child, message) {
|
def debugEventFromParent(child, message) {
|
||||||
if (child != null) { child.sendEvent("name":"debugEventFromParent", "value":message, "description":message, displayed: true, isStateChange: true)}
|
if (child != null) { child.sendEvent("name":"debugEventFromParent", "value":message, "description":message, displayed: true, isStateChange: true)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//send both push notification and mobile activity feeds
|
||||||
|
def sendPushAndFeeds(notificationMessage){
|
||||||
|
log.warn "sendPushAndFeeds >> notificationMessage: ${notificationMessage}"
|
||||||
|
log.warn "sendPushAndFeeds >> atomicState.timeSendPush: ${atomicState.timeSendPush}"
|
||||||
|
if (atomicState.timeSendPush){
|
||||||
|
if (now() - atomicState.timeSendPush > 86400000){ // notification is sent to remind user once a day
|
||||||
|
sendPush("Your Ecobee thermostat " + notificationMessage)
|
||||||
|
sendActivityFeeds(notificationMessage)
|
||||||
|
atomicState.timeSendPush = now()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendPush("Your Ecobee thermostat " + notificationMessage)
|
||||||
|
sendActivityFeeds(notificationMessage)
|
||||||
|
atomicState.timeSendPush = now()
|
||||||
|
}
|
||||||
|
atomicState.authToken = null
|
||||||
|
}
|
||||||
|
|
||||||
|
def sendActivityFeeds(notificationMessage) {
|
||||||
|
def devices = getChildDevices()
|
||||||
|
devices.each { child ->
|
||||||
|
child.generateActivityFeedsEvent(notificationMessage) //parse received message from parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user