-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
beam.ts
84 lines (71 loc) · 2.55 KB
/
beam.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
import { RingDevice, RingDeviceType } from 'ring-client-api'
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { BaseDeviceAccessory } from './base-device-accessory'
import { PlatformAccessory } from 'homebridge'
import { logInfo } from 'ring-client-api/util'
export class Beam extends BaseDeviceAccessory {
isLightGroup =
this.device.data.deviceType === RingDeviceType.BeamsLightGroupSwitch
groupId = this.device.data.groupId
constructor(
public readonly device: RingDevice,
public readonly accessory: PlatformAccessory,
public readonly config: RingPlatformConfig,
) {
super()
const { Characteristic, Service } = hap,
{ MotionSensor } = Service,
{
data: { deviceType },
} = this.device
if (deviceType !== RingDeviceType.BeamsMotionSensor) {
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Lightbulb,
getValue: (data) => Boolean(data.on),
setValue: (value) => this.setOnState(value),
})
this.getService(Service.Lightbulb).setPrimaryService(true)
}
if (deviceType === RingDeviceType.BeamsMultiLevelSwitch) {
this.registerLevelCharacteristic({
characteristicType: Characteristic.Brightness,
serviceType: Service.Lightbulb,
getValue: (data) => {
return data.level && !isNaN(data.level) ? 100 * data.level : 0
},
setValue: (value) => this.setLevelState(value),
})
}
if (
device.data.motionStatus !== undefined &&
device.data.motionSensorEnabled !== false
) {
this.registerCharacteristic({
characteristicType: hap.Characteristic.MotionDetected,
serviceType: MotionSensor,
getValue: (data) => data.motionStatus === 'faulted',
})
this.initSensorService(MotionSensor)
}
}
setOnState(on: boolean) {
logInfo(`Turning ${this.device.name} ${on ? 'On' : 'Off'}`)
const { beamDurationSeconds } = this.config,
duration = beamDurationSeconds
? Math.min(beamDurationSeconds, 32767)
: undefined
if (this.isLightGroup && this.groupId) {
return this.device.location.setLightGroup(this.groupId, on, duration)
}
const data = on ? { lightMode: 'on', duration } : { lightMode: 'default' }
return this.device.sendCommand('light-mode.set', data)
}
setLevelState(level: number) {
logInfo(`Setting brightness of ${this.device.name} to ${level}%`)
return this.device.setInfo({
device: { v1: { level: level / 100 } },
})
}
}