@homebridge-plugins/homebridge-tuya 2.7.2-beta.5 → 2.7.2-beta.7
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 +70 -0
- package/dist/accessory/AccessoryFactory.js +3 -0
- package/dist/accessory/AccessoryFactory.js.map +1 -1
- package/dist/accessory/BaseAccessory.js +2 -0
- 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 +119 -0
- package/dist/accessory/LockAccessory.js.map +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 +1 -1
- package/test/accessory/et72gwAccessory.test.ts +489 -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
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import ThermostatAccessory from '../../src/accessory/ThermostatAccessory';
|
|
3
|
+
import ET72GWAccessory from '../../src/accessory/ET72GWAccessory';
|
|
4
|
+
import AccessoryFactory from '../../src/accessory/AccessoryFactory';
|
|
5
|
+
import TuyaDevice from '../../src/device/TuyaDevice';
|
|
6
|
+
import { initLogger } from '../../src/util/Logger';
|
|
7
|
+
import { TuyaPlatform } from '../../src/platform';
|
|
8
|
+
|
|
9
|
+
class MockHapStatusError extends Error {
|
|
10
|
+
constructor(public hapStatus: number) { super(String(hapStatus)); }
|
|
11
|
+
}
|
|
12
|
+
const deviceID = 'et72gw-test-device';
|
|
13
|
+
const schemaOverride = {
|
|
14
|
+
code: 'temp_set', property: { unit: '°C', min: 50, max: 400, scale: 1, step: 1 },
|
|
15
|
+
onGet: '(value - 320) * 5 / 9', onSet: 'Math.round(value * 9 / 5 + 320)',
|
|
16
|
+
};
|
|
17
|
+
const payload = () => ({ properties: [
|
|
18
|
+
{ code: 'ControlMode', dpId: 103, value: 'Manual', time: Date.now() - 240000 },
|
|
19
|
+
{ code: 'FloorTemper', dpId: 106, value: 820, time: Date.now() - 240000 },
|
|
20
|
+
{ code: 'RoomTemper', dpId: 105, value: 740, time: Date.now() - 240000 },
|
|
21
|
+
{ code: 'SetTemp', dpId: 102, value: 800, time: Date.now() - 240000 },
|
|
22
|
+
{ code: 'TemperUnitChange', dpId: 107, value: true, time: Date.now() - 1000000 },
|
|
23
|
+
{ code: 'work_status', dpId: 118, value: false, time: Date.now() - 240000 },
|
|
24
|
+
{ code: 'power1', dpId: 101, value: true, time: Date.now() - 1000000 },
|
|
25
|
+
] });
|
|
26
|
+
|
|
27
|
+
describe('automatically recognized ET-72GW accessory', () => {
|
|
28
|
+
const handlers: ThermostatAccessory[] = [];
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
jest.useFakeTimers({ now: 1800000000000 });
|
|
31
|
+
initLogger({ info: jest.fn(), debug: jest.fn(), warn: jest.fn(), error: jest.fn() });
|
|
32
|
+
});
|
|
33
|
+
afterEach(() => { handlers.forEach(handler => handler.dispose()); handlers.length = 0; jest.useRealTimers(); });
|
|
34
|
+
|
|
35
|
+
const setup = (withOverride = false, product = 'm2dkohai78cwr3yd', configID = deviceID, heating = false) => {
|
|
36
|
+
const characteristics = new Map<any, any>();
|
|
37
|
+
const makeCharacteristic = (type: any) => {
|
|
38
|
+
const c: any = {
|
|
39
|
+
UUID: type?.UUID ?? type, value: null,
|
|
40
|
+
onGet: jest.fn((get: any) => { c.getHandler = get; return c; }),
|
|
41
|
+
onSet: jest.fn((set: any) => { c.setHandler = set; return c; }),
|
|
42
|
+
setProps: jest.fn((props: any) => { c.props = props; return c; }),
|
|
43
|
+
updateValue: jest.fn((value: any) => { c.value = value; return c; }),
|
|
44
|
+
};
|
|
45
|
+
characteristics.set(type, c); return c;
|
|
46
|
+
};
|
|
47
|
+
const services: any[] = [];
|
|
48
|
+
const accessory: any = {
|
|
49
|
+
UUID: 'existing-accessory-uuid', context: { deviceID }, services,
|
|
50
|
+
getService: (type: any) => services.find(s => s.UUID === type),
|
|
51
|
+
addService: (type: any) => {
|
|
52
|
+
const s: any = {
|
|
53
|
+
UUID: type, characteristics: [], emit: jest.fn(),
|
|
54
|
+
getCharacteristic: (key: any) => {
|
|
55
|
+
const c = characteristics.get(key) || makeCharacteristic(key);
|
|
56
|
+
if (!s.characteristics.includes(c)) { s.characteristics.push(c); }
|
|
57
|
+
return c;
|
|
58
|
+
},
|
|
59
|
+
setCharacteristic: () => s,
|
|
60
|
+
testCharacteristic: () => true,
|
|
61
|
+
};
|
|
62
|
+
services.push(s); return s;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const device = new TuyaDevice({
|
|
66
|
+
id: deviceID, uuid: 'existing-device-uuid', name: 'Floor thermostat', online: true,
|
|
67
|
+
product_id: product, category: 'wk', schema: [
|
|
68
|
+
{ code: 'temp_set', mode: 'rw', type: 'Integer', property: { min: 50, max: 400, scale: 1, step: 1, unit: '°C' } },
|
|
69
|
+
{ code: 'temp_unit_convert', mode: 'rw', type: 'Enum', property: { range: ['c', 'f'] } },
|
|
70
|
+
], status: [{ code: 'temp_set', value: 800 }, { code: 'temp_unit_convert', value: 'f' }],
|
|
71
|
+
} as any);
|
|
72
|
+
if (heating) {
|
|
73
|
+
device.schema.push(
|
|
74
|
+
{ code: 'switch', mode: 'rw', type: 'Boolean', property: {} } as any,
|
|
75
|
+
{ code: 'mode', mode: 'rw', type: 'Enum', property: { range: ['hot', 'auto'] } } as any,
|
|
76
|
+
);
|
|
77
|
+
device.status.push({ code: 'switch', value: true }, { code: 'mode', value: 'hot' });
|
|
78
|
+
}
|
|
79
|
+
const shadow = jest.fn<() => Promise<unknown>>().mockResolvedValue(payload());
|
|
80
|
+
const commands = jest.fn();
|
|
81
|
+
const setMode = jest.fn<() => Promise<void>>().mockResolvedValue(undefined);
|
|
82
|
+
const characteristic: any = {
|
|
83
|
+
CurrentHeatingCoolingState: { OFF: 0, HEAT: 1, COOL: 2 }, TargetHeatingCoolingState: { OFF: 0, HEAT: 1, COOL: 2, AUTO: 3 },
|
|
84
|
+
CurrentTemperature: 'CurrentTemperature', TargetTemperature: 'TargetTemperature', TemperatureDisplayUnits: { CELSIUS: 0, FAHRENHEIT: 1 },
|
|
85
|
+
StatusLowBattery: {}, ChargingState: {}, ProgrammableSwitchEvent: { UUID: 'event' },
|
|
86
|
+
};
|
|
87
|
+
for (const name of ['Manufacturer', 'Model', 'Name', 'ConfiguredName', 'SerialNumber']) { characteristic[name] = name; }
|
|
88
|
+
const platform: any = {
|
|
89
|
+
api: { hap: {
|
|
90
|
+
Characteristic: characteristic, Service: { AccessoryInformation: 'Info', Battery: 'Battery', Thermostat: 'Thermostat' },
|
|
91
|
+
HapStatusError: MockHapStatusError, HAPStatus: { SERVICE_COMMUNICATION_FAILURE: -70402, INVALID_VALUE_IN_REQUEST: -70410 },
|
|
92
|
+
} },
|
|
93
|
+
options: withOverride ? { deviceOverrides: [{ id: configID, schema: [schemaOverride] }] } : {},
|
|
94
|
+
deviceManager: { getDevice: () => device, api: { getDeviceShadow: shadow, setDeviceControlMode: setMode }, sendCommands: commands },
|
|
95
|
+
getDeviceConfig: TuyaPlatform.prototype.getDeviceConfig,
|
|
96
|
+
getDeviceSchemaConfig: TuyaPlatform.prototype.getDeviceSchemaConfig,
|
|
97
|
+
};
|
|
98
|
+
const handler = AccessoryFactory.createAccessory(platform, accessory, device) as ThermostatAccessory;
|
|
99
|
+
handlers.push(handler);
|
|
100
|
+
return {
|
|
101
|
+
handler, accessory, platform, device, shadow, commands, setMode,
|
|
102
|
+
current: characteristics.get('CurrentTemperature'), target: characteristics.get('TargetTemperature'),
|
|
103
|
+
state: characteristics.get(characteristic.CurrentHeatingCoolingState),
|
|
104
|
+
targetState: characteristics.get(characteristic.TargetHeatingCoolingState),
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
test('actual floor replaces setpoint, leaves state/identities/cache untouched, and never sends commands', async () => {
|
|
109
|
+
const t = setup();
|
|
110
|
+
expect(() => t.current.getHandler()).toThrow(MockHapStatusError);
|
|
111
|
+
expect(t.current.value).toBeInstanceOf(MockHapStatusError);
|
|
112
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
113
|
+
expect(t.current.getHandler()).toBeCloseTo(27.7777778);
|
|
114
|
+
expect(t.current.value).toBeCloseTo(27.7777778);
|
|
115
|
+
expect(t.target.getHandler()).toBeCloseTo(26.6666667);
|
|
116
|
+
expect(t.state.getHandler()).toBe(0); // Explicit idle relay, regardless of the unchanged target mode.
|
|
117
|
+
expect(t.device.status.find(s => s.code === 'temp_set')?.value).toBe(800);
|
|
118
|
+
expect(t.accessory.UUID).toBe('existing-accessory-uuid');
|
|
119
|
+
expect(t.accessory.context.deviceID).toBe(deviceID);
|
|
120
|
+
for (let i = 0; i < 20; i++) { t.current.getHandler(); }
|
|
121
|
+
expect(t.shadow).toHaveBeenCalledTimes(1);
|
|
122
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
123
|
+
t.device.online = false;
|
|
124
|
+
await t.handler.onDeviceInfoUpdate({});
|
|
125
|
+
expect(() => t.current.getHandler()).toThrow(MockHapStatusError);
|
|
126
|
+
expect(t.current.value).toBeInstanceOf(MockHapStatusError);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('relay notifications never turn idle into a target-mode or setpoint change', async () => {
|
|
130
|
+
const t = setup(true, 'm2dkohai78cwr3yd', deviceID, true);
|
|
131
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
132
|
+
expect(t.state.value).toBe(0);
|
|
133
|
+
const before = JSON.stringify(t.device.status);
|
|
134
|
+
const mode = t.targetState.getHandler();
|
|
135
|
+
const target = t.target.getHandler();
|
|
136
|
+
for (const active of [true, false, true]) {
|
|
137
|
+
t.shadow.mockImplementation(async () => ({ properties: payload().properties.map(p => p.dpId === 118 ? { ...p, value: active } : p) }));
|
|
138
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
139
|
+
expect(t.state.getHandler()).toBe(active ? 1 : 0);
|
|
140
|
+
expect(t.state.updateValue).toHaveBeenLastCalledWith(active ? 1 : 0);
|
|
141
|
+
expect(t.targetState.getHandler()).toBe(mode);
|
|
142
|
+
expect(t.target.getHandler()).toBe(target);
|
|
143
|
+
expect(JSON.stringify(t.device.status)).toBe(before);
|
|
144
|
+
}
|
|
145
|
+
expect(t.shadow).toHaveBeenCalledTimes(4); // One response supplies both readings.
|
|
146
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('shadow power-off reports both relay and target off without changing cached legacy mode', async () => {
|
|
150
|
+
const t = setup(true, 'm2dkohai78cwr3yd', deviceID, true);
|
|
151
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
152
|
+
t.shadow.mockImplementation(async () => ({ properties: payload().properties.map(p => p.dpId === 118 ? { ...p, value: true }
|
|
153
|
+
: p.dpId === 101 ? { ...p, value: false } : p) }));
|
|
154
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
155
|
+
expect(t.state.getHandler()).toBe(0);
|
|
156
|
+
expect(t.targetState.getHandler()).toBe(0);
|
|
157
|
+
expect(t.device.status.find(s => s.code === 'switch')?.value).toBe(true);
|
|
158
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('timestamped shadow power is authoritative over an untimestamped legacy power cache', async () => {
|
|
162
|
+
const t = setup(true, 'm2dkohai78cwr3yd', deviceID, true);
|
|
163
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
164
|
+
t.device.status.find(s => s.code === 'switch')!.value = false;
|
|
165
|
+
t.shadow.mockImplementation(async () => ({ properties: payload().properties.map(p => p.dpId === 118 ? { ...p, value: true } : p) }));
|
|
166
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
167
|
+
expect(t.state.getHandler()).toBe(1); // Actual shadow power=true/relay=true.
|
|
168
|
+
expect(t.targetState.getHandler()).toBe(1); // Shadow power and Manual mode also govern target state.
|
|
169
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('missing relay publishes unavailable rather than inferring activity from temperature', async () => {
|
|
173
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
174
|
+
t.shadow.mockResolvedValue({ properties: payload().properties.filter(p => p.dpId !== 118) });
|
|
175
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
176
|
+
expect(() => t.state.getHandler()).toThrow(MockHapStatusError);
|
|
177
|
+
expect(t.state.value).toBeInstanceOf(MockHapStatusError);
|
|
178
|
+
expect(t.current.getHandler()).toBeCloseTo(27.77778);
|
|
179
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test.each([false, true])('target conversion and exact commands work with explicit override=%s', async withOverride => {
|
|
183
|
+
const t = setup(withOverride);
|
|
184
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
185
|
+
expect(t.target.getHandler()).toBeCloseTo(26.6666667);
|
|
186
|
+
await t.target.setHandler(27.3); // Mock command spy only: never connected to a device.
|
|
187
|
+
expect(t.commands).toHaveBeenCalledTimes(1);
|
|
188
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 811 }]);
|
|
189
|
+
expect(t.target.getHandler()).toBeCloseTo((811 - 320) * 5 / 9 / 10);
|
|
190
|
+
expect(t.current.getHandler()).toBeCloseTo(27.7777778);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test.each([false, true])('existing heating commands remain unchanged with opt-in=%s', async enabled => {
|
|
194
|
+
const t = setup(enabled, 'm2dkohai78cwr3yd', deviceID, true);
|
|
195
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
196
|
+
await t.targetState.setHandler(0);
|
|
197
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'switch', value: false }]);
|
|
198
|
+
await t.targetState.setHandler(1);
|
|
199
|
+
expect(t.setMode).toHaveBeenLastCalledWith(deviceID, 'Manual', true);
|
|
200
|
+
expect(t.commands).toHaveBeenCalledTimes(1);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('Manual and Smart map to Heat and Auto without issuing commands on refresh', async () => {
|
|
204
|
+
const t = setup();
|
|
205
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
206
|
+
expect(t.targetState.props.validValues).toEqual([0, 1, 3]);
|
|
207
|
+
expect(t.targetState.getHandler()).toBe(1);
|
|
208
|
+
t.shadow.mockResolvedValue({ properties: payload().properties.map(p => p.dpId === 103
|
|
209
|
+
? { ...p, value: 'Smart', time: Date.now() } : p) });
|
|
210
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
211
|
+
expect(t.targetState.getHandler()).toBe(3);
|
|
212
|
+
expect(t.targetState.updateValue).toHaveBeenLastCalledWith(3);
|
|
213
|
+
expect(t.state.getHandler()).toBe(0);
|
|
214
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
215
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
216
|
+
expect(t.shadow).toHaveBeenCalledTimes(2);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('mode capabilities announce a configuration change once, without polling announcements or commands', async () => {
|
|
220
|
+
const t = setup();
|
|
221
|
+
const service = t.handler.mainService() as any;
|
|
222
|
+
expect(service.emit).toHaveBeenCalledTimes(1);
|
|
223
|
+
expect(service.emit).toHaveBeenCalledWith('service-configurationChange');
|
|
224
|
+
await jest.advanceTimersByTimeAsync(600000);
|
|
225
|
+
expect(service.emit).toHaveBeenCalledTimes(1);
|
|
226
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
227
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('explicit mode writes leave the setpoint alone and reject cooling/offline requests', async () => {
|
|
231
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
232
|
+
const target = t.target.getHandler();
|
|
233
|
+
await t.targetState.setHandler(3);
|
|
234
|
+
await t.targetState.setHandler(1);
|
|
235
|
+
expect(t.setMode.mock.calls).toEqual([[deviceID, 'Smart', false], [deviceID, 'Manual', false]]);
|
|
236
|
+
await expect(t.targetState.setHandler(2)).rejects.toThrow(MockHapStatusError);
|
|
237
|
+
t.device.online = false;
|
|
238
|
+
await expect(t.targetState.setHandler(1)).rejects.toThrow(MockHapStatusError);
|
|
239
|
+
expect(t.setMode).toHaveBeenCalledTimes(2);
|
|
240
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
241
|
+
expect(t.target.getHandler()).toBe(target);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('selecting Heat from reported Off powers on without a setpoint command', async () => {
|
|
245
|
+
const t = setup();
|
|
246
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
247
|
+
t.shadow.mockResolvedValue({ properties: payload().properties.map(p => p.dpId === 101 ? { ...p, value: false, time: Date.now() } : p) });
|
|
248
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
249
|
+
expect(t.targetState.getHandler()).toBe(0);
|
|
250
|
+
await t.targetState.setHandler(1);
|
|
251
|
+
expect(t.setMode).toHaveBeenCalledWith(deviceID, 'Manual', true);
|
|
252
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
253
|
+
expect(t.target.getHandler()).toBeCloseTo(26.6666667);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('mode changes with unavailable power cache do not issue a guessed power command', async () => {
|
|
257
|
+
const t = setup();
|
|
258
|
+
await expect(t.targetState.setHandler(1)).rejects.toThrow(MockHapStatusError);
|
|
259
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
260
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test.each([false, true])('Manual with a newer power timestamp keeps reported activity=%s and notifications', async active => {
|
|
264
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
265
|
+
t.shadow.mockResolvedValue({ properties: payload().properties.map(p => p.dpId === 101 ? { ...p, time: Date.now() }
|
|
266
|
+
: p.dpId === 118 ? { ...p, value: active, time: Date.now() - 1000 } : p) });
|
|
267
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
268
|
+
expect(t.targetState.getHandler()).toBe(1);
|
|
269
|
+
expect(t.state.getHandler()).toBe(active ? 1 : 0);
|
|
270
|
+
expect(t.state.updateValue).toHaveBeenLastCalledWith(active ? 1 : 0);
|
|
271
|
+
expect(t.current.getHandler()).toBeCloseTo(27.7777778);
|
|
272
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
273
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('failed mode commands propagate without changing other controls', async () => {
|
|
277
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
278
|
+
t.setMode.mockRejectedValue(new Error('rejected'));
|
|
279
|
+
await expect(t.targetState.setHandler(1)).rejects.toThrow('rejected');
|
|
280
|
+
expect(t.setMode).toHaveBeenCalledTimes(1);
|
|
281
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
282
|
+
expect(t.target.getHandler()).toBeCloseTo(26.6666667);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test.each(['missing', 'unknown', 'future', 'duplicate', 'conflicting', 'regressing', 'invalid-time', 'malformed'])('invalid %s mode is unavailable independently of floor and relay', async kind => {
|
|
286
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
287
|
+
let properties = payload().properties;
|
|
288
|
+
if (kind === 'missing') { properties = properties.filter(p => p.dpId !== 103); }
|
|
289
|
+
if (kind === 'unknown') { properties = properties.map(p => p.dpId === 103 ? { ...p, value: 'Anti_frozen' } : p); }
|
|
290
|
+
if (kind === 'future') { properties = properties.map(p => p.dpId === 103 ? { ...p, time: Date.now() + 1000000 } : p); }
|
|
291
|
+
if (kind === 'regressing') { properties = properties.map(p => p.dpId === 103 ? { ...p, time: p.time - 1 } : p); }
|
|
292
|
+
if (kind === 'invalid-time') { properties = properties.map(p => p.dpId === 103 ? { ...p, time: 0 } : p); }
|
|
293
|
+
if (kind === 'malformed') { properties = properties.map(p => p.dpId === 103 ? { ...p, value: 1 } : p); }
|
|
294
|
+
if (kind === 'duplicate') { properties.push(properties.find(p => p.dpId === 103)!); }
|
|
295
|
+
if (kind === 'conflicting') { properties = properties.map(p => p.dpId === 103 ? { ...p, value: 'Smart' } : p); }
|
|
296
|
+
t.shadow.mockResolvedValue({ properties });
|
|
297
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
298
|
+
expect(() => t.targetState.getHandler()).toThrow(MockHapStatusError);
|
|
299
|
+
expect(t.targetState.value).toBeInstanceOf(MockHapStatusError);
|
|
300
|
+
expect(t.current.getHandler()).toBeCloseTo(27.7777778);
|
|
301
|
+
expect(t.state.getHandler()).toBe(0);
|
|
302
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('power off remains readable even with missing mode; expired mode is unavailable', async () => {
|
|
306
|
+
const t = setup(); await jest.advanceTimersByTimeAsync(0);
|
|
307
|
+
t.shadow.mockResolvedValue({ properties: payload().properties.filter(p => p.dpId !== 103)
|
|
308
|
+
.map(p => p.dpId === 101 ? { ...p, value: false, time: Date.now() } : p) });
|
|
309
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
310
|
+
expect(t.targetState.getHandler()).toBe(0);
|
|
311
|
+
t.shadow.mockRejectedValue(new Error('transport'));
|
|
312
|
+
await jest.advanceTimersByTimeAsync(900000);
|
|
313
|
+
expect(() => t.targetState.getHandler()).toThrow(MockHapStatusError);
|
|
314
|
+
expect(t.setMode).not.toHaveBeenCalled();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('factory automatically selects only the verified product before category fallback', async () => {
|
|
318
|
+
const t = setup();
|
|
319
|
+
expect(t.platform.options.deviceOverrides).toBeUndefined();
|
|
320
|
+
expect(t.handler).toBeInstanceOf(ET72GWAccessory);
|
|
321
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
322
|
+
expect(t.shadow).toHaveBeenCalledTimes(1);
|
|
323
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('unknown products keep the generic thermostat and never poll shadow', async () => {
|
|
327
|
+
const t = setup(false, 'unverified-product');
|
|
328
|
+
expect(t.handler).toBeInstanceOf(ThermostatAccessory);
|
|
329
|
+
expect(t.handler).not.toBeInstanceOf(ET72GWAccessory);
|
|
330
|
+
const before = t.current.getHandler();
|
|
331
|
+
await jest.advanceTimersByTimeAsync(900000);
|
|
332
|
+
expect(t.shadow).not.toHaveBeenCalled();
|
|
333
|
+
expect(t.current.getHandler()).toBe(before);
|
|
334
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test.each([
|
|
338
|
+
['f', 500, 10], ['f', 680, 20], ['f', 750, 23.8888889], ['f', 800, 26.6666667], ['f', 900, 32.2222222],
|
|
339
|
+
['f', 1000, 37.7777778], ['c', 100, 10], ['c', 225, 22.5], ['c', 250, 25], ['c', 380, 38],
|
|
340
|
+
])('native %s target %s reads as Celsius %s and round-trips without overrides', async (unit, raw, celsius) => {
|
|
341
|
+
const t = setup();
|
|
342
|
+
t.device.status.find(s => s.code === 'temp_unit_convert')!.value = unit;
|
|
343
|
+
t.device.status.find(s => s.code === 'temp_set')!.value = raw;
|
|
344
|
+
const value = t.target.getHandler();
|
|
345
|
+
expect(value).toBeCloseTo(celsius);
|
|
346
|
+
await t.target.setHandler(value);
|
|
347
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: raw }]);
|
|
348
|
+
expect(t.target.getHandler()).toBeCloseTo(celsius);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test.each([
|
|
352
|
+
['f', 26.7, 801], ['f', 27.3, 811], ['f', 32.2, 900], ['f', 26.69, 800],
|
|
353
|
+
['c', 25.65, 257], ['c', 27.3, 273], ['c', 32.2, 322],
|
|
354
|
+
])('native %s target writes Celsius %s as integer %s', async (unit, celsius, raw) => {
|
|
355
|
+
const t = setup();
|
|
356
|
+
t.device.status.find(s => s.code === 'temp_unit_convert')!.value = unit;
|
|
357
|
+
await t.target.setHandler(celsius);
|
|
358
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: raw }]);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test('target unit follows the reported unit after a switch, not the schema label or floor cache', async () => {
|
|
362
|
+
const t = setup();
|
|
363
|
+
expect(t.target.getHandler()).toBeCloseTo((80 - 32) * 5 / 9);
|
|
364
|
+
t.device.status.find(s => s.code === 'temp_unit_convert')!.value = 'c';
|
|
365
|
+
t.device.status.find(s => s.code === 'temp_set')!.value = 250;
|
|
366
|
+
expect(t.target.getHandler()).toBe(25);
|
|
367
|
+
await t.target.setHandler(26);
|
|
368
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 260 }]);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test.each(['invalid', true, undefined])('unknown target unit %s is unavailable and cannot send a command', async unit => {
|
|
372
|
+
const t = setup();
|
|
373
|
+
t.device.status.find(s => s.code === 'temp_unit_convert')!.value = unit as any;
|
|
374
|
+
expect(() => t.target.getHandler()).toThrow(MockHapStatusError);
|
|
375
|
+
await expect(t.target.setHandler(25)).rejects.toBeInstanceOf(MockHapStatusError);
|
|
376
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test.each([NaN, Infinity, '25', undefined, 9, 39])('invalid target write %s sends no command', async value => {
|
|
380
|
+
const t = setup();
|
|
381
|
+
await expect(t.target.setHandler(value)).rejects.toBeInstanceOf(MockHapStatusError);
|
|
382
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test('configuration refresh cancels the old polling instance', async () => {
|
|
386
|
+
const t = setup();
|
|
387
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
388
|
+
t.handler.configureServices();
|
|
389
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
390
|
+
expect(t.shadow).toHaveBeenCalledTimes(2);
|
|
391
|
+
await jest.advanceTimersByTimeAsync(300000);
|
|
392
|
+
expect(t.shadow).toHaveBeenCalledTimes(3);
|
|
393
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test.each([undefined, NaN, Infinity, 1000, -1000, 0.5])(
|
|
397
|
+
'malformed explicit conversion scale %s cannot send a nonfinite command', async scale => {
|
|
398
|
+
const t = setup(true);
|
|
399
|
+
t.platform.options.deviceOverrides[0].schema[0] = {
|
|
400
|
+
...schemaOverride, property: { ...schemaOverride.property, scale },
|
|
401
|
+
};
|
|
402
|
+
t.handler.configureTargetTemp();
|
|
403
|
+
expect(() => t.target.getHandler()).toThrow(MockHapStatusError);
|
|
404
|
+
await expect(t.target.setHandler(25)).rejects.toBeInstanceOf(MockHapStatusError);
|
|
405
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test('Fahrenheit range metadata is converted to Celsius without a manual schema', async () => {
|
|
409
|
+
const t = setup();
|
|
410
|
+
t.device.schema.find(s => s.code === 'temp_set')!.property = {
|
|
411
|
+
min: 500, max: 1040, scale: 1, step: 1, unit: '°F',
|
|
412
|
+
};
|
|
413
|
+
t.handler.configureTargetTemp();
|
|
414
|
+
expect(t.target.props).toEqual({ minValue: 10, maxValue: 38, minStep: 0.1 });
|
|
415
|
+
expect(t.target.getHandler()).toBeCloseTo((80 - 32) * 5 / 9);
|
|
416
|
+
await t.target.setHandler(27.3);
|
|
417
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 811 }]);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test.each([deviceID, 'global', 'm2dkohai78cwr3yd'])('existing %s conversion override applies once', async id => {
|
|
421
|
+
const t = setup(true, 'm2dkohai78cwr3yd', id);
|
|
422
|
+
expect(t.target.getHandler()).toBeCloseTo((80 - 32) * 5 / 9);
|
|
423
|
+
await t.target.setHandler(26.69);
|
|
424
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 800 }]);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test.each(['onGet', 'onSet'])('a one-direction %s override leaves the native other direction working', async direction => {
|
|
428
|
+
const t = setup(true);
|
|
429
|
+
const config = t.platform.options.deviceOverrides[0].schema[0];
|
|
430
|
+
if (direction === 'onGet') { delete config.onSet; } else { delete config.onGet; }
|
|
431
|
+
t.handler.configureTargetTemp();
|
|
432
|
+
expect(t.target.getHandler()).toBeCloseTo((80 - 32) * 5 / 9);
|
|
433
|
+
await t.target.setHandler(27.3);
|
|
434
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 811 }]);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test('removing an accessory disposes polling before unregistering without commands', async () => {
|
|
438
|
+
const t = setup();
|
|
439
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
440
|
+
const unregister = jest.fn();
|
|
441
|
+
TuyaPlatform.prototype.removeAccessory.call({
|
|
442
|
+
getAccessoryHandler: () => t.handler, accessoryHandlers: [t.handler],
|
|
443
|
+
api: { unregisterPlatformAccessories: unregister }, log: { info: jest.fn() },
|
|
444
|
+
} as any, deviceID);
|
|
445
|
+
await jest.advanceTimersByTimeAsync(900000);
|
|
446
|
+
expect(t.shadow).toHaveBeenCalledTimes(1);
|
|
447
|
+
expect(unregister).toHaveBeenCalledTimes(1);
|
|
448
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
test.each([42, {}, null, 'kelvin'])('malformed unit metadata %s makes only target unavailable', async unit => {
|
|
452
|
+
const t = setup();
|
|
453
|
+
(t.device.schema.find(s => s.code === 'temp_set')!.property as any).unit = unit;
|
|
454
|
+
expect(() => t.handler.configureTargetTemp()).not.toThrow();
|
|
455
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
456
|
+
expect(t.current.getHandler()).toBeCloseTo(27.7777778);
|
|
457
|
+
expect(() => t.target.getHandler()).toThrow(MockHapStatusError);
|
|
458
|
+
await expect(t.target.setHandler(25)).rejects.toBeInstanceOf(MockHapStatusError);
|
|
459
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('explicit native-unit conversion handlers honor whitespace-normalized Fahrenheit metadata', async () => {
|
|
463
|
+
const t = setup(true);
|
|
464
|
+
t.platform.options.deviceOverrides[0].schema[0] = {
|
|
465
|
+
code: 'temp_set', property: { min: 500, max: 1040, scale: 1, step: 1, unit: ' °F ' },
|
|
466
|
+
onGet: 'value', onSet: 'Math.round(value)',
|
|
467
|
+
};
|
|
468
|
+
t.handler.configureTargetTemp();
|
|
469
|
+
expect(t.target.getHandler()).toBeCloseTo((80 - 32) * 5 / 9);
|
|
470
|
+
await t.target.setHandler(27.3);
|
|
471
|
+
expect(t.commands).toHaveBeenLastCalledWith(deviceID, [{ code: 'temp_set', value: 811 }]);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test('cache expiry publishes unavailable without replacing it with setpoint; dispose cancels polling', async () => {
|
|
475
|
+
const t = setup();
|
|
476
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
477
|
+
t.shadow.mockRejectedValue(new Error('mock timeout'));
|
|
478
|
+
await jest.advanceTimersByTimeAsync(900000);
|
|
479
|
+
expect(() => t.current.getHandler()).toThrow(MockHapStatusError);
|
|
480
|
+
expect(t.current.value).toBeInstanceOf(MockHapStatusError);
|
|
481
|
+
expect(t.state.value).toBeInstanceOf(MockHapStatusError);
|
|
482
|
+
expect(() => t.state.getHandler()).toThrow(MockHapStatusError);
|
|
483
|
+
const calls = t.shadow.mock.calls.length;
|
|
484
|
+
t.handler.dispose();
|
|
485
|
+
await jest.advanceTimersByTimeAsync(900000);
|
|
486
|
+
expect(t.shadow).toHaveBeenCalledTimes(calls);
|
|
487
|
+
expect(t.commands).not.toHaveBeenCalled();
|
|
488
|
+
});
|
|
489
|
+
});
|