forked from homebridge-plugins/homebridge-lutron-caseta-leap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunnataKeypad.ts
137 lines (117 loc) · 6.21 KB
/
sunnataKeypad.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
import { Service, PlatformAccessory } from 'homebridge';
import { GlobalOptions, LutronRadioRA3Platform, DeviceWireResult, DeviceWireResultType } from './platform';
import { ButtonTracker } from './buttonTracker';
import { ExceptionDetail, OneButtonStatusEvent, Response, Processor } from './leap';
export class SunnataKeypad {
private services: Map<string, Service> = new Map();
private trackers: Map<string, ButtonTracker> = new Map();
constructor(
private readonly platform: LutronRadioRA3Platform,
private readonly accessory: PlatformAccessory,
private readonly processor: Processor,
private readonly options: GlobalOptions,
) {
}
async initialize(): Promise<DeviceWireResult> {
this.accessory
.getService(this.platform.api.hap.Service.AccessoryInformation)!
.setCharacteristic(this.platform.api.hap.Characteristic.Manufacturer, 'Lutron Electronics Co., Inc')
.setCharacteristic(this.platform.api.hap.Characteristic.Model, this.accessory.context.device.ModelNumber)
.setCharacteristic(
this.platform.api.hap.Characteristic.SerialNumber,
this.accessory.context.device.SerialNumber.toString(),
);
const labelService =
this.accessory.getService(this.platform.api.hap.Service.ServiceLabel) ||
this.accessory.addService(this.platform.api.hap.Service.ServiceLabel);
labelService.setCharacteristic(
this.platform.api.hap.Characteristic.ServiceLabelNamespace,
this.platform.api.hap.Characteristic.ServiceLabelNamespace.ARABIC_NUMERALS,
);
let buttonGroups;
try {
buttonGroups = await this.processor.getDeviceButtonGroupsExpanded(this.accessory.context.device);
} catch (e) {
this.platform.log.error('Failed to get button groups belonging to', this.accessory.displayName, e);
return {
kind: DeviceWireResultType.Error,
reason: `Failed to get button groups belonging to ${this.accessory.displayName}: ${e}`,
};
}
buttonGroups.forEach((buttonGroup) => {
if (buttonGroup instanceof ExceptionDetail) {
return new Error('Device has been removed');
}
});
for (const buttonGroup of buttonGroups) {
for (const button of buttonGroup.Buttons) {
const label = button.Engraving.Text || button.Name;
this.platform.log.info(`setting up ${button.href} named ${label} numbered ${button.ButtonNumber}`);
const service =
this.accessory.getServiceById(this.platform.api.hap.Service.StatelessProgrammableSwitch, button.Name) ||
this.accessory.addService(
this.platform.api.hap.Service.StatelessProgrammableSwitch,
label,
button.Name,
);
service.addLinkedService(labelService);
service.setCharacteristic(this.platform.api.hap.Characteristic.Name, label);
service.setCharacteristic(this.platform.api.hap.Characteristic.ServiceLabelIndex, button.ButtonNumber);
service
.getCharacteristic(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent)
.setProps({ maxValue: 2 });
this.services.set(button.href, service);
this.trackers.set(
button.href,
new ButtonTracker(
() =>
service
.getCharacteristic(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent)
.updateValue(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS),
() =>
service
.getCharacteristic(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent)
.updateValue(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS),
() =>
service
.getCharacteristic(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent)
.updateValue(this.platform.api.hap.Characteristic.ProgrammableSwitchEvent.LONG_PRESS),
this.platform.log,
button.href,
this.options.clickSpeedDouble,
this.options.clickSpeedLong,
false,
),
);
this.platform.log.debug(`subscribing to ${button.href} events`);
this.processor.subscribeToButton(button, this.handleEvent.bind(this));
// when the connection is lost, so are subscriptions.
this.processor.on('disconnected', () => {
this.platform.log.debug(`re-subscribing to ${button.href} events after connection loss`);
this.processor.subscribeToButton(button, this.handleEvent.bind(this));
});
}
}
this.platform.on('unsolicited', this.handleUnsolicited.bind(this));
return {
kind: DeviceWireResultType.Success,
name: this.accessory.displayName,
};
}
handleEvent(response: Response): void {
const evt = (response.Body! as OneButtonStatusEvent).ButtonStatus;
this.platform.log.info(
`Button ${evt.Button.href} on keypad ${this.accessory.displayName} got action ${evt.ButtonEvent.EventType}`,
);
this.trackers.get(evt.Button.href)!.update(evt.ButtonEvent.EventType);
}
handleUnsolicited(response: Response): void {
if (response.Header.MessageBodyType === 'OneButtonStatusEvent') {
const href = (response.Body as OneButtonStatusEvent)?.ButtonStatus.Button.href;
if (this.services.has(href)) {
this.platform.log.warn('got unsolicited response for known button ', href, ', handling anyway');
this.handleEvent(response);
}
}
}
}