forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security-panel.ts
167 lines (146 loc) · 5.02 KB
/
security-panel.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { BaseDeviceAccessory } from './base-device-accessory'
import {
AlarmMode,
AlarmState,
allAlarmStates,
RingDevice,
RingDeviceData,
RingDeviceType,
} from '../api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { Logging, PlatformAccessory } from 'homebridge'
function isValidNightModeBypass(mode?: AlarmMode) {
return mode && (mode === 'all' || mode === 'some')
}
export class SecurityPanel extends BaseDeviceAccessory {
private alarmStates: AlarmState[] = this.config.alarmOnEntryDelay
? allAlarmStates
: allAlarmStates.filter((x) => x !== 'entry-delay')
constructor(
public readonly device: RingDevice,
public readonly accessory: PlatformAccessory,
public readonly logger: Logging,
public readonly config: RingPlatformConfig
) {
super()
const { Characteristic, Service } = hap,
validValues = [
Characteristic.SecuritySystemTargetState.AWAY_ARM,
Characteristic.SecuritySystemTargetState.STAY_ARM,
Characteristic.SecuritySystemTargetState.DISARM,
]
if (isValidNightModeBypass(config.nightModeBypassFor)) {
validValues.push(Characteristic.SecuritySystemTargetState.NIGHT_ARM)
}
this.registerCharacteristic({
characteristicType: Characteristic.SecuritySystemCurrentState,
serviceType: Service.SecuritySystem,
getValue: (data) => this.getCurrentState(data),
})
this.registerCharacteristic({
characteristicType: Characteristic.SecuritySystemTargetState,
serviceType: Service.SecuritySystem,
getValue: (data) => this.getCurrentState(data),
setValue: (value) => this.setTargetState(value),
})
this.getService(Service.SecuritySystem)
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.setProps({ validValues })
if (!config.hideAlarmSirenSwitch) {
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Switch,
name: this.device.name + ' Siren',
getValue: (data) => data.siren && data.siren.state === 'on',
setValue: (value) => {
if (value) {
return this.device.location.soundSiren()
}
return this.device.location.silenceSiren()
},
})
}
this.getService(Service.SecuritySystem).setPrimaryService(true)
}
getTargetNightMode() {
return this.targetingNightMode && this.config.nightModeBypassFor
}
getCurrentState({ mode, alarmInfo }: RingDeviceData) {
const {
Characteristic: { SecuritySystemCurrentState: State },
} = hap
if (alarmInfo && this.alarmStates.includes(alarmInfo.state)) {
return State.ALARM_TRIGGERED
}
if (mode === this.getTargetNightMode()) {
setTimeout(() => {
// clear in next tick so that Target and Current state both get night mode
this.targetingNightMode = false
})
return State.NIGHT_ARM
}
switch (mode) {
case 'all':
return State.AWAY_ARM
case 'some':
return State.STAY_ARM
case 'none':
return State.DISARMED
default:
return State.DISARMED
}
}
private targetingNightMode = false
async setTargetState(state: any) {
const {
Characteristic: { SecuritySystemTargetState: State },
} = hap,
{ location } = this.device,
{ nightModeBypassFor } = this.config
let bypass = false
this.targetingNightMode = state === State.NIGHT_ARM
if (state === State.NIGHT_ARM) {
if (
nightModeBypassFor &&
(nightModeBypassFor === 'all' || nightModeBypassFor === 'some')
) {
state = nightModeBypassFor === 'all' ? State.AWAY_ARM : State.STAY_ARM
bypass = true
} else {
// Switch to Home since we don't know which mode the user wanted
state = State.STAY_ARM
}
}
const bypassContactSensors = bypass
? (await location.getDevices()).filter((device) => {
return (
device.deviceType === RingDeviceType.ContactSensor &&
device.data.faulted
)
})
: [],
bypassSensorZids = bypassContactSensors.map((sensor) => sensor.id),
bypassSensorNames = bypassContactSensors.map((sensor) => sensor.name),
bypassLog = bypassSensorNames.length
? ' - Bypassing Sensors: ' + bypassSensorNames.join(', ')
: ''
try {
if (state === State.AWAY_ARM) {
this.logger.info(`Arming (Away) ${this.device.name}${bypassLog}`)
await location.armAway(bypassSensorZids)
} else if (state === State.DISARM) {
this.logger.info(`Disarming ${this.device.name}`)
await location.disarm()
} else {
this.logger.info(`Arming (Home) ${this.device.name}${bypassLog}`)
await location.armHome(bypassSensorZids)
}
} catch (e) {
this.logger.error(e)
this.getService(hap.Service.SecuritySystem)
.getCharacteristic(hap.Characteristic.SecuritySystemTargetState)
.updateValue(this.getCurrentState(this.device.data))
}
}
}