generated from homebridge/homebridge-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
cover.ts
339 lines (296 loc) 路 12.8 KB
/
cover.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { BasicAccessory, ServiceCreator, ServiceHandler } from './interfaces';
import {
exposesCanBeGet,
exposesCanBeSet,
ExposesEntry,
ExposesEntryWithEnumProperty,
ExposesEntryWithFeatures,
ExposesEntryWithNumericRangeProperty,
exposesHasEnumProperty,
exposesHasFeatures,
exposesHasNumericRangeProperty,
exposesIsPublished,
ExposesKnownTypes,
} from '../z2mModels';
import { hap } from '../hap';
import { getOrAddCharacteristic } from '../helpers';
import { Characteristic, CharacteristicSetCallback, CharacteristicValue, Service } from 'homebridge';
import { ExtendedTimer } from '../timer';
import { CharacteristicMonitor, NumericCharacteristicMonitor } from './monitor';
export class CoverCreator implements ServiceCreator {
createServicesFromExposes(accessory: BasicAccessory, exposes: ExposesEntry[]): void {
exposes
.filter(
(e) =>
e.type === ExposesKnownTypes.COVER &&
exposesHasFeatures(e) &&
!accessory.isServiceHandlerIdKnown(CoverHandler.generateIdentifier(e.endpoint))
)
.forEach((e) => this.createService(e as ExposesEntryWithFeatures, accessory));
}
private createService(expose: ExposesEntryWithFeatures, accessory: BasicAccessory): void {
try {
const handler = new CoverHandler(expose, accessory);
accessory.registerServiceHandler(handler);
} catch (error) {
accessory.log.warn(`Failed to setup cover for accessory ${accessory.displayName} from expose "${JSON.stringify(expose)}": ${error}`);
}
}
}
class CoverHandler implements ServiceHandler {
private static readonly STATE_HOLD_POSITION = 'STOP';
private readonly positionExpose: ExposesEntryWithNumericRangeProperty;
private readonly tiltExpose: ExposesEntryWithNumericRangeProperty | undefined;
private readonly stateExpose: ExposesEntryWithEnumProperty | undefined;
private readonly service: Service;
private readonly updateTimer: ExtendedTimer | undefined;
private readonly target_min: number;
private readonly target_max: number;
private readonly current_min: number;
private readonly current_max: number;
private readonly target_tilt_min: number;
private readonly target_tilt_max: number;
private monitors: CharacteristicMonitor[] = [];
private waitingForUpdate: boolean;
private ignoreNextUpdateIfEqualToTarget: boolean;
private lastPositionSet = -1;
private positionCurrent = -1;
public readonly mainCharacteristics: Characteristic[] = [];
constructor(
expose: ExposesEntryWithFeatures,
private readonly accessory: BasicAccessory
) {
const endpoint = expose.endpoint;
this.identifier = CoverHandler.generateIdentifier(endpoint);
let positionExpose = expose.features.find(
(e) => exposesHasNumericRangeProperty(e) && e.name === 'position' && exposesCanBeSet(e) && exposesIsPublished(e)
) as ExposesEntryWithNumericRangeProperty;
this.tiltExpose = expose.features.find(
(e) => exposesHasNumericRangeProperty(e) && e.name === 'tilt' && exposesCanBeSet(e) && exposesIsPublished(e)
) as ExposesEntryWithNumericRangeProperty | undefined;
this.stateExpose = expose.features.find(
(e) =>
e.type === ExposesKnownTypes.ENUM &&
e.name === 'state' &&
exposesCanBeSet(e) &&
exposesHasEnumProperty(e) &&
e.values.includes(CoverHandler.STATE_HOLD_POSITION)
) as ExposesEntryWithEnumProperty;
if (positionExpose === undefined) {
if (this.tiltExpose !== undefined) {
// Tilt only device
positionExpose = this.tiltExpose;
this.tiltExpose = undefined;
} else {
throw new Error('Required "position" property not found for WindowCovering and no "tilt" as backup.');
}
}
this.positionExpose = positionExpose;
const serviceName = accessory.getDefaultServiceDisplayName(endpoint);
accessory.log.debug(`Configuring WindowCovering for ${serviceName}`);
this.service = accessory.getOrAddService(new hap.Service.WindowCovering(serviceName, endpoint));
const current = getOrAddCharacteristic(this.service, hap.Characteristic.CurrentPosition);
this.mainCharacteristics.push(current);
if (current.props.minValue === undefined || current.props.maxValue === undefined) {
throw new Error('CurrentPosition for Cover does not hav a rang (minValue, maxValue) defined.');
}
this.current_min = current.props.minValue;
this.current_max = current.props.maxValue;
this.mainCharacteristics.push(getOrAddCharacteristic(this.service, hap.Characteristic.PositionState));
const target = getOrAddCharacteristic(this.service, hap.Characteristic.TargetPosition);
if (target.props.minValue === undefined || target.props.maxValue === undefined) {
throw new Error('TargetPosition for Cover does not have a rang (minValue, maxValue) defined.');
}
this.target_min = target.props.minValue;
this.target_max = target.props.maxValue;
target.on('set', this.handleSetTargetPosition.bind(this));
if (this.target_min !== this.current_min || this.target_max !== this.current_max) {
this.accessory.log.error(accessory.displayName + ': cover: TargetPosition and CurrentPosition do not have the same range!');
}
// Tilt
if (this.tiltExpose !== undefined) {
getOrAddCharacteristic(this.service, hap.Characteristic.CurrentHorizontalTiltAngle);
this.monitors.push(
new NumericCharacteristicMonitor(
this.tiltExpose.property,
this.service,
hap.Characteristic.CurrentHorizontalTiltAngle,
this.tiltExpose.value_min,
this.tiltExpose.value_max
)
);
const target_tilt = getOrAddCharacteristic(this.service, hap.Characteristic.TargetHorizontalTiltAngle);
if (target_tilt.props.minValue === undefined || target_tilt.props.maxValue === undefined) {
throw new Error('TargetHorizontalTiltAngle for Cover does not have a rang (minValue, maxValue) defined.');
}
this.target_tilt_min = target_tilt.props.minValue;
this.target_tilt_max = target_tilt.props.maxValue;
target_tilt.on('set', this.handleSetTargetHorizontalTilt.bind(this));
} else {
this.target_tilt_min = -90;
this.target_tilt_max = 90;
}
// Hold Position
if (this.stateExpose !== undefined) {
getOrAddCharacteristic(this.service, hap.Characteristic.HoldPosition).on('set', this.handleSetHoldPosition.bind(this));
}
if (exposesCanBeGet(this.positionExpose)) {
this.updateTimer = new ExtendedTimer(this.requestPositionUpdate.bind(this), 4000);
}
this.waitingForUpdate = false;
this.ignoreNextUpdateIfEqualToTarget = false;
}
identifier: string;
get getableKeys(): string[] {
const keys: string[] = [];
if (exposesCanBeGet(this.positionExpose)) {
keys.push(this.positionExpose.property);
}
if (this.tiltExpose !== undefined && exposesCanBeGet(this.tiltExpose)) {
keys.push(this.tiltExpose.property);
}
return keys;
}
updateState(state: Record<string, unknown>): void {
this.monitors.forEach((m) => m.callback(state));
if (this.positionExpose.property in state) {
const latestPosition = state[this.positionExpose.property] as number;
// Ignore "first" update?
const doIgnoreIfEqual = this.ignoreNextUpdateIfEqualToTarget;
this.ignoreNextUpdateIfEqualToTarget = false;
if (latestPosition === this.lastPositionSet && doIgnoreIfEqual) {
this.accessory.log.debug(`${this.accessory.displayName}: cover: ignore position update (equal to last target)`);
return;
}
// Received an update: Reset flag
this.waitingForUpdate = false;
// If we cannot retrieve the position or we were not expecting an update,
// always assume the state is "stopped".
let didStop = true;
// As long as the update timer is running, we are expecting updates.
if (this.updateTimer !== undefined && this.updateTimer.isActive) {
if (latestPosition === this.positionCurrent) {
// Stop requesting frequent updates if no change is detected.
this.updateTimer.stop();
} else {
// Assume cover is still moving as the position is still changing
didStop = false;
this.startOrRestartUpdateTimer();
}
}
// Update current position
this.positionCurrent = latestPosition;
this.scaleAndUpdateCurrentPosition(this.positionCurrent, didStop);
}
}
private startOrRestartUpdateTimer(): void {
if (this.updateTimer === undefined) {
return;
}
this.waitingForUpdate = true;
if (this.updateTimer.isActive) {
this.updateTimer.restart();
} else {
this.updateTimer.start();
}
}
private requestPositionUpdate() {
if (!exposesCanBeGet(this.positionExpose)) {
return;
}
if (this.waitingForUpdate) {
// Manually polling for the state, as we have not yet received an update.
this.accessory.queueKeyForGetAction(this.positionExpose.property);
}
}
private scaleNumber(value: number, input_min: number, input_max: number, output_min: number, output_max: number): number {
if (value <= input_min) {
return output_min;
}
if (value >= input_max) {
return output_max;
}
const percentage = (value - input_min) / (input_max - input_min);
return output_min + percentage * (output_max - output_min);
}
private scaleAndUpdateCurrentPosition(value: number, isStopped: boolean): void {
const characteristicValue = this.scaleNumber(
value,
this.positionExpose.value_min,
this.positionExpose.value_max,
this.current_min,
this.current_max
);
this.service.updateCharacteristic(hap.Characteristic.CurrentPosition, characteristicValue);
if (isStopped) {
// Update target position and position state
// This should improve the UX in the Home.app
this.accessory.log.debug(`${this.accessory.displayName}: cover: assume movement stopped`);
this.service.updateCharacteristic(hap.Characteristic.PositionState, hap.Characteristic.PositionState.STOPPED);
this.service.updateCharacteristic(hap.Characteristic.TargetPosition, characteristicValue);
}
}
private handleSetTargetPosition(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
const target = this.scaleNumber(
value as number,
this.target_min,
this.target_max,
this.positionExpose.value_min,
this.positionExpose.value_max
);
const data = {};
data[this.positionExpose.property] = target;
this.accessory.queueDataForSetAction(data);
// Assume position state based on new target
if (target > this.positionCurrent) {
this.service.updateCharacteristic(hap.Characteristic.PositionState, hap.Characteristic.PositionState.INCREASING);
} else if (target < this.positionCurrent) {
this.service.updateCharacteristic(hap.Characteristic.PositionState, hap.Characteristic.PositionState.DECREASING);
} else {
this.service.updateCharacteristic(hap.Characteristic.PositionState, hap.Characteristic.PositionState.STOPPED);
}
// Store last sent position for future reference
this.lastPositionSet = target;
// Ignore next status update if it is equal to the target position set here
// and the position can be get.
// This was needed for my Swedish blinds when reporting was enabled.
// (First update would contain the target position that was sent, followed by the actual position.)
if (exposesCanBeGet(this.positionExpose)) {
this.ignoreNextUpdateIfEqualToTarget = true;
}
// Start requesting frequent updates (if we do not receive them automatically)
this.startOrRestartUpdateTimer();
callback(null);
}
private handleSetTargetHorizontalTilt(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
if (this.tiltExpose) {
// map value: angle back to target: percentage
// must be rounded for set action
const targetTilt = Math.round(
this.scaleNumber(value as number, this.target_tilt_min, this.target_tilt_max, this.tiltExpose.value_min, this.tiltExpose.value_max)
);
const data = {};
data[this.tiltExpose.property] = targetTilt;
this.accessory.queueDataForSetAction(data);
callback(null);
} else {
callback(new Error('tilt not supported'));
}
}
private handleSetHoldPosition(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
const doHold = value as boolean;
if (doHold && this.stateExpose) {
const data = {};
data[this.stateExpose.property] = CoverHandler.STATE_HOLD_POSITION;
this.accessory.queueDataForSetAction(data);
}
callback(null);
}
static generateIdentifier(endpoint: string | undefined) {
let identifier = hap.Service.WindowCovering.UUID;
if (endpoint !== undefined) {
identifier += '_' + endpoint.trim();
}
return identifier;
}
}