forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location-mode-switch.ts
167 lines (146 loc) · 4.67 KB
/
location-mode-switch.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 { Location, LocationMode } from '../api'
import { distinctUntilChanged, take } from 'rxjs/operators'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { logError } from '../api/util'
import { BaseAccessory } from './base-accessory'
import { lastValueFrom, of } from 'rxjs'
import {
Logging,
PlatformAccessory,
CharacteristicEventTypes,
CharacteristicGetCallback,
CharacteristicSetCallback,
CharacteristicValue,
} from 'homebridge'
function getStateFromMode(mode: LocationMode) {
const {
Characteristic: { SecuritySystemCurrentState: State },
} = hap
switch (mode) {
case 'away':
return State.AWAY_ARM
case 'home':
return State.STAY_ARM
case 'disarmed':
return State.DISARMED
default:
return State.DISARMED
}
}
export class LocationModeSwitch extends BaseAccessory<Location> {
private targetState: any
public device = this.location // for use in BaseAccessory
constructor(
private readonly location: Location,
public readonly accessory: PlatformAccessory,
public readonly logger: Logging,
public readonly config: RingPlatformConfig
) {
super()
const {
Characteristic,
Service: { SecuritySystem, AccessoryInformation },
} = hap,
accessoryName = location.name + ' Mode',
service = this.getService(SecuritySystem, accessoryName),
currentState = service.getCharacteristic(
Characteristic.SecuritySystemCurrentState
),
targetState = service.getCharacteristic(
Characteristic.SecuritySystemTargetState
),
getCurrentMode = () => {
return lastValueFrom(location.onLocationMode.pipe(take(1)))
},
getCurrentState = async () => getStateFromMode(await getCurrentMode())
location.onLocationMode.pipe(distinctUntilChanged()).subscribe((mode) => {
const state = getStateFromMode(mode)
if (state === this.targetState) {
this.targetState = undefined
}
if (!this.targetState) {
targetState.updateValue(state)
}
currentState.updateValue(state)
})
currentState.on(
CharacteristicEventTypes.GET,
async (callback: CharacteristicGetCallback) => {
location.getLocationMode().catch((e) => {
logError('Failed to retrieve location mode for ' + location.name)
logError(e)
})
const state = await getCurrentState()
if (state === this.targetState) {
this.targetState = undefined
}
callback(null, state)
}
)
targetState.on(
CharacteristicEventTypes.GET,
async (callback: CharacteristicGetCallback) => {
callback(
null,
this.targetState !== undefined
? this.targetState
: await getCurrentState()
)
}
)
targetState.on(
CharacteristicEventTypes.SET,
async (
state: CharacteristicValue,
callback: CharacteristicSetCallback
) => {
const {
Characteristic: { SecuritySystemTargetState: State },
} = hap
callback()
if (state === State.NIGHT_ARM) {
state = State.STAY_ARM
// Ring doesn't have night mode, so switch over to stay mode
setTimeout(() => targetState.updateValue(state), 100)
}
if (state === (await getCurrentState())) {
this.targetState = undefined
return
}
this.targetState = state
if (state === State.AWAY_ARM) {
this.logger.info(`Setting ${this.location.name} Mode to away`)
return this.location.setLocationMode('away')
} else if (state === State.DISARM) {
this.logger.info(`Setting ${this.location.name} Mode to disarmed`)
return this.location.setLocationMode('disarmed')
}
this.logger.info(`Setting ${this.location.name} Mode to home`)
return this.location.setLocationMode('home')
}
)
targetState.setProps({
validValues: [
Characteristic.SecuritySystemTargetState.AWAY_ARM,
Characteristic.SecuritySystemTargetState.STAY_ARM,
Characteristic.SecuritySystemTargetState.DISARM,
],
})
this.registerObservableCharacteristic({
characteristicType: Characteristic.Manufacturer,
serviceType: AccessoryInformation,
onValue: of('Ring'),
})
this.registerObservableCharacteristic({
characteristicType: Characteristic.Model,
serviceType: AccessoryInformation,
onValue: of('Location Mode'),
})
this.registerObservableCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: AccessoryInformation,
onValue: of('N/A'),
})
}
}