forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-device-accessory.ts
145 lines (128 loc) · 4.18 KB
/
base-device-accessory.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
import { RingDevice, RingDeviceData } from '../api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { BaseDataAccessory } from './base-data-accessory'
import { Logging, PlatformAccessory } from 'homebridge'
import { ServiceType } from './base-accessory'
function getBatteryLevel({ batteryLevel, batteryStatus }: RingDeviceData) {
if (batteryLevel !== undefined) {
return batteryLevel
} else if (batteryStatus === 'full' || batteryStatus === 'charged') {
return 100
} else if (batteryStatus === 'ok' || batteryStatus === 'charging') {
return 50
}
return 0
}
function getStatusLowBattery(data: RingDeviceData) {
const { StatusLowBattery } = hap.Characteristic,
batteryLevel = getBatteryLevel(data)
return batteryLevel > 20
? StatusLowBattery.BATTERY_LEVEL_NORMAL
: StatusLowBattery.BATTERY_LEVEL_LOW
}
function getBatteryChargingState({
batteryStatus,
batteryBackup,
acStatus,
}: RingDeviceData) {
const { ChargingState } = hap.Characteristic
if (
batteryStatus === 'charging' ||
batteryStatus === 'charged' ||
batteryBackup === 'charged' ||
batteryBackup === 'charging' ||
acStatus === 'ok'
) {
return ChargingState.CHARGING
}
if (batteryBackup === 'inUse' || acStatus === 'error') {
return ChargingState.NOT_CHARGING
}
return ChargingState.NOT_CHARGEABLE
}
function hasBatteryStatus({ batteryStatus }: RingDeviceData) {
return batteryStatus !== 'none'
}
export abstract class BaseDeviceAccessory extends BaseDataAccessory<RingDevice> {
abstract readonly device: RingDevice
abstract readonly accessory: PlatformAccessory
abstract readonly logger: Logging
abstract readonly config: RingPlatformConfig
initBase() {
const {
device: { data: initialData },
device,
} = this,
{ 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: (data) => data.deviceType,
})
this.registerCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: Service.AccessoryInformation,
getValue: (data) => data.serialNumber || 'Unknown',
})
if ('volume' in initialData && 'setVolume' in device) {
this.registerCharacteristic({
characteristicType: Characteristic.Mute,
serviceType: Service.Speaker,
getValue: () => false,
})
this.registerLevelCharacteristic({
characteristicType: Characteristic.Volume,
serviceType: Service.Speaker,
getValue: (data) => {
return data.volume ? data.volume * 100 : 0
},
setValue: (volume: number) => {
device.setVolume(volume / 100)
},
})
}
if (hasBatteryStatus(initialData)) {
this.registerCharacteristic({
characteristicType: Characteristic.BatteryLevel,
serviceType: Service.BatteryService,
getValue: getBatteryLevel,
})
this.registerCharacteristic({
characteristicType: Characteristic.StatusLowBattery,
serviceType: Service.BatteryService,
getValue: getStatusLowBattery,
})
this.registerCharacteristic({
characteristicType: Characteristic.ChargingState,
serviceType: Service.BatteryService,
getValue: getBatteryChargingState,
})
}
super.initBase()
}
initSensorService(serviceType: ServiceType) {
const { Characteristic } = hap
this.registerCharacteristic({
characteristicType: Characteristic.StatusTampered,
serviceType,
getValue: (data) => {
return data.tamperStatus === 'ok'
? Characteristic.StatusTampered.NOT_TAMPERED
: Characteristic.StatusTampered.TAMPERED
},
})
if (hasBatteryStatus(this.device.data)) {
this.registerCharacteristic({
characteristicType: Characteristic.StatusLowBattery,
serviceType,
getValue: (data) => getStatusLowBattery(data),
})
}
}
}