forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic-buttons.ts
93 lines (82 loc) · 2.8 KB
/
panic-buttons.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
import { RingDevice, RingDeviceData, AlarmState } from '../api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { BaseDataAccessory } from './base-data-accessory'
import { Logging, PlatformAccessory } from 'homebridge'
const burglarStates: AlarmState[] = [
'burglar-alarm',
'user-verified-burglar-alarm',
'burglar-accelerated-alarm',
],
fireStates: AlarmState[] = [
'fire-alarm',
'user-verified-co-or-fire-alarm',
'fire-accelerated-alarm',
]
function matchesAnyAlarmState(
{ alarmInfo }: RingDeviceData,
targetStates: AlarmState[]
) {
return Boolean(alarmInfo && targetStates.includes(alarmInfo.state))
}
export class PanicButtons extends BaseDataAccessory<RingDevice> {
constructor(
public readonly device: RingDevice,
public readonly accessory: PlatformAccessory,
public readonly logger: Logging,
public readonly config: RingPlatformConfig
) {
super()
const { Characteristic, Service } = hap,
locationName = device.location.name
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Switch,
serviceSubType: 'Burglar',
name: 'Burglar Alarm',
getValue: (data) => matchesAnyAlarmState(data, burglarStates),
setValue: (on) => {
if (on) {
this.logger.info(`Burglar Alarm activated for ${locationName}`)
return this.device.location.triggerBurglarAlarm()
}
this.logger.info(`Burglar Alarm turned off for ${locationName}`)
return this.device.location.setAlarmMode('none')
},
})
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Switch,
serviceSubType: 'Fire',
name: 'Fire Alarm',
getValue: (data) => matchesAnyAlarmState(data, fireStates),
setValue: (on) => {
if (on) {
this.logger.info(`Fire Alarm activated for ${locationName}`)
return this.device.location.triggerFireAlarm()
}
this.logger.info(`Fire Alarm turned off for ${locationName}`)
return this.device.location.setAlarmMode('none')
},
})
}
initBase() {
const { Characteristic, Service } = hap
this.registerCharacteristic({
characteristicType: Characteristic.Manufacturer,
serviceType: Service.AccessoryInformation,
getValue: (data) => data.manufacturerName || 'Ring',
})
this.registerCharacteristic({
characteristicType: Characteristic.Model,
serviceType: Service.AccessoryInformation,
getValue: () => 'Panic Buttons for ' + this.device.location.name,
})
this.registerCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: Service.AccessoryInformation,
getValue: () => 'None',
})
super.initBase()
}
}