@homebridge-plugins/homebridge-tuya 2.7.2-beta.1 → 2.7.2-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADVANCED_OPTIONS.md +73 -0
- package/CHANGELOG.md +9 -0
- package/SUPPORTED_DEVICES.md +1 -1
- package/config.schema.json +81 -0
- package/dist/accessory/AccessoryFactory.js +5 -1
- package/dist/accessory/AccessoryFactory.js.map +1 -1
- package/dist/accessory/AirConditionerAccessory.js +42 -0
- package/dist/accessory/AirConditionerAccessory.js.map +1 -1
- package/dist/accessory/BaseAccessory.js +21 -4
- package/dist/accessory/BaseAccessory.js.map +1 -1
- package/dist/accessory/ET72GWAccessory.js +182 -0
- package/dist/accessory/ET72GWAccessory.js.map +1 -0
- package/dist/accessory/GarageDoorAccessory.js +107 -4
- package/dist/accessory/GarageDoorAccessory.js.map +1 -1
- package/dist/accessory/LockAccessory.js +133 -2
- package/dist/accessory/LockAccessory.js.map +1 -1
- package/dist/accessory/WindowCoveringAccessory.js +14 -2
- package/dist/accessory/WindowCoveringAccessory.js.map +1 -1
- package/dist/accessory/characteristic/EnergyUsage.js +1 -1
- package/dist/config.js.map +1 -1
- package/dist/core/TuyaOpenAPI.js +129 -1
- package/dist/core/TuyaOpenAPI.js.map +1 -1
- package/dist/device/ET72GWMeasurements.js +305 -0
- package/dist/device/ET72GWMeasurements.js.map +1 -0
- package/dist/device/TuyaCustomDeviceManager.js +1 -1
- package/dist/device/TuyaCustomDeviceManager.js.map +1 -1
- package/dist/device/TuyaDeviceManager.js +4 -1
- package/dist/device/TuyaDeviceManager.js.map +1 -1
- package/dist/device/TuyaHomeDeviceManager.js +1 -1
- package/dist/device/TuyaHomeDeviceManager.js.map +1 -1
- package/dist/platform.js +6 -0
- package/dist/platform.js.map +1 -1
- package/package.json +6 -1
- package/test/accessory/airConditionerAccessory.test.ts +99 -0
- package/test/accessory/et72gwAccessory.test.ts +489 -0
- package/test/accessory/lockAccessory.test.ts +94 -0
- package/test/accessory/timedState.test.ts +372 -0
- package/test/et72gw-floor-temperature.test.ts +248 -0
- package/test/et72gw-heating-state.test.ts +140 -0
- package/test/skipSchema.test.ts +54 -0
- package/test/tuya-shadow.test.ts +159 -0
|
@@ -353,6 +353,66 @@ describe('LockAccessory', () => {
|
|
|
353
353
|
expect(allLogText()).toContain('Tuya reports this device as offline');
|
|
354
354
|
});
|
|
355
355
|
|
|
356
|
+
test('offline warnings are emitted once until the device comes back online', async () => {
|
|
357
|
+
device = createDevice({ online: false } as any);
|
|
358
|
+
const accessory = setup();
|
|
359
|
+
const offlineWarning = 'Tuya reports this device as offline';
|
|
360
|
+
|
|
361
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
362
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
363
|
+
expect(logs.filter(log => log.message.includes(offlineWarning))).toHaveLength(1);
|
|
364
|
+
|
|
365
|
+
device.online = true;
|
|
366
|
+
await accessory.onDeviceInfoUpdate({ bizCode: 'online' });
|
|
367
|
+
device.online = false;
|
|
368
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
369
|
+
|
|
370
|
+
expect(logs.filter(log => log.message.includes(offlineWarning))).toHaveLength(2);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test('offline command warnings are emitted once until the device comes back online', async () => {
|
|
374
|
+
device = createDevice({ online: false } as any);
|
|
375
|
+
const accessory = setup();
|
|
376
|
+
const offlineWarning = 'Device is offline, skip send command.';
|
|
377
|
+
const send = () => accessory.sendCommands([{ code: 'lock_motor_state', value: true }]);
|
|
378
|
+
|
|
379
|
+
await expect(send()).rejects.toBeDefined();
|
|
380
|
+
await expect(send()).rejects.toBeDefined();
|
|
381
|
+
expect(logs.filter(log => log.message.includes(offlineWarning))).toHaveLength(1);
|
|
382
|
+
|
|
383
|
+
device.online = true;
|
|
384
|
+
await accessory.onDeviceInfoUpdate({ bizCode: 'online' });
|
|
385
|
+
device.online = false;
|
|
386
|
+
await expect(send()).rejects.toBeDefined();
|
|
387
|
+
|
|
388
|
+
expect(logs.filter(log => log.message.includes(offlineWarning))).toHaveLength(2);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test('each kind of offline warning is emitted once until the device comes back online', async () => {
|
|
392
|
+
device = createDevice({ online: false } as any);
|
|
393
|
+
const accessory = setup();
|
|
394
|
+
const commandWarning = 'Device is offline, skip send command.';
|
|
395
|
+
const lockWarning = 'Tuya reports this device as offline';
|
|
396
|
+
const send = () => accessory.sendCommands([{ code: 'lock_motor_state', value: true }]);
|
|
397
|
+
const requestUnlock = () => targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
398
|
+
|
|
399
|
+
await expect(send()).rejects.toBeDefined();
|
|
400
|
+
await requestUnlock();
|
|
401
|
+
await expect(send()).rejects.toBeDefined();
|
|
402
|
+
await requestUnlock();
|
|
403
|
+
expect(logs.filter(log => log.message.includes(commandWarning))).toHaveLength(1);
|
|
404
|
+
expect(logs.filter(log => log.message.includes(lockWarning))).toHaveLength(1);
|
|
405
|
+
|
|
406
|
+
device.online = true;
|
|
407
|
+
await accessory.onDeviceInfoUpdate({ bizCode: 'online' });
|
|
408
|
+
device.online = false;
|
|
409
|
+
await expect(send()).rejects.toBeDefined();
|
|
410
|
+
await requestUnlock();
|
|
411
|
+
|
|
412
|
+
expect(logs.filter(log => log.message.includes(commandWarning))).toHaveLength(2);
|
|
413
|
+
expect(logs.filter(log => log.message.includes(lockWarning))).toHaveLength(2);
|
|
414
|
+
});
|
|
415
|
+
|
|
356
416
|
// ─── HTTP success must not imply an unlocked door ────────────────
|
|
357
417
|
|
|
358
418
|
test('a successful HTTP unlock does NOT mark LockCurrentState as unsecured', async () => {
|
|
@@ -465,6 +525,40 @@ describe('LockAccessory', () => {
|
|
|
465
525
|
expect(mockDeviceManager.getLockTemporaryKey).toHaveBeenCalledTimes(4);
|
|
466
526
|
});
|
|
467
527
|
|
|
528
|
+
test('logs 2312 once while retrying and logs again after a successful operation', async () => {
|
|
529
|
+
mockDeviceManager.sendLockCommands
|
|
530
|
+
.mockResolvedValueOnce(NOT_ONLINE as never)
|
|
531
|
+
.mockResolvedValueOnce(NOT_ONLINE as never)
|
|
532
|
+
.mockResolvedValueOnce({ success: true, result: true } as never);
|
|
533
|
+
|
|
534
|
+
setup();
|
|
535
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
536
|
+
await jest.advanceTimersByTimeAsync(20 * 1000);
|
|
537
|
+
|
|
538
|
+
const warning = '[Lock] door-operate rejected:';
|
|
539
|
+
expect(logs.filter(log => log.message.includes(warning))).toHaveLength(1);
|
|
540
|
+
|
|
541
|
+
mockDeviceManager.sendLockCommands.mockResolvedValue(NOT_ONLINE as never);
|
|
542
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
543
|
+
|
|
544
|
+
expect(logs.filter(log => log.message.includes(warning))).toHaveLength(2);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
test('an online notification resets the 2312 warning', async () => {
|
|
548
|
+
mockDeviceManager.sendLockCommands.mockResolvedValue(NOT_ONLINE as never);
|
|
549
|
+
|
|
550
|
+
const accessory = setup();
|
|
551
|
+
const warning = '[Lock] door-operate rejected:';
|
|
552
|
+
await targetStateCharacteristic().setHandler(LockTargetState.UNSECURED);
|
|
553
|
+
expect(logs.filter(log => log.message.includes(warning))).toHaveLength(1);
|
|
554
|
+
|
|
555
|
+
device.online = true;
|
|
556
|
+
await accessory.onDeviceInfoUpdate({ bizCode: 'online' });
|
|
557
|
+
await targetStateCharacteristic().setHandler(LockTargetState.SECURED);
|
|
558
|
+
|
|
559
|
+
expect(logs.filter(log => log.message.includes(warning))).toHaveLength(2);
|
|
560
|
+
});
|
|
561
|
+
|
|
468
562
|
test('stops once the window closes and falls back to the reported state', async () => {
|
|
469
563
|
mockDeviceManager.sendLockCommands.mockResolvedValue(NOT_ONLINE as never);
|
|
470
564
|
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import util from 'util';
|
|
3
|
+
import { describe, expect, test, beforeEach, afterEach, jest } from '@jest/globals';
|
|
4
|
+
|
|
5
|
+
import GarageDoorAccessory from '../../src/accessory/GarageDoorAccessory';
|
|
6
|
+
import LockAccessory from '../../src/accessory/LockAccessory';
|
|
7
|
+
import TuyaDevice, { TuyaDeviceSchemaMode, TuyaDeviceSchemaType } from '../../src/device/TuyaDevice';
|
|
8
|
+
import { initLogger } from '../../src/util/Logger';
|
|
9
|
+
|
|
10
|
+
// HAP constants, mirroring the real values.
|
|
11
|
+
const CurrentDoorState = { OPEN: 0, CLOSED: 1, OPENING: 2, CLOSING: 3, STOPPED: 4 };
|
|
12
|
+
const TargetDoorState = { OPEN: 0, CLOSED: 1 };
|
|
13
|
+
const LockCurrentState = { UNSECURED: 0, SECURED: 1, JAMMED: 2, UNKNOWN: 3 };
|
|
14
|
+
const LockTargetState = { UNSECURED: 0, SECURED: 1 };
|
|
15
|
+
|
|
16
|
+
class MockHapStatusError extends Error {
|
|
17
|
+
constructor(public hapStatus: number) {
|
|
18
|
+
super(`HapStatusError ${hapStatus}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const bool = (code: string) => ({
|
|
23
|
+
code, mode: TuyaDeviceSchemaMode.READ_WRITE, type: TuyaDeviceSchemaType.Boolean, property: {},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('timed garage door and lock state', () => {
|
|
27
|
+
let characteristics: Map<any, any>;
|
|
28
|
+
let services: any[];
|
|
29
|
+
let mockAccessory: any;
|
|
30
|
+
let mockPlatform: any;
|
|
31
|
+
let mockDeviceManager: any;
|
|
32
|
+
let deviceConfig: any;
|
|
33
|
+
let device: TuyaDevice;
|
|
34
|
+
let logs: string[];
|
|
35
|
+
|
|
36
|
+
const getCharacteristic = (type: any) => {
|
|
37
|
+
if (characteristics.has(type)) {
|
|
38
|
+
return characteristics.get(type);
|
|
39
|
+
}
|
|
40
|
+
const c: any = {
|
|
41
|
+
type,
|
|
42
|
+
value: null,
|
|
43
|
+
getHandler: undefined,
|
|
44
|
+
setHandler: undefined,
|
|
45
|
+
onGet: jest.fn((h: any) => (c.getHandler = h, c)),
|
|
46
|
+
onSet: jest.fn((h: any) => (c.setHandler = h, c)),
|
|
47
|
+
setProps: jest.fn(() => c),
|
|
48
|
+
updateValue: jest.fn((v: any) => (c.value = v, c)),
|
|
49
|
+
};
|
|
50
|
+
characteristics.set(type, c);
|
|
51
|
+
return c;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const makeService = (type: any) => {
|
|
55
|
+
const owned: any[] = [];
|
|
56
|
+
return {
|
|
57
|
+
UUID: type,
|
|
58
|
+
displayName: `${type}`,
|
|
59
|
+
subtype: undefined,
|
|
60
|
+
characteristics: owned,
|
|
61
|
+
getCharacteristic: jest.fn((t: any) => {
|
|
62
|
+
const c = getCharacteristic(t);
|
|
63
|
+
if (!owned.includes(c)) {
|
|
64
|
+
owned.push(c);
|
|
65
|
+
}
|
|
66
|
+
return c;
|
|
67
|
+
}),
|
|
68
|
+
setCharacteristic: jest.fn(function(this: any) {
|
|
69
|
+
return this;
|
|
70
|
+
}),
|
|
71
|
+
addOptionalCharacteristic: jest.fn(),
|
|
72
|
+
testCharacteristic: jest.fn(() => true),
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
jest.useFakeTimers();
|
|
78
|
+
characteristics = new Map();
|
|
79
|
+
services = [];
|
|
80
|
+
logs = [];
|
|
81
|
+
deviceConfig = undefined;
|
|
82
|
+
|
|
83
|
+
mockDeviceManager = {
|
|
84
|
+
getDevice: jest.fn(() => device),
|
|
85
|
+
sendCommands: jest.fn(async () => ({ success: true })),
|
|
86
|
+
getLockTemporaryKey: jest.fn(async () => ({ success: true, result: { ticket_id: 'T', expire_time: 300 } })),
|
|
87
|
+
sendLockCommands: jest.fn(async () => ({ success: true, result: true })),
|
|
88
|
+
getLockRemoteUnlockMethods: jest.fn(async () => ({ success: true, result: [] })),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const hap = {
|
|
92
|
+
Service: {
|
|
93
|
+
GarageDoorOpener: 'GarageDoorOpener',
|
|
94
|
+
LockMechanism: 'LockMechanism',
|
|
95
|
+
AccessoryInformation: 'AccessoryInformation',
|
|
96
|
+
Battery: 'Battery',
|
|
97
|
+
},
|
|
98
|
+
Characteristic: {
|
|
99
|
+
CurrentDoorState,
|
|
100
|
+
TargetDoorState,
|
|
101
|
+
LockCurrentState,
|
|
102
|
+
LockTargetState,
|
|
103
|
+
StatusLowBattery: { BATTERY_LEVEL_NORMAL: 0, BATTERY_LEVEL_LOW: 1 },
|
|
104
|
+
ChargingState: { NOT_CHARGING: 0, CHARGING: 1 },
|
|
105
|
+
BatteryLevel: 'BatteryLevel',
|
|
106
|
+
StatusActive: 'StatusActive',
|
|
107
|
+
Manufacturer: 'Manufacturer',
|
|
108
|
+
Model: 'Model',
|
|
109
|
+
Name: 'Name',
|
|
110
|
+
ConfiguredName: 'ConfiguredName',
|
|
111
|
+
SerialNumber: 'SerialNumber',
|
|
112
|
+
ProgrammableSwitchEvent: { UUID: 'ProgrammableSwitchEvent' },
|
|
113
|
+
},
|
|
114
|
+
HapStatusError: MockHapStatusError,
|
|
115
|
+
HAPStatus: { SERVICE_COMMUNICATION_FAILURE: -70402 },
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const record = (message?: any, ...args: any[]) => {
|
|
119
|
+
logs.push(util.format(message, ...args));
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
mockPlatform = {
|
|
123
|
+
api: { hap },
|
|
124
|
+
Service: hap.Service,
|
|
125
|
+
Characteristic: hap.Characteristic,
|
|
126
|
+
options: { debug: true, debugLevel: '' },
|
|
127
|
+
deviceManager: mockDeviceManager,
|
|
128
|
+
getDeviceConfig: jest.fn(() => deviceConfig),
|
|
129
|
+
getDeviceSchemaConfig: jest.fn(() => undefined),
|
|
130
|
+
log: { info: record, warn: record, error: record, debug: record },
|
|
131
|
+
};
|
|
132
|
+
initLogger(mockPlatform.log);
|
|
133
|
+
|
|
134
|
+
mockAccessory = {
|
|
135
|
+
UUID: 'uuid',
|
|
136
|
+
displayName: 'Test',
|
|
137
|
+
context: { deviceID: 'dev-1' },
|
|
138
|
+
services,
|
|
139
|
+
getService: jest.fn((type: any) => services.find(s => s.UUID === type)),
|
|
140
|
+
addService: jest.fn((type: any) => {
|
|
141
|
+
const s = makeService(type);
|
|
142
|
+
services.push(s);
|
|
143
|
+
return s;
|
|
144
|
+
}),
|
|
145
|
+
removeService: jest.fn(),
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
afterEach(() => {
|
|
150
|
+
jest.useRealTimers();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// ─── Garage ───────────────────────────────────────────────────────
|
|
154
|
+
|
|
155
|
+
describe('garage door (garageDoorTiming)', () => {
|
|
156
|
+
const setupGarage = () => {
|
|
157
|
+
device = new TuyaDevice({
|
|
158
|
+
id: 'dev-1', uuid: 'uuid', name: 'Garage', online: true, owner_id: 'o', product_id: 'p',
|
|
159
|
+
product_name: 'Garage', category: 'ckmkzq', sub: false,
|
|
160
|
+
schema: [bool('switch_1')],
|
|
161
|
+
status: [{ code: 'switch_1', value: false }],
|
|
162
|
+
} as any);
|
|
163
|
+
const garage = new GarageDoorAccessory(mockPlatform as any, mockAccessory as any);
|
|
164
|
+
garage.configureServices();
|
|
165
|
+
return garage;
|
|
166
|
+
};
|
|
167
|
+
const current = () => characteristics.get(CurrentDoorState);
|
|
168
|
+
const target = () => characteristics.get(TargetDoorState);
|
|
169
|
+
|
|
170
|
+
test('without garageDoorTiming, Open sends switch_1=true as before', async () => {
|
|
171
|
+
setupGarage();
|
|
172
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
173
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledWith('dev-1', [{ code: 'switch_1', value: true }]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('with a working doorcontact_state, garageDoorTiming is ignored', async () => {
|
|
177
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
178
|
+
device = new TuyaDevice({
|
|
179
|
+
id: 'dev-1', uuid: 'uuid', name: 'Garage', online: true, owner_id: 'o', product_id: 'p',
|
|
180
|
+
product_name: 'Garage', category: 'ckmkzq', sub: false,
|
|
181
|
+
schema: [bool('switch_1'), bool('doorcontact_state')],
|
|
182
|
+
status: [{ code: 'switch_1', value: false }, { code: 'doorcontact_state', value: false }],
|
|
183
|
+
} as any);
|
|
184
|
+
const garage = new GarageDoorAccessory(mockPlatform as any, mockAccessory as any);
|
|
185
|
+
garage.configureServices();
|
|
186
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
187
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledWith('dev-1', [{ code: 'switch_1', value: true }]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('Open sends exactly one pulse and runs Opening -> Open -> Closing -> Closed', async () => {
|
|
191
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
192
|
+
setupGarage();
|
|
193
|
+
expect(current().getHandler()).toBe(CurrentDoorState.CLOSED);
|
|
194
|
+
|
|
195
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
196
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledTimes(1);
|
|
197
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledWith('dev-1', [{ code: 'switch_1', value: false }]);
|
|
198
|
+
expect(current().getHandler()).toBe(CurrentDoorState.OPENING);
|
|
199
|
+
expect(target().getHandler()).toBe(TargetDoorState.OPEN);
|
|
200
|
+
|
|
201
|
+
await jest.advanceTimersByTimeAsync(8 * 1000);
|
|
202
|
+
expect(current().getHandler()).toBe(CurrentDoorState.OPEN);
|
|
203
|
+
|
|
204
|
+
await jest.advanceTimersByTimeAsync(17 * 1000);
|
|
205
|
+
expect(current().getHandler()).toBe(CurrentDoorState.CLOSING);
|
|
206
|
+
expect(target().getHandler()).toBe(TargetDoorState.CLOSED);
|
|
207
|
+
|
|
208
|
+
await jest.advanceTimersByTimeAsync(8 * 1000);
|
|
209
|
+
expect(current().getHandler()).toBe(CurrentDoorState.CLOSED);
|
|
210
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledTimes(1);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('a second Open during the cycle sends nothing', async () => {
|
|
214
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
215
|
+
setupGarage();
|
|
216
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
217
|
+
await jest.advanceTimersByTimeAsync(3 * 1000);
|
|
218
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
219
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledTimes(1);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('Close only resets the display and sends nothing', async () => {
|
|
223
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
224
|
+
setupGarage();
|
|
225
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
226
|
+
await target().setHandler(TargetDoorState.CLOSED);
|
|
227
|
+
expect(mockDeviceManager.sendCommands).toHaveBeenCalledTimes(1);
|
|
228
|
+
expect(current().getHandler()).toBe(CurrentDoorState.CLOSED);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('a press reported from outside HomeKit runs the cycle without sending', async () => {
|
|
232
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
233
|
+
const garage = setupGarage();
|
|
234
|
+
await garage.onDeviceStatusUpdate([{ code: 'switch_1', value: true }]);
|
|
235
|
+
expect(mockDeviceManager.sendCommands).not.toHaveBeenCalled();
|
|
236
|
+
expect(current().getHandler()).toBe(CurrentDoorState.OPENING);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('the echo of our own command does not start a second cycle', async () => {
|
|
240
|
+
deviceConfig = { id: 'dev-1', garageDoorTiming: { opening: 8, open: 17, closing: 8 } };
|
|
241
|
+
const garage = setupGarage();
|
|
242
|
+
await target().setHandler(TargetDoorState.OPEN);
|
|
243
|
+
await jest.advanceTimersByTimeAsync(2 * 1000);
|
|
244
|
+
await garage.onDeviceStatusUpdate([{ code: 'switch_1', value: false }]);
|
|
245
|
+
await jest.advanceTimersByTimeAsync(6 * 1000);
|
|
246
|
+
expect(current().getHandler()).toBe(CurrentDoorState.OPEN);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// ─── Lock ─────────────────────────────────────────────────────────
|
|
251
|
+
|
|
252
|
+
describe('lock (lockTiming)', () => {
|
|
253
|
+
const setupLock = (status: any[] = []) => {
|
|
254
|
+
device = new TuyaDevice({
|
|
255
|
+
id: 'dev-1', uuid: 'uuid', name: 'Lock Box', online: true, owner_id: 'o', product_id: 'p',
|
|
256
|
+
product_name: 'Lock Box', category: 'jtmspro', sub: true,
|
|
257
|
+
schema: [bool('lock_motor_state')],
|
|
258
|
+
status: [{ code: 'lock_motor_state', value: false }, ...status],
|
|
259
|
+
} as any);
|
|
260
|
+
const lock = new LockAccessory(mockPlatform as any, mockAccessory as any);
|
|
261
|
+
lock.configureServices();
|
|
262
|
+
return lock;
|
|
263
|
+
};
|
|
264
|
+
const current = () => characteristics.get(LockCurrentState);
|
|
265
|
+
const target = () => characteristics.get(LockTargetState);
|
|
266
|
+
|
|
267
|
+
test('Unlock opens once, shows Unlocked for the set time, then Locked', async () => {
|
|
268
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
269
|
+
setupLock();
|
|
270
|
+
|
|
271
|
+
await target().setHandler(LockTargetState.UNSECURED);
|
|
272
|
+
expect(mockDeviceManager.sendLockCommands).toHaveBeenCalledWith('dev-1', 'T', true);
|
|
273
|
+
expect(current().getHandler()).toBe(LockCurrentState.UNSECURED);
|
|
274
|
+
expect(target().getHandler()).toBe(LockTargetState.UNSECURED);
|
|
275
|
+
|
|
276
|
+
await jest.advanceTimersByTimeAsync(10 * 1000);
|
|
277
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
278
|
+
expect(target().getHandler()).toBe(LockTargetState.SECURED);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('unlocked -1 follows auto_lock_time + 1s', async () => {
|
|
282
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: -1 } };
|
|
283
|
+
setupLock([{ code: 'auto_lock_time', value: 5 }]);
|
|
284
|
+
|
|
285
|
+
await target().setHandler(LockTargetState.UNSECURED);
|
|
286
|
+
await jest.advanceTimersByTimeAsync(5 * 1000);
|
|
287
|
+
expect(current().getHandler()).toBe(LockCurrentState.UNSECURED);
|
|
288
|
+
await jest.advanceTimersByTimeAsync(1 * 1000);
|
|
289
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('Lock sends nothing and shows Locked', async () => {
|
|
293
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
294
|
+
setupLock();
|
|
295
|
+
await target().setHandler(LockTargetState.UNSECURED);
|
|
296
|
+
await target().setHandler(LockTargetState.SECURED);
|
|
297
|
+
expect(mockDeviceManager.sendLockCommands).toHaveBeenCalledTimes(1);
|
|
298
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('never stays stuck: no device report is needed to return to Locked', async () => {
|
|
302
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
303
|
+
setupLock();
|
|
304
|
+
await target().setHandler(LockTargetState.UNSECURED);
|
|
305
|
+
await jest.advanceTimersByTimeAsync(11 * 1000);
|
|
306
|
+
expect(target().getHandler()).toBe(LockTargetState.SECURED);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test('a failed unlock shows Locked and reports the error to HomeKit', async () => {
|
|
310
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
311
|
+
mockDeviceManager.sendLockCommands.mockResolvedValueOnce({ success: false, code: 1, msg: 'nope' });
|
|
312
|
+
setupLock();
|
|
313
|
+
await expect(target().setHandler(LockTargetState.UNSECURED)).rejects.toBeInstanceOf(MockHapStatusError);
|
|
314
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('an unreachable lock is retried, and the cycle starts when it answers', async () => {
|
|
318
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
319
|
+
mockDeviceManager.sendLockCommands
|
|
320
|
+
.mockResolvedValueOnce({ success: false, code: 2312, msg: 'offline' })
|
|
321
|
+
.mockResolvedValueOnce({ success: true, result: true });
|
|
322
|
+
setupLock();
|
|
323
|
+
|
|
324
|
+
await target().setHandler(LockTargetState.UNSECURED);
|
|
325
|
+
expect(target().getHandler()).toBe(LockTargetState.UNSECURED);
|
|
326
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
327
|
+
|
|
328
|
+
await jest.advanceTimersByTimeAsync(10 * 1000);
|
|
329
|
+
expect(mockDeviceManager.sendLockCommands).toHaveBeenCalledTimes(2);
|
|
330
|
+
expect(current().getHandler()).toBe(LockCurrentState.UNSECURED);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('an unlock reported by the lock itself runs the cycle', async () => {
|
|
334
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
335
|
+
const lock = setupLock();
|
|
336
|
+
await lock.onDeviceStatusUpdate([{ code: 'unlock_fingerprint', value: 3 }]);
|
|
337
|
+
expect(current().getHandler()).toBe(LockCurrentState.UNSECURED);
|
|
338
|
+
expect(mockDeviceManager.sendLockCommands).not.toHaveBeenCalled();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test('record-type unlock dps are ignored', async () => {
|
|
342
|
+
deviceConfig = { id: 'dev-1', lockTiming: { unlocked: 10 } };
|
|
343
|
+
const lock = setupLock();
|
|
344
|
+
await lock.onDeviceStatusUpdate([{ code: 'unlock_method_create', value: 1 }]);
|
|
345
|
+
expect(current().getHandler()).toBe(LockCurrentState.SECURED);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
describe('settings screen', () => {
|
|
351
|
+
test('timing fields are number boxes, not sliders', () => {
|
|
352
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
353
|
+
const { layout } = require('../../config.schema.json');
|
|
354
|
+
const flat: { key?: string; type?: string }[] = [];
|
|
355
|
+
const walk = (items: unknown[]) => items.forEach((i) => {
|
|
356
|
+
const item = i as { items?: unknown[]; key?: string; type?: string };
|
|
357
|
+
if (item && typeof item === 'object') {
|
|
358
|
+
if (item.key) {
|
|
359
|
+
flat.push(item);
|
|
360
|
+
}
|
|
361
|
+
if (Array.isArray(item.items)) {
|
|
362
|
+
walk(item.items);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
walk(layout);
|
|
367
|
+
for (const key of ['garageDoorTiming.opening', 'garageDoorTiming.open', 'garageDoorTiming.closing', 'lockTiming.unlocked']) {
|
|
368
|
+
const item = flat.find((i) => i.key === `options.deviceOverrides[].${key}`);
|
|
369
|
+
expect(item?.type).toBe('number');
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
});
|