UNPKG

homebridge-gsh

Version:
853 lines 27.5 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 hap_types_1 = require("../hap-types"); const logger_1 = require("../logger"); const thermostat_1 = require("./thermostat"); 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 thermostat = new thermostat_1.Thermostat(hap); describe('thermostat', () => { describe('sync message', () => { it('thermostat heat and cool', () => __awaiter(void 0, void 0, void 0, function* () { const response = thermostat.sync(thermostatTemp); expect(response).toBeDefined(); expect(response.type).toBe('action.devices.types.THERMOSTAT'); expect(response.traits).toContain('action.devices.traits.TemperatureSetting'); 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.availableThermostatModes).toBeDefined(); expect(response.attributes.availableThermostatModes).toContain('off'); expect(response.attributes.availableThermostatModes).toContain('heat'); expect(response.attributes.availableThermostatModes).toContain('cool'); expect(response.attributes.availableThermostatModes).toContain('heatcool'); expect(response.attributes.availableThermostatModes).not.toContain('auto'); expect(response.attributes.thermostatTemperatureUnit).toBeDefined(); })); it('thermostat cool only', () => __awaiter(void 0, void 0, void 0, function* () { const response = thermostat.sync(thermostatNoHeat); expect(response).toBeDefined(); expect(response.type).toBe('action.devices.types.THERMOSTAT'); expect(response.traits).toContain('action.devices.traits.TemperatureSetting'); 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.availableThermostatModes).toBeDefined(); expect(response.attributes.availableThermostatModes).toContain('off'); expect(response.attributes.availableThermostatModes).toContain('heat'); expect(response.attributes.availableThermostatModes).toContain('cool'); expect(response.attributes.availableThermostatModes).not.toContain('heatcool'); expect(response.attributes.availableThermostatModes).toContain('auto'); expect(response.attributes.thermostatTemperatureUnit).toBeDefined(); })); }); describe('query message', () => { it('thermostat heat and cool', () => __awaiter(void 0, void 0, void 0, function* () { const response = thermostat.query(thermostatTemp); expect(response).toBeDefined(); expect(response.online).toBeDefined(); expect(response.thermostatMode).toBeDefined(); expect(response.thermostatTemperatureSetpoint).toBeDefined(); expect(response.thermostatTemperatureAmbient).toBeDefined(); })); it('thermostat cool only', () => __awaiter(void 0, void 0, void 0, function* () { const response = thermostat.query(thermostatNoHeat); expect(response).toBeDefined(); expect(response.online).toBeDefined(); expect(response.thermostatMode).toBeDefined(); expect(response.thermostatTemperatureAmbient).toBeDefined(); })); }); describe('execute message', () => { it('thermostat - setMode', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield thermostat.execute(thermostatTemp, commandThermostatSetModeOff); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('SUCCESS'); })); it('thermostat - setTemp', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield thermostat.execute(thermostatTemp, commandThermostatTemperatureSetpoint); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('SUCCESS'); })); it('thermostat - setRange', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield thermostat.execute(thermostatTemp, commandThermostatTemperatureSetRange); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('SUCCESS'); })); it('thermostat - commandMalformed', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield thermostat.execute(thermostatTemp, commandMalformed); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('ERROR'); })); it('thermostat - commandIncorrectCommand', () => __awaiter(void 0, void 0, void 0, function* () { const response = yield thermostat.execute(thermostatTemp, commandIncorrectCommand); expect(response).toBeDefined(); expect(response.ids).toBeDefined(); expect(response.status).toBe('ERROR'); })); it('thermostat - Error', () => __awaiter(void 0, void 0, void 0, function* () { expect.assertions(1); thermostatTemp.serviceCharacteristics[0].setValue = setValueError; expect(thermostat.execute(thermostatTemp, commandThermostatSetModeOff)).rejects.toThrow('Error setting value'); })); }); 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 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: 'Thermostat', 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: 'Thermostat', 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 thermostatTemp; }); }; 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: 'Thermostat', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return thermostatTemp; }); }; const getCharacteristic = function () { const result = { aid: 1, iid: 1, uuid: '00000025-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'Thermostat', serviceName: 'Trailer Step', description: 'On', value: 0, format: 'bool', perms: [ 'ev', 'pr', 'pw', ], canRead: true, canWrite: true, ev: true, }; return result; }; const thermostatTemp = { aid: 13, iid: 8, uuid: '00000043-0000-1000-8000-0026BB765291', type: 'Thermostat', humanType: 'Thermostat', serviceName: 'Shed Light', serviceCharacteristics: [ { aid: 13, iid: 11, uuid: hap_types_1.Characteristic.TargetHeaterCoolerState, type: 'TargetHeaterCoolerState', serviceType: 'Active', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: '000000B0-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'Active', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', perms: ['ev', 'pr', 'pw'], unit: undefined, maxValue: undefined, minValue: undefined, minStep: undefined, canRead: true, canWrite: true, ev: true, setValue, getValue, }, { aid: 13, iid: 10, uuid: '00000011-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'CurrentTemperature', serviceName: 'Shed Light', description: 'On', value: 25, 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: 10, uuid: '00000035-0000-1000-8000-0026BB765291', type: 'TargetTemperature', serviceType: 'TargetTemperature', serviceName: 'Shed Light', description: 'On', value: 25, 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: '00000012-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'HeatingThresholdTemperature', serviceName: 'Shed Light', description: 'Configured Name', value: 19, format: 'string', 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: '00000036-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'TemperatureDisplayUnits', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: '000000B2-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'TargetThermostatState', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: '0000000D-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'CoolingThresholdTemperature', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: hap_types_1.Characteristic.TargetHeatingCoolingState, type: 'TargetHeatingCoolingState', serviceType: 'TargetHeatingCoolingState', serviceName: 'Shed Light', description: 'Configured Name', value: 1, 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 thermostatNoHeat = { aid: 13, iid: 8, uuid: '00000043-0000-1000-8000-0026BB765291', type: 'Thermostat', humanType: 'Thermostat', serviceName: 'Shed Light', serviceCharacteristics: [ { aid: 13, iid: 10, uuid: '00000011-0000-1000-8000-0026BB765291', type: 'On', serviceType: 'CurrentTemperature', serviceName: 'Shed Light', description: 'On', value: 25, 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: '00000036-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'TemperatureDisplayUnits', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: '000000B2-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'TargetThermostatState', serviceName: 'Shed Light', description: 'Configured Name', value: 2, format: 'string', 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: '0000000D-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'CoolingThresholdTemperature', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: '000000B0-0000-1000-8000-0026BB765291', type: 'ConfiguredName', serviceType: 'Active', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: hap_types_1.Characteristic.TargetHeatingCoolingState, type: 'TargetHeatingCoolingState', serviceType: 'TargetHeatingCoolingState', serviceName: 'Shed Light', description: 'Configured Name', value: 1, format: 'string', 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: hap_types_1.Characteristic.TargetTemperature, type: 'TargetTemperature', serviceType: 'TargetTemperature', serviceName: 'Shed Light', description: 'Configured Name', value: 1, 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 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, }, }, ], }; const commandThermostatSetModeOff = { 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.ThermostatSetMode', params: { thermostatMode: 'off', }, }, ], }; const commandThermostatTemperatureSetpoint = { 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.ThermostatSetMode', params: { thermostatMode: 'off', }, }, ], }; const commandThermostatTemperatureSetRange = { 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.ThermostatSetMode', params: { thermostatMode: 'off', }, }, ], }; //# sourceMappingURL=thermostat.spec.js.map