UNPKG

homebridge-gsh

Version:
109 lines 6.56 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 }); exports.Lightbulb = void 0; const hap_types_1 = require("../hap-types"); const ghToHapTypes_1 = require("./ghToHapTypes"); class Lightbulb extends ghToHapTypes_1.ghToHap { sync(service) { const attributes = {}; const traits = [ 'action.devices.traits.OnOff', ]; if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Brightness)) { traits.push('action.devices.traits.Brightness'); } if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Hue)) { traits.push('action.devices.traits.ColorSetting'); attributes.colorModel = 'hsv'; attributes.colorTemp; } if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature)) { traits.push('action.devices.traits.ColorSetting'); attributes.colorTemperatureRange = { temperatureMinK: 2000, temperatureMaxK: 6000, }; attributes.commandOnlyColorSetting = false; } return this.createSyncData(service, { type: 'action.devices.types.LIGHT', traits, attributes, }); } query(service) { const response = { on: !!service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.On).value, online: true, }; if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Brightness)) { response.brightness = service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Brightness).value; } if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Hue)) { response.color = { spectrumHsv: { hue: service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Hue).value, saturation: Number(service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Saturation).value) / 100, value: 1, }, }; } if (service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature)) { const min = service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).minValue; const max = service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).maxValue; const value = (Number(max) - Number(min)) - (Number(service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).value) - Number(min)) + Number(min); response.color = { temperatureK: 2000 + (6000 - 2000) * ((value - min) / (max - min)), }; } return response; } execute(service, command) { return __awaiter(this, void 0, void 0, function* () { if (!command.execution.length) { return { ids: [service.uniqueId], status: 'ERROR', debugString: 'missing command' }; } switch (command.execution[0].command) { case ('action.devices.commands.OnOff'): { yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.On).setValue(command.execution[0].params.on); return { ids: [service.uniqueId], status: 'SUCCESS' }; } case ('action.devices.commands.BrightnessAbsolute'): { yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.On).setValue(command.execution[0].params.on); yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Brightness).setValue(command.execution[0].params.brightness); return { ids: [service.uniqueId], status: 'SUCCESS' }; } case ('action.devices.commands.ColorAbsolute'): { if (command.execution[0].params.color.spectrumHSV) { yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Hue).setValue(command.execution[0].params.color.spectrumHSV.hue); yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.Saturation).setValue(command.execution[0].params.color.spectrumHSV.saturation * 100); return { ids: [service.uniqueId], status: 'SUCCESS' }; } if (command.execution[0].params.color.temperature) { const min = service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).minValue; const max = service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).maxValue; const value = command.execution[0].params.color.temperature; const hbAccessoryValue = min + (max - min) * ((value - 2000) / (6000 - 2000)); yield service.serviceCharacteristics.find(x => x.uuid === hap_types_1.Characteristic.ColorTemperature).setValue((max - min) - (hbAccessoryValue - min) + min); return { ids: [service.uniqueId], status: 'SUCCESS' }; } break; } default: { return { ids: [service.uniqueId], status: 'ERROR', debugString: `unknown command ${command.execution[0].command}` }; } } }); } } exports.Lightbulb = Lightbulb; //# sourceMappingURL=lightbulb.js.map