generated from homebridge/homebridge-plugin-template
/
myStromPIRAccessory.ts
272 lines (242 loc) · 9.31 KB
/
myStromPIRAccessory.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { CharacteristicEventTypes } from 'homebridge';
import type {
Service,
PlatformAccessory,
CharacteristicGetCallback,
} from 'homebridge';
import { Policy } from 'cockatiel';
import { Mutex } from 'async-mutex';
import { DingzDaHomebridgePlatform } from './platform';
import { MyStromDeviceInfo, MyStromPIRReport } from './lib/myStromTypes';
import { AccessoryActionUrl, ButtonAction } from './lib/commonTypes';
import { DeviceNotReachableError } from './lib/errors';
import { PlatformEvent } from './lib/platformEventBus';
import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory';
// Policy for long running tasks, retry every hour
const retrySlow = Policy.handleAll()
.orWhenResult((retry) => retry === true)
.retry()
.exponential({ initialDelay: 10000, maxDelay: 60 * 60 * 1000 });
/**
* 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 MyStromPIRAccessory extends DingzDaBaseAccessory {
private readonly mutex = new Mutex();
private motionService: Service;
private temperatureService: Service;
private lightService: Service;
// Eventually replaced by:
private mystromDeviceInfo: MyStromDeviceInfo;
private pirState = {
motion: false,
temperature: 0,
light: 0,
} as MyStromPIRReport;
constructor(
private readonly _platform: DingzDaHomebridgePlatform,
private readonly _accessory: PlatformAccessory,
) {
super(_platform, _accessory);
// Set Base URL
this.mystromDeviceInfo = this.device.hwInfo as MyStromDeviceInfo;
this.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.HardwareRevision,
'PQWBB1',
)
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.device.mac,
);
// get the LightBulb service if it exists, otherwise create a new LightBulb service
// you can create multiple services for each accessory
this.log.info('Create Motion Sensor');
this.temperatureService =
this.accessory.getService(this.platform.Service.TemperatureSensor) ??
this.accessory.addService(this.platform.Service.TemperatureSensor);
this.temperatureService.setCharacteristic(
this.platform.Characteristic.Name,
`Temperature ${this.device.name}`,
);
// create handlers for required characteristics
this.temperatureService
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.on(CharacteristicEventTypes.GET, this.getTemperature.bind(this));
// Add the LightSensor that's integrated in the dingz
// API: /api/v1/light
this.lightService =
this.accessory.getService(this.platform.Service.LightSensor) ??
this.accessory.addService(this.platform.Service.LightSensor);
this.lightService.setCharacteristic(
this.platform.Characteristic.Name,
`Light ${this.device.name}`,
);
// create handlers for required characteristics
this.lightService
.getCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel)
.on(CharacteristicEventTypes.GET, this.getLightLevel.bind(this));
this.motionService =
this.accessory.getService(this.platform.Service.MotionSensor) ??
this.accessory.addService(this.platform.Service.MotionSensor);
this.motionService.setCharacteristic(
this.platform.Characteristic.Name,
`Motion ${this.device.name}`,
);
if (!(this.platform.config.motionPoller ?? true)) {
// Implement *push* event handling
this.platform.eb.on(PlatformEvent.ACTION, (mac, action) => {
this.log.debug(`Processing DingzEvent.ACTION ${action}`);
if (mac === this.device.mac) {
this.log.debug(`Motion detected -> ${action}`);
let isMotion: boolean | undefined;
switch (action) {
case ButtonAction.PIR_MOTION_STOP:
isMotion = false;
break;
case ButtonAction.PIR_MOTION_START:
default:
isMotion = true;
break;
}
this.log.debug('Motion Update from PUSH');
this.pirState.motion = isMotion;
this.motionService.setCharacteristic(
this.platform.Characteristic.MotionDetected,
this.pirState.motion,
);
}
});
}
// Set the callback URL (Override!)
retrySlow.execute(() => {
this.getButtonCallbackUrl()
.then((callBackUrl) => {
if (!callBackUrl?.url.includes(this.platform.getCallbackUrl())) {
this.log.warn('Update existing callback URL ->', callBackUrl);
// Set the callback URL (Override!)
this.platform.setButtonCallbackUrl({
baseUrl: this.baseUrl,
token: this.device.token,
oldUrl: callBackUrl.url,
endpoints: ['pir/generic'],
});
} else {
this.log.debug('Callback URL already set ->', callBackUrl?.url);
}
})
.catch(this.handleRequestErrors.bind(this));
});
}
/**
* Returns the callback URL for the device
*/
public async getButtonCallbackUrl(): Promise<AccessoryActionUrl> {
const getCallbackEndpoint = '/api/v1/action/pir/generic';
this.log.debug('Getting the callback URL -> ', getCallbackEndpoint);
return await this.request.get(getCallbackEndpoint).then((response) => {
return response.data;
});
}
// Get updated device info and update the corresponding values
protected getDeviceStateUpdate(): Promise<void> {
return this.getDeviceReport()
.then((report) => {
if (report) {
// If we are in motion polling mode, update motion from poller
// TODO: remove this -- doesn't make sense at all
if (this.platform.config.motionPoller ?? true) {
this.log.info('Motion POLLING of', this.device.name, 'enabled');
const isMotion: boolean = report.motion;
// Only update if motionService exists *and* if there's a change in motion'
if (this.pirState.motion !== isMotion) {
this.log.debug('Motion Update from POLLER');
this.pirState.motion = isMotion;
this.motionService
.getCharacteristic(this.platform.Characteristic.MotionDetected)
.updateValue(this.pirState.motion);
}
}
// Update temperature and light in any case
this.pirState.temperature = report.temperature;
this.temperatureService
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.updateValue(this.pirState.temperature);
this.pirState.light = report.light ?? 0;
this.lightService
.getCharacteristic(
this.platform.Characteristic.CurrentAmbientLightLevel,
)
.updateValue(this.pirState.light);
return Promise.resolve();
} else {
return Promise.reject(new DeviceNotReachableError());
}
})
.catch((e: Error) => {
return Promise.reject(e);
});
}
/**
* Handle Handle the "GET" requests from HomeKit
* to get the current value of the "Ambient Light Level" characteristic
*/
private getLightLevel(callback: CharacteristicGetCallback) {
const light: number = this.pirState?.light ?? 42;
this.log.debug('Get Characteristic Ambient Light Level ->', light, ' lux');
callback(this.reachabilityState, light);
}
/**
* Handle Handle the "GET" requests from HomeKit
* to get the current value of the "Temperature" characteristic
*/
private getTemperature(callback: CharacteristicGetCallback) {
const temperature: number = this.pirState?.temperature;
this.log.debug('Get Characteristic Temperature ->', temperature, '° C');
callback(this.reachabilityState, temperature);
}
/**
* Handle Handle the "GET" requests from HomeKit
* to get the current value of the "Motion Detected" characteristic
*/
private getMotionDetected(callback: CharacteristicGetCallback) {
// set this to a valid value for MotionDetected
const isMotion = this.pirState.motion;
callback(this.reachabilityState, isMotion);
}
private async getDeviceReport(): Promise<MyStromPIRReport> {
const getSensorsUrl = `${this.baseUrl}/api/v1/sensors`;
return await this.request.get(getSensorsUrl).then((response) => {
return response.data;
});
}
/*
* This method is optional to implement. It is called when HomeKit ask to identify the accessory.
* Typical this only ever happens at the pairing process.
*/
identify(): void {
this.log.debug('Identify! -> Who am I? I am', this.accessory.displayName);
}
}