Skip to content

Commit

Permalink
Add onDing observable to intercoms
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Dec 12, 2022
1 parent 836c580 commit c084848
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-melons-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ring-client-api': minor
---

Add onDing observable to intercoms, which will be triggered by ding push notifications
21 changes: 14 additions & 7 deletions packages/ring-client-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,22 @@ export class RingApi extends Subscribed {
}
}

async registerPushReceiver(cameras: RingCamera[]) {
private async registerPushReceiver(
cameras: RingCamera[],
intercoms: RingIntercom[]
) {
const pushReceiver = new PushReceiver({
logLevel: 'NONE',
senderId: '876313859327', // for Ring android app. 703521446232 for ring-site
}),
camerasById = cameras.reduce((byId, camera) => {
byId[camera.id] = camera
return byId
}, {} as { [id: number]: RingCamera })
devicesById: { [id: number]: RingCamera | RingIntercom } = {}

for (const camera of cameras) {
devicesById[camera.id] = camera
}
for (const intercom of intercoms) {
devicesById[intercom.id] = intercom
}

pushReceiver.onCredentialsChanged(
async ({
Expand Down Expand Up @@ -259,7 +266,7 @@ export class RingApi extends Subscribed {
return
}

camerasById[notification.ding.doorbot_id]?.processPushNotification(
devicesById[notification.ding.doorbot_id]?.processPushNotification(
notification
)
} catch (e) {
Expand Down Expand Up @@ -357,7 +364,7 @@ export class RingApi extends Subscribed {
)

this.listenForDeviceUpdates(cameras, ringChimes, ringIntercoms)
this.registerPushReceiver(cameras).catch((e) => {
this.registerPushReceiver(cameras, ringIntercoms).catch((e) => {
logError(e)
})

Expand Down
13 changes: 12 additions & 1 deletion packages/ring-client-api/ring-intercom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IntercomHandsetAudioData } from './ring-types'
import {
IntercomHandsetAudioData,
PushNotification,
PushNotificationAction,
} from './ring-types'
import { clientApi, commandsApi, RingRestClient } from './rest-client'
import { BehaviorSubject, Subject } from 'rxjs'
import { distinctUntilChanged, map } from 'rxjs/operators'
Expand All @@ -11,6 +15,7 @@ export class RingIntercom {
onData
onRequestUpdate = new Subject()
onBatteryLevel
onDing = new Subject<void>()

constructor(
private initialData: IntercomHandsetAudioData,
Expand Down Expand Up @@ -96,4 +101,10 @@ export class RingIntercom {
url: this.doorbotUrl('unsubscribe'),
})
}

processPushNotification(notification: PushNotification) {
if (notification.action === PushNotificationAction.Ding) {
this.onDing.next()
}
}
}
2 changes: 2 additions & 0 deletions packages/ring-client-api/ring-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,8 @@ export enum NotificationDetectionType {
export enum PushNotificationAction {
Ding = 'com.ring.push.HANDLE_NEW_DING',
Motion = 'com.ring.push.HANDLE_NEW_motion',
LowBattery = 'com.ring.push.LOW_BATTERY_ALERT',
IntercomUnlock = 'com.ring.push.INTERCOM_UNLOCK_FROM_APP',
}

export interface PushNotification {
Expand Down

0 comments on commit c084848

Please # to comment.