forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chime.ts
128 lines (118 loc) · 3.68 KB
/
chime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { RingChime } from '../api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { PlatformAccessory } from 'homebridge'
import { BaseDataAccessory } from './base-data-accessory'
import { logInfo } from '../api/util'
const minutesFor24Hours = 24 * 60
export class Chime extends BaseDataAccessory<RingChime> {
constructor(
public readonly device: RingChime,
public readonly accessory: PlatformAccessory,
public readonly config: RingPlatformConfig
) {
super()
const { Characteristic, Service } = hap,
snoozeService = this.getService(
Service.Switch,
device.name + ' Snooze',
'snooze'
),
playDingService = this.getService(
Service.Switch,
device.name + ' Play Ding',
'play-ding'
),
playMotionService = this.getService(
Service.Switch,
device.name + ' Play Motion',
'play-motion'
)
// Snooze Switch
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: snoozeService,
getValue: (data) => Boolean(data.do_not_disturb.seconds_left),
setValue: (snooze: boolean) => {
if (snooze) {
logInfo(device.name + ' snoozed for 24 hours')
return device.snooze(minutesFor24Hours)
}
logInfo(device.name + ' snooze cleared')
return device.clearSnooze()
},
requestUpdate: () => device.requestUpdate(),
})
snoozeService.setPrimaryService(true)
// Speaker Service
this.registerCharacteristic({
characteristicType: Characteristic.Mute,
serviceType: Service.Speaker,
getValue: () => false,
})
this.registerLevelCharacteristic({
characteristicType: Characteristic.Volume,
serviceType: Service.Speaker,
getValue: (data) => data.settings.volume,
setValue: (volume: number) => device.setVolume(volume),
requestUpdate: () => device.requestUpdate(),
})
this.getService(Service.Speaker)
.getCharacteristic(Characteristic.Volume)
.setProps({
minValue: 0,
maxValue: 11,
})
// Play Sound Switches
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: playDingService,
getValue: () => false,
setValue: (play: boolean) => {
if (!play) {
return
}
setTimeout(() => {
playDingService
.getCharacteristic(Characteristic.On)
.updateValue(false)
}, 1000)
return this.device.playSound('ding')
},
requestUpdate: () => device.requestUpdate(),
})
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: playMotionService,
getValue: () => false,
setValue: (play: boolean) => {
if (!play) {
return
}
setTimeout(() => {
playMotionService
.getCharacteristic(Characteristic.On)
.updateValue(false)
}, 1000)
return this.device.playSound('motion')
},
requestUpdate: () => device.requestUpdate(),
})
// Accessory Information Service
this.registerCharacteristic({
characteristicType: Characteristic.Manufacturer,
serviceType: Service.AccessoryInformation,
getValue: () => 'Ring',
})
this.registerCharacteristic({
characteristicType: Characteristic.Model,
serviceType: Service.AccessoryInformation,
getValue: () => device.model,
})
this.registerCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: Service.AccessoryInformation,
getValue: (data) => data.device_id || 'Unknown',
})
}
}