UNPKG

homebridge-gsh

Version:
851 lines 26.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const ts_expect_1 = require("ts-expect"); const hap_1 = require("./hap"); const logger_1 = require("./logger"); class socketMock { on(event, callback) { if (event === 'websocket-status') { callback('websocket-status'); } if (event === 'json') { callback({ serverMessage: 'serverMessage' }); } } sendJson(data) { console.log('sendJson', data); } } const config = { name: 'Google Smart Home', token: '1234567890', notice: 'Keep your token a secret!', debug: false, platform: 'google-smarthome', twoFactorAuthPin: '1234', }; const log = new logger_1.Log(console, true); const hap = new hap_1.Hap(socketMock, log, '031-45-154', config); (0, globals_1.describe)('hap', () => { globals_1.describe.skip('process the QUERY intent', () => { test('wait for HAP to be Ready', () => __awaiter(void 0, void 0, void 0, function* () { while (!hap.ready) { yield sleep(500); } console.log('hap ready, testing started'); }), 30000); (0, globals_1.describe)('QUERY message with delay to allow manual testing', () => { test('lightbulb with On/Off only', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.query(query); })); test('sleeping', () => __awaiter(void 0, void 0, void 0, function* () { yield sleep(5000); }), 30000); test('lightbulb with On/Off only', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.query(query); })); test('sleeping', () => __awaiter(void 0, void 0, void 0, function* () { yield sleep(5000); }), 30000); test('lightbulb with On/Off only', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.query(query); })); }); afterAll(() => __awaiter(void 0, void 0, void 0, function* () { console.log('destroy'); yield hap.destroy(); })); }); (0, globals_1.describe)('process the SYNC intent', () => { test('Wait for HAP to be Ready', () => __awaiter(void 0, void 0, void 0, function* () { while (!hap.ready) { yield sleep(500); } console.log('hap ready, testing started'); }), 30000); (0, globals_1.describe)('SYNC message with delay to allow manual testing', () => { test('lightbulb with On/Off only', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.buildSyncResponse(); (0, ts_expect_1.expectType)(response); })); }); }); (0, globals_1.describe)('execute', () => { test('Wait for HAP to be Ready', () => __awaiter(void 0, void 0, void 0, function* () { while (!hap.ready) { yield sleep(500); } console.log('hap ready, testing started'); }), 30000); test('Turn Off Light', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeLightOff]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('SUCCESS'); })); }); (0, globals_1.describe)('Garage Door 2fa and PIN Code', () => { test('Close - 2fa not Required', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeGarageClose]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('SUCCESS'); })); test('Open - 2fa Required - No Pin', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeGarageOpen]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('ERROR'); (0, globals_1.expect)(response[0].errorCode).toBe('challengeNeeded'); (0, globals_1.expect)(response[0].challengeNeeded).toBeDefined(); (0, globals_1.expect)(response[0].challengeNeeded.type).toBeDefined(); (0, globals_1.expect)(response[0].challengeNeeded.type).toBe('pinNeeded'); })); test('Open - 2fa Required - incorrect PIN', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeGarageDoorOpenWithIncorrectPin]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('ERROR'); (0, globals_1.expect)(response[0].errorCode).toBe('challengeNeeded'); (0, globals_1.expect)(response[0].challengeNeeded).toBeDefined(); (0, globals_1.expect)(response[0].challengeNeeded.type).toBeDefined(); (0, globals_1.expect)(response[0].challengeNeeded.type).toBe('pinNeeded'); })); test.skip('Open - 2fa Required - correct PIN', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeGarageDoorOpenWithCorrectPin]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('SUCCESS'); })); test('Close - 2fa not Required', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield hap.execute([executeGarageClose]); (0, globals_1.expect)(response).toBeDefined(); (0, globals_1.expect)(response).toBeInstanceOf(Array); (0, globals_1.expect)(response).toHaveLength(1); (0, globals_1.expect)(response[0].status).toBe('SUCCESS'); }), 10000); }); afterAll(() => __awaiter(void 0, void 0, void 0, function* () { yield hap.destroy(); })); }); function sleep(ms) { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, ms)); }); } const executeLightOff = { 'devices': [ { 'customData': { 'aid': 75, 'iid': 8, 'instanceIpAddress': '192.168.1.11', 'instancePort': 42909, 'instanceUsername': '1C:22:3D:E3:CF:34', }, 'id': 'c4644ccdad8201ccee9ae469f20ea3f6dc5f8338525729da5e51bbc005c00e44', }, ], 'execution': [ { 'command': 'action.devices.commands.OnOff', 'params': { 'on': false, }, }, ], }; const executeGarageOpen = { 'devices': [ { 'customData': { 'aid': 5, 'iid': 8, 'instanceIpAddress': '192.168.1.11', 'instancePort': 42909, 'instanceUsername': '1C:22:3D:E3:CF:34', }, 'id': '53d899e23044252d020ef417d472697eaea748bb9c7b3e860cda6b8b1253ab18', }, ], 'execution': [ { 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 100, }, }, ], }; const executeGarageDoorOpenWithIncorrectPin = { 'devices': [ { 'customData': { 'aid': 5, 'iid': 8, 'instanceIpAddress': '192.168.1.11', 'instancePort': 42909, 'instanceUsername': '1C:22:3D:E3:CF:34', }, 'id': '53d899e23044252d020ef417d472697eaea748bb9c7b3e860cda6b8b1253ab18', }, ], 'execution': [ { 'challenge': { 'pin': '54321', }, 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 100, }, }, ], }; const executeGarageDoorOpenWithCorrectPin = { 'devices': [ { 'customData': { 'aid': 5, 'iid': 8, 'instanceIpAddress': '192.168.1.11', 'instancePort': 42909, 'instanceUsername': '1C:22:3D:E3:CF:34', }, 'id': '53d899e23044252d020ef417d472697eaea748bb9c7b3e860cda6b8b1253ab18', }, ], 'execution': [ { 'challenge': { 'pin': '1234', }, 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 100, }, }, ], }; const executeGarageClose = { 'devices': [ { 'customData': { 'aid': 5, 'iid': 8, 'instanceIpAddress': '192.168.1.11', 'instancePort': 42909, 'instanceUsername': '1C:22:3D:E3:CF:34', }, 'id': '53d899e23044252d020ef417d472697eaea748bb9c7b3e860cda6b8b1253ab18', }, ], 'execution': [ { 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 0, }, }, ], }; const query = [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ]; const setValue = function (value) { return __awaiter(this, void 0, void 0, function* () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }); }; const getValue = function () { return __awaiter(this, void 0, void 0, function* () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }); }; const refreshCharacteristics = function () { return __awaiter(this, void 0, void 0, function* () { return hapServiceHue; }); }; const setCharacteristic = function (value) { return __awaiter(this, void 0, void 0, function* () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return hapServiceHue; }); }; const getCharacteristic = function () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }; const hapServiceHue = { aid: 58, iid: 8, uuid: '00000043-0000-1000-8000-0026BB765291', type: 'Lightbulb', humanType: 'Lightbulb', serviceName: 'Powder Shower', serviceCharacteristics: [ { aid: 58, iid: 10, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'On', value: 0, format: 'bool', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 58, iid: 11, uuid: '000000E3-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'Configured Name', value: 'Powder Shower', format: 'string', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 58, iid: 12, uuid: '00000008-0000-1000-8000-0026BB765291', type: 'Brightness', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'Brightness', value: 65, format: 'int', perms: ['ev', 'pr', 'pw'], unit: 'percentage', maxValue: 100, minValue: 0, minStep: 1, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 58, iid: 13, uuid: '00000013-0000-1000-8000-0026BB765291', type: 'Hue', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'Hue', value: 0, format: 'float', perms: ['ev', 'pr', 'pw'], unit: 'arcdegrees', maxValue: 360, minValue: 0, minStep: 1, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 58, iid: 14, uuid: '0000002F-0000-1000-8000-0026BB765291', type: 'Saturation', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'Saturation', value: 0, format: 'float', perms: ['ev', 'pr', 'pw'], unit: 'percentage', maxValue: 100, minValue: 0, minStep: 1, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 58, iid: 15, uuid: '000000CE-0000-1000-8000-0026BB765291', type: 'ColorTemperature', serviceType: 'Lightbulb', serviceName: 'Powder Shower', description: 'Color Temperature', value: 325, format: 'int', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: 500, minValue: 140, minStep: 1, canRead: true, canWrite: true, ev: true, setValue, getValue, }, ], accessoryInformation: { 'Manufacturer': 'Tasmota', 'Model': 'Tuya MCU', 'Name': 'Powder Shower', 'Serial Number': 'ED8243-jessie', 'Firmware Revision': '9.5.0tasmota', }, values: { On: 0, ConfiguredName: 'Powder Shower', Brightness: 65, Hue: 0, Saturation: 0, ColorTemperature: 325, }, linked: undefined, instance: { name: 'homebridge', username: '1C:22:3D:E3:CF:34', ipAddress: '192.168.1.11', port: 46283, connectionFailedCount: 0, services: [], configurationNumber: 1, }, uniqueId: '2a1f1a87419c2afbd847828b96095f892975c36572751ab71f53edf0c5372fdb', refreshCharacteristics, setCharacteristic, getCharacteristic, }; const hapServiceOnOff = { aid: 13, iid: 8, uuid: '00000043-0000-1000-8000-0026BB765291', type: 'Lightbulb', humanType: 'Lightbulb', serviceName: 'Shed Light', serviceCharacteristics: [ { aid: 13, iid: 10, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Shed Light', description: 'On', value: 0, format: 'bool', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 13, iid: 11, uuid: '000000E3-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'Lightbulb', serviceName: 'Shed Light', description: 'Configured Name', value: 'Shed Light', format: 'string', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, ], accessoryInformation: { 'Manufacturer': 'Tasmota', 'Model': 'WiOn', 'Name': 'Shed Light', 'Serial Number': '02231D-jessie', 'Firmware Revision': '9.5.0tasmota', }, values: { On: 0, ConfiguredName: 'Shed Light' }, linked: undefined, instance: { name: 'homebridge', username: '1C:22:3D:E3:CF:34', ipAddress: '192.168.1.11', port: 46283, connectionFailedCount: 0, services: [], configurationNumber: 1, }, uniqueId: '664195d5556f1e0b424ed32bcd863ec8954c76f8ab81cc399f0e24f8827806d1', refreshCharacteristics, setCharacteristic, getCharacteristic, }; const hapServiceDimmer = { aid: 14, iid: 8, uuid: '00000043-0000-1000-8000-0026BB765291', type: 'Lightbulb', humanType: 'Lightbulb', serviceName: 'Front Hall', serviceCharacteristics: [ { aid: 14, iid: 10, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Lightbulb', serviceName: 'Front Hall', description: 'On', value: 0, format: 'bool', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 14, iid: 11, uuid: '00000008-0000-1000-8000-0026BB765291', type: 'Brightness', serviceType: 'Lightbulb', serviceName: 'Front Hall', description: 'Brightness', value: 100, format: 'int', perms: ['ev', 'pr', 'pw'], unit: 'percentage', maxValue: 100, minValue: 0, minStep: 1, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 14, iid: 12, uuid: '000000E3-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'Lightbulb', serviceName: 'Front Hall', description: 'Configured Name', value: 'Front Hall', format: 'string', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, ], accessoryInformation: { 'Manufacturer': 'Tasmota', 'Model': 'Tuya MCU', 'Name': 'Front Hall', 'Serial Number': '23CAC5-jessie', 'Firmware Revision': '9.5.0tasmota', }, values: { On: 0, Brightness: 100, ConfiguredName: 'Front Hall' }, linked: undefined, instance: { name: 'homebridge', username: '1C:22:3D:E3:CF:34', ipAddress: '192.168.1.11', port: 46283, connectionFailedCount: 0, services: [], configurationNumber: 1, }, uniqueId: '028fc478c0b4b116ead9be0dc8a72251b351b745cbc3961704268737101c803d', refreshCharacteristics, setCharacteristic, getCharacteristic, }; const commandOpen = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 100, }, }, ], }; const commandClose = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { 'command': 'action.devices.commands.OpenClose', 'params': { 'followUpToken': '1234567890', 'openPercent': 0, }, }, ], }; const commandOnOff = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { command: 'action.devices.commands.OnOff', params: { on: true, }, }, ], }; const commandBrightness = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { command: 'action.devices.commands.OnOff', params: { on: true, }, }, ], }; const commandColorHSV = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { command: 'action.devices.commands.OnOff', params: { on: true, }, }, ], }; const commandColorTemperature = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [ { command: 'action.devices.commands.OnOff', params: { on: true, }, }, ], }; //# sourceMappingURL=hap.spec.js.map