generated from homebridge/homebridge-plugin-template
/
myStromLightbulbAccessory.ts
248 lines (220 loc) · 8.2 KB
/
myStromLightbulbAccessory.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { CharacteristicEventTypes } from 'homebridge';
import type {
Service,
PlatformAccessory,
CharacteristicValue,
CharacteristicSetCallback,
CharacteristicGetCallback,
} from 'homebridge';
import simpleColorConverter from 'simple-color-converter';
import qs from 'qs';
import { DingzDaHomebridgePlatform } from './platform';
import { MyStromDeviceInfo, MyStromLightbulbReport } from './lib/myStromTypes';
import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory';
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export class MyStromLightbulbAccessory extends DingzDaBaseAccessory {
private lightbulbService: Service;
// Eventually replaced by:
private mystromDeviceInfo: MyStromDeviceInfo;
private lightbulbState = {
on: false,
color: '0;0;0',
hue: 0,
saturation: 0,
value: 0,
mode: 'hsv',
power: 0,
} as MyStromLightbulbReport;
constructor(
private readonly _platform: DingzDaHomebridgePlatform,
private readonly _accessory: PlatformAccessory,
) {
super(_platform, _accessory);
// Set Base URL
this.mystromDeviceInfo = this.device.hwInfo as MyStromDeviceInfo;
this.platform.log.debug(
'Setting informationService Characteristics ->',
this.device.model,
);
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(
this.platform.Characteristic.Manufacturer,
'MyStrom AG',
)
.setCharacteristic(
this.platform.Characteristic.AppMatchingIdentifier,
'ch.mystrom.iOSApp',
)
.setCharacteristic(
this.platform.Characteristic.Model,
this.device.model as string,
)
.setCharacteristic(
this.platform.Characteristic.FirmwareRevision,
this.mystromDeviceInfo.version ?? 'N/A',
)
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.device.mac,
);
this.lightbulbService =
this.accessory.getService(this.platform.Service.Lightbulb) ??
this.accessory.addService(this.platform.Service.Lightbulb);
this.lightbulbService.setCharacteristic(
this.platform.Characteristic.Name,
this.device.name ?? `${this.accessory.context.device.model} Bulb`,
);
// register handlers for the On/Off Characteristic
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.On)
.on(CharacteristicEventTypes.SET, this.setOn.bind(this)) // SET - bind to the `setOn` method below
.on(CharacteristicEventTypes.GET, this.getOn.bind(this)); // GET - bind to the `getOn` method below
// register handlers for the Brightness Characteristic
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Brightness)
.on(CharacteristicEventTypes.SET, this.setBrightness.bind(this)) // SET - bind to the 'setBrightness` method below
.on(CharacteristicEventTypes.GET, this.getBrightness.bind(this)); // SET - bind to the 'setBrightness` method below
// register handlers for the Brightness Characteristic
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Hue)
.on(CharacteristicEventTypes.SET, this.setHue.bind(this)) // SET - bind to the 'setBrightness` method below
.on(CharacteristicEventTypes.GET, this.getHue.bind(this)); // SET - bind to the 'setBrightness` method below
// register handlers for the Brightness Characteristic
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Saturation)
.on(CharacteristicEventTypes.SET, this.setSaturation.bind(this)) // SET - bind to the 'setBrightness` method below
.on(CharacteristicEventTypes.GET, this.getSaturation.bind(this)); // SET - bind to the 'setBrightness` method below
}
// Get updated device info and update the corresponding values
protected getDeviceStateUpdate() {
this.getDeviceReport(this.device.mac)
.then((report) => {
// push the new value to HomeKit
this.lightbulbState = report;
// FIXME: Add 'mono' as well
if (report.mode === 'hsv') {
const hsv = report.color.split(';');
this.lightbulbState.hue = parseInt(hsv[0]);
this.lightbulbState.saturation = parseInt(hsv[1]);
this.lightbulbState.value = parseInt(hsv[2]);
} else {
// rgbw
const hsv = new simpleColorConverter({
color: `hex #${report.color}`,
to: 'hsv',
});
this.lightbulbState.hue = hsv.c;
this.lightbulbState.saturation = hsv.s;
this.lightbulbState.value = hsv.i;
}
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Hue)
.updateValue(this.lightbulbState.hue);
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Saturation)
.updateValue(this.lightbulbState.saturation);
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.Brightness)
.updateValue(this.lightbulbState.value);
this.lightbulbService
.getCharacteristic(this.platform.Characteristic.On)
.updateValue(this.lightbulbState.on);
})
.catch((e) => {
this.platform.log.debug('Error while retrieving Device Report ->', e);
});
}
identify(): void {
this.platform.log.debug(
'Identify! -> Who am I? I am',
this.accessory.displayName,
);
}
private setOn(
value: CharacteristicValue,
callback: CharacteristicSetCallback,
) {
this.lightbulbState.on = value as boolean;
this.setDeviceLightbulb(callback);
}
private getOn(callback: CharacteristicGetCallback) {
const isOn = this.lightbulbState.on;
this.platform.log.debug('Get Characteristic On ->', isOn);
callback(this.isReachable ? null : new Error(), isOn);
}
private async setHue(
value: CharacteristicValue,
callback: CharacteristicSetCallback,
) {
this.lightbulbState.hue = value as number;
this.setDeviceLightbulb(callback);
}
private getHue(callback: CharacteristicGetCallback) {
const hue: number = this.lightbulbState.hue;
callback(this.isReachable ? null : new Error(), hue);
}
private async setSaturation(
value: CharacteristicValue,
callback: CharacteristicSetCallback,
) {
this.lightbulbState.saturation = value as number;
this.setDeviceLightbulb(callback);
}
private getSaturation(callback: CharacteristicGetCallback) {
const saturation: number = this.lightbulbState.saturation;
callback(this.isReachable ? null : new Error(), saturation);
}
private async setBrightness(
value: CharacteristicValue,
callback: CharacteristicSetCallback,
) {
this.lightbulbState.value = value as number;
this.setDeviceLightbulb(callback);
}
private getBrightness(callback: CharacteristicGetCallback) {
const brightness = this.lightbulbState.value;
callback(this.isReachable ? null : new Error(), brightness);
}
// Set individual dimmer
private setDeviceLightbulb(callback: CharacteristicSetCallback) {
const state: MyStromLightbulbReport = this.lightbulbState;
const color = `${state.hue};${state.saturation};${state.value}`;
const setDimmerUrl = `${this.baseUrl}/api/v1/device/${this.device.mac}`;
const data = qs.stringify(
{
action: state.on ? 'on' : 'off',
color: color ?? undefined,
mode: color ? 'hsv' : undefined,
},
{ encode: false },
);
return this.request
.post(setDimmerUrl, data)
.catch(this.handleRequestErrors.bind(this))
.finally(() => {
// make sure we callback
if (!this.isReachable) {
callback(new Error());
} else {
callback(null);
}
});
}
private async getDeviceReport(mac: string): Promise<MyStromLightbulbReport> {
const reportUrl = `${this.baseUrl}/api/v1/device/`;
return await DingzDaHomebridgePlatform.fetch({
url: reportUrl,
returnBody: true,
token: this.device.token,
})
.then((response) => {
return response[mac];
})
.catch(this.handleRequestErrors.bind(this));
}
}