forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrightness-only.ts
65 lines (56 loc) · 1.92 KB
/
brightness-only.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
import { BaseDeviceAccessory } from './base-device-accessory'
import type { RingDevice } from 'ring-client-api'
import { RingPlatformConfig } from './config'
import { PlatformAccessory } from 'homebridge'
import { hap } from './hap'
import { logInfo } from 'ring-client-api/util'
export class BrightnessOnly extends BaseDeviceAccessory {
constructor(
public readonly device: RingDevice,
public readonly accessory: PlatformAccessory,
public readonly config: RingPlatformConfig,
) {
super()
const { Characteristic, Service } = hap,
{ context } = accessory
if (
device.data.brightness !== undefined &&
!isNaN(device.data.brightness)
) {
this.registerLevelCharacteristic({
characteristicType: Characteristic.Brightness,
serviceType: Service.Lightbulb,
getValue: (data) => {
if (!data.brightness || isNaN(data.brightness)) {
return 0
}
// store the brightness so that we can set it again if light is turned off then back on
context.brightness = data.brightness * 100
return context.brightness
},
setValue: (brightness) => {
if (brightness) {
context.brightness = brightness
}
return this.setLevelState(brightness)
},
})
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Lightbulb,
getValue: (data) => Boolean(data.brightness),
setValue: (on) => {
const brightness = context.brightness || 100
return this.setLevelState(on ? brightness : 0)
},
})
this.getService(Service.Lightbulb).setPrimaryService(true)
}
}
setLevelState(brightness: number) {
logInfo(`Setting brightness of ${this.device.name} to ${brightness}%`)
return this.device.setInfo({
device: { v1: { brightness: brightness / 100 } },
})
}
}