Skip to content

Commit

Permalink
Avoid mode change yoyo after Hubitat mode change
Browse files Browse the repository at this point in the history
Avoid processing update to same mode
Improve log output in different socket disconnection scenarios
Fix checking of current attribute values
  • Loading branch information
jorhett committed Mar 24, 2020
1 parent 92b1374 commit ea2f563
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
54 changes: 36 additions & 18 deletions AbodeAlarm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,23 @@ private clearLoginState() {
sendEvent(name: 'isLoggedIn', value: false, displayed: true)
}

// Send a request to change mode to Abode
private changeMode(String new_mode) {
current_mode = mode
if(current_mode != new_mode) {
if(new_mode != device.currentValue('gatewayMode')) {
// Only update area 1 since area is not returned in event messages
reply = doHttpRequest('PUT','/api/v1/panel/mode/1/' + new_mode)
if (reply['area'] == 1)
log.info "Successfully sent request to change gateway mode to ${new_mode}"
if (reply['area'] == '1') {
log.info "Sent request to change Abode gateway mode to ${new_mode}"
state.localModeChange = new_mode
}
} else {
if (logDebug) log.debug "Gateway is already in mode ${new_mode}"
}
}

// Only update area 1 since area is not provided in event messages
// Process an update from Abode that the mode has changed
private updateMode(String new_mode) {
if (logDebug) log.info 'Gateway mode has changed to ' + new_mode
log.info 'Abode gateway mode has changed to ' + new_mode
sendEvent(name: "gatewayMode", value: new_mode, descriptionText: 'Gateway mode has changed to ' + new_mode, displayed: true)

// Set isArmed?
Expand All @@ -219,10 +222,20 @@ private updateMode(String new_mode) {
isArmed.off()
else {
isArmed.on()
if (targetModeAway && new_mode == 'away')
location.setMode(targetModeAway)
else if (targetModeHome)
location.setMode(targetModeHome)

// Avoid changing the mode if it's a rebound from a local action
if (new_mode == state.localModeChange) {
state.remove('localModeChange')
} else {
if (targetModeAway && new_mode == 'away') {
log.info 'Changing Hubitat mode to ' + new_mode
location.setMode(targetModeAway)
}
else if (targetModeHome) {
log.info 'Changing Hubitat mode to ' + new_mode
location.setMode(targetModeHome)
}
}
}
}

Expand Down Expand Up @@ -285,7 +298,7 @@ private parseMode(Map mode, Set areas) {
modeMap[number] = mode["area_${number}"]
}
// Status is based on area 1 only
if (gatewayMode != modeMap['1'])
if (device.currentValue('gatewayMode') != modeMap['1'])
sendEvent(name: "gatewayMode", value: modeMap['1'], descriptionText: "Gateway mode is ${modeMap['1']}", displayed: true)

state.modes = modeMap
Expand Down Expand Up @@ -359,7 +372,7 @@ private doHttpRequest(String method, String path, Map body = [:]) {
}

// Abode event websocket handling
private connectEventSocket() {
def connectEventSocket() {
if (!state.webSocketConnectAttempt) state.webSocketConnectAttempt = 0
if (logDebug) log.debug "Attempting WebSocket connection for Abode events (attempt ${state.webSocketConnectAttempt})"
try {
Expand Down Expand Up @@ -388,10 +401,10 @@ private terminateEventSocket() {
}
}

// failure handler: validate state and reconnect in 5 seconds
private restartEventSocket() {
terminateEventSocket()
refresh()
runInMillis(30000, connectEventSocket) // Try connect again in 30 seconds
runInMillis(5000, refresh)
}

def sendPing() {
Expand All @@ -408,11 +421,16 @@ def receivePong() {
runInMillis(state.webSocketPingInterval, sendPing)
}

// This is called every 5 minutes whether we are connected or not
def checkSocketTimeout() {
responseTimeout = state.lastMsgReceived + state.webSocketPingTimeout + (timeoutSlack*1000)
if (now() > responseTimeout) {
log.warn 'Socket ping timeout - Disconnecting Abode event socket'
restartEventSocket()
if (state.webSocketConnected) {
responseTimeout = state.lastMsgReceived + state.webSocketPingTimeout + (timeoutSlack*1000)
if (now() > responseTimeout) {
log.warn 'Socket ping timeout - Disconnecting Abode event socket'
restartEventSocket()
}
} else {
connectEventSocket()
}
}

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)

## 2020-03-24 Beta Release 0.6.1

### Changed

- Suppress Hubitat mode change when Abode mode was set from Hubitat
- Avoid processing Abode mode update to same mode
- Improve log output in different socket disconnection scenarios

## 2020-03-21 Beta Release 0.6.0

### Added
Expand Down

0 comments on commit ea2f563

Please sign in to comment.