UNPKG

homebridge-gsh

Version:
465 lines 16 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 hap_1 = require("../hap"); const logger_1 = require("../logger"); const temperature_sensor_1 = require("./temperature-sensor"); 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: '123-456', }; const log = new logger_1.Log(console, true); const hap = new hap_1.Hap(socketMock, log, '031-45-154', config); const temperatureSensor = new temperature_sensor_1.TemperatureSensor(hap); describe('temperatureSensor', () => { describe('sync message', () => { it('temperatureSensor', () => __awaiter(void 0, void 0, void 0, function* () { const response = temperatureSensor.sync(temperatureSensorTemp); expect(response).toBeDefined(); expect(response.name).toBeDefined(); expect(response.name.defaultNames).toBeDefined(); expect(response.name.defaultNames).toHaveLength(2); expect(response.name.name).toBeDefined(); expect(response.name.name).not.toBe(''); expect(response.name.name).toBe('Backyard'); expect(response.name.nicknames).toBeDefined(); expect(response.type).toBe('action.devices.types.SENSOR'); expect(response.traits).toContain('action.devices.traits.TemperatureControl'); expect(response.traits).not.toContain('action.devices.traits.Brightness'); expect(response.traits).not.toContain('action.devices.traits.ColorSetting'); expect(response.attributes).toBeDefined(); expect(response.attributes.queryOnlyTemperatureControl).toBeDefined(); expect(response.attributes.temperatureUnitForUX).toBeDefined(); })); it('temperatureSensor - No Service Name', () => __awaiter(void 0, void 0, void 0, function* () { temperatureSensorTemp.serviceName = ''; const response = temperatureSensor.sync(temperatureSensorTemp); expect(response).toBeDefined(); expect(response.name).toBeDefined(); expect(response.name.defaultNames).toBeDefined(); expect(response.name.defaultNames).toHaveLength(1); expect(response.name.name).toBeDefined(); expect(response.name.name).not.toBe(''); expect(response.name.name).toBe('Backyard'); expect(response.name.nicknames).toBeDefined(); expect(response.type).toBe('action.devices.types.SENSOR'); expect(response.traits).toContain('action.devices.traits.TemperatureControl'); expect(response.traits).not.toContain('action.devices.traits.Brightness'); expect(response.traits).not.toContain('action.devices.traits.ColorSetting'); expect(response.attributes).toBeDefined(); expect(response.attributes.queryOnlyTemperatureControl).toBeDefined(); expect(response.attributes.temperatureUnitForUX).toBeDefined(); })); it('temperatureSensor - No service or Accessory Name', () => __awaiter(void 0, void 0, void 0, function* () { temperatureSensorTemp.accessoryInformation.Name = ''; const response = temperatureSensor.sync(temperatureSensorTemp); expect(response).toBeDefined(); expect(response.name).toBeDefined(); expect(response.name.defaultNames).toBeDefined(); expect(response.name.defaultNames).toHaveLength(0); expect(response.name.name).toBeDefined(); expect(response.name.name).not.toBe(''); expect(response.name.name).toBe('Missing Name'); expect(response.name.nicknames).toBeDefined(); expect(response.type).toBe('action.devices.types.SENSOR'); expect(response.traits).toContain('action.devices.traits.TemperatureControl'); expect(response.traits).not.toContain('action.devices.traits.Brightness'); expect(response.traits).not.toContain('action.devices.traits.ColorSetting'); expect(response.attributes).toBeDefined(); expect(response.attributes.queryOnlyTemperatureControl).toBeDefined(); expect(response.attributes.temperatureUnitForUX).toBeDefined(); })); }); describe('query message', () => { it('temperatureSensor ', () => __awaiter(void 0, void 0, void 0, function* () { const response = temperatureSensor.query(temperatureSensorTemp); expect(response).toBeDefined(); expect(response.temperatureSetpointCelsius).toBeDefined(); expect(response.temperatureAmbientCelsius).toBeDefined(); expect(response.online).toBeDefined(); })); }); describe('execute message', () => { it('temperatureSensor ', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield temperatureSensor.execute(temperatureSensorTemp, commandOnOff); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('ERROR'); })); it('temperatureSensor - commandMalformed', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield temperatureSensor.execute(temperatureSensorTemp, commandMalformed); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('ERROR'); })); it('temperatureSensor - commandIncorrectCommand', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield temperatureSensor.execute(temperatureSensorTemp, commandIncorrectCommand); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('ERROR'); })); }); afterAll(() => __awaiter(void 0, void 0, void 0, function* () { console.log('destroy'); yield hap.destroy(); })); }); function sleep(ms) { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, ms)); }); } 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: 'TemperatureSensor', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }); }; const setValueError = function (value) { return __awaiter(this, void 0, void 0, function* () { throw new Error('Error setting value'); 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: 'TemperatureSensor', 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 temperatureSensorTemp; }); }; 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: 'TemperatureSensor', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return temperatureSensorTemp; }); }; const getCharacteristic = function () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'TemperatureSensor', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }; const temperatureSensorTemp = { 'aid': 23, 'iid': 10, 'uuid': '0000008A-0000-1000-8000-0026BB765291', 'type': 'TemperatureSensor', 'humanType': 'Temperature Sensor', 'serviceName': 'Backyard', 'serviceCharacteristics': [ { 'aid': 23, 'iid': 312, 'uuid': '00000079-0000-1000-8000-0026BB765291', 'type': 'StatusLowBattery', 'serviceType': 'TemperatureSensor', 'serviceName': '', 'description': 'Status Low Battery', 'value': 0, 'format': 'uint8', 'perms': [ 'ev', 'pr', ], 'maxValue': 1, 'minValue': 0, 'minStep': 1, 'canRead': true, 'canWrite': false, 'ev': true, }, { 'aid': 23, 'iid': 12, 'uuid': '00000011-0000-1000-8000-0026BB765291', 'type': 'CurrentTemperature', 'serviceType': 'TemperatureSensor', 'serviceName': '', 'description': 'Current Temperature', 'value': 0, 'format': 'float', 'perms': [ 'ev', 'pr', ], 'unit': 'celsius', 'maxValue': 100, 'minValue': -100, 'minStep': 0.1, 'canRead': true, 'canWrite': false, 'ev': true, }, ], 'accessoryInformation': { 'Manufacturer': 'NRCHKB', 'Model': '1.4.3', 'Name': 'Backyard', 'Serial Number': 'Default Serial Number', 'Firmware Revision': '1.4.3', 'Hardware Revision': '1.4.3', 'Software Revision': '1.4.3', }, 'values': { 'StatusLowBattery': 0, 'CurrentTemperature': 0, }, 'linked': [ 13, ], 'instance': { 'name': 'Default Model', 'username': '69:62:B7:AE:38:D4', 'ipAddress': '192.168.1.11', 'port': 51830, connectionFailedCount: 0, services: [], configurationNumber: 1, }, 'uniqueId': '49c24a777f09eddbe4579d8d9432a8f313d1d90d5c4a3ac8ff018be24469c7e2', }; 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 commandMalformed = { devices: [ { customData: { aid: 75, iid: 8, instanceIpAddress: '192.168.1.11', instancePort: 46283, instanceUsername: '1C:22:3D:E3:CF:34', }, id: 'b9245954ec41632a14076df3bbb7336f756c17ca4b040914a593e14d652d5738', }, ], execution: [], }; const commandIncorrectCommand = { 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.notACommand', 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=temperature-sensor.spec.js.map