Skip to content

Commit

Permalink
feat(homebridge): inform HomeKit of failure to arm if bypass required
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Jul 28, 2019
1 parent 67649d8 commit e53d317
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
23 changes: 20 additions & 3 deletions api/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
publishReplay,
refCount,
scan,
skip,
take
} from 'rxjs/operators'
import { delay, logError, logInfo } from './util'
Expand Down Expand Up @@ -329,14 +330,30 @@ export class Location {

async sendCommandToSecurityPanel(commandType: string, data?: {}) {
const securityPanel = await this.getSecurityPanel()
return securityPanel.sendCommand(commandType, data)
securityPanel.sendCommand(commandType, data)
}

setAlarmMode(alarmMode: AlarmMode, bypassSensorZids?: string[]) {
return this.sendCommandToSecurityPanel('security-panel.switch-mode', {
async setAlarmMode(alarmMode: AlarmMode, bypassSensorZids?: string[]) {
const securityPanel = await this.getSecurityPanel(),
updatedDataPromise = securityPanel.onData
.pipe(
skip(1),
take(1)
)
.toPromise()

await this.sendCommandToSecurityPanel('security-panel.switch-mode', {
mode: alarmMode,
bypass: bypassSensorZids
})

const updatedData = await updatedDataPromise

if (updatedData.mode !== alarmMode) {
throw new Error(
`Failed to set alarm mode to "${alarmMode}". Sensors may require bypass, which can only be done in the Ring app.`
)
}
}

soundSiren() {
Expand Down
14 changes: 12 additions & 2 deletions homebridge/base-accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,25 @@ export abstract class BaseAccessory<T extends RingDevice | RingCamera> {
}
})

if (setValue) {
if (setValue && setValueDebounceTime) {
const onValueToSet = new Subject<any>()

characteristic.on('set', async (newValue, callback) => {
characteristic.on('set', (newValue, callback) => {
onValueToSet.next(newValue)
callback()
})

onValueToSet.pipe(debounceTime(setValueDebounceTime)).subscribe(setValue)
} else if (setValue) {
characteristic.on('set', async (newValue, callback) => {
try {
await Promise.resolve(setValue(newValue))
callback()
} catch (e) {
this.logger.error(e)
callback(e)
}
})
}

;(this.device.onData as Observable<T['data']>)
Expand Down
6 changes: 3 additions & 3 deletions homebridge/security-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ export class SecurityPanel extends BaseDeviceAccessory {

if (state === State.AWAY_ARM) {
this.logger.info(`Arming (Away) ${this.device.name}`)
location.armAway()
return location.armAway()
} else if (state === State.DISARM) {
this.logger.info(`Disarming ${this.device.name}`)
location.disarm()
return location.disarm()
} else {
this.logger.info(`Arming (Home) ${this.device.name}`)
location.armHome()
return location.armHome()
}
}

Expand Down

0 comments on commit e53d317

Please sign in to comment.