forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-panel.ts
More file actions
177 lines (153 loc) · 5.27 KB
/
security-panel.ts
File metadata and controls
177 lines (153 loc) · 5.27 KB
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
168
169
170
171
172
173
174
175
176
177
import { BaseDeviceAccessory } from './base-device-accessory'
import {
AlarmMode,
AlarmState,
allAlarmStates,
RingDevice,
RingDeviceData,
RingDeviceType,
} from 'ring-client-api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { PlatformAccessory } from 'homebridge'
import { logError, logInfo } from 'ring-client-api/util'
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 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.getTargetState(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
}
getTargetState({ mode }: RingDeviceData) {
const {
Characteristic: { SecuritySystemTargetState: State },
} = hap
if (mode === this.getTargetNightMode()) {
return State.NIGHT_ARM
}
// current mode does not match night mode target, so we are no longer targeting night mode
this.targetingNightMode = false
switch (mode) {
case 'all':
return State.AWAY_ARM
case 'some':
return State.STAY_ARM
case 'none':
return State.DISARM
default:
return State.DISARM
}
}
getCurrentState(data: RingDeviceData) {
const { alarmInfo } = data
if (alarmInfo && this.alarmStates.includes(alarmInfo.state)) {
return hap.Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED
}
return this.getTargetState(data)
}
get targetingNightMode() {
return this.accessory.context.targetingNightMode
}
set targetingNightMode(value: boolean) {
this.accessory.context.targetingNightMode = value
}
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) {
logInfo(`Arming (Away) ${this.device.name}${bypassLog}`)
await location.armAway(bypassSensorZids)
} else if (state === State.DISARM) {
logInfo(`Disarming ${this.device.name}`)
await location.disarm()
} else {
logInfo(`Arming (Home) ${this.device.name}${bypassLog}`)
await location.armHome(bypassSensorZids)
}
} catch (e: any) {
logError(e)
this.getService(hap.Service.SecuritySystem)
.getCharacteristic(hap.Characteristic.SecuritySystemTargetState)
.updateValue(this.getTargetState(this.device.data))
}
}
}