UNPKG

homebridge-hatch-baby-rest-volume

Version:
88 lines (87 loc) 4.58 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.HatchBabyRestPlatform = void 0; const api_1 = require("./api"); const hap_1 = require("./hap"); const util_1 = require("./util"); const hatch_baby_rest_plus_volume_1 = require("./accessories/hatch-baby-rest-plus-volume"); const debug = __filename.includes('release'); process.env.HBR_DEBUG = debug ? 'true' : ''; class HatchBabyRestPlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.homebridgeAccessories = {}; util_1.useLogger({ logInfo(message) { log.info(message); }, logError(message) { log.error(message); }, }); if (!config) { this.log.info('No configuration found for platform HatchBabyRest'); return; } this.api.on('didFinishLaunching', () => { this.log.debug('didFinishLaunching'); this.connectToApi().catch((e) => { this.log.error('Error connecting to API'); this.log.error(e); }); }); this.homebridgeAccessories = {}; } configureAccessory(accessory) { this.log.info(`Configuring cached accessory ${accessory.UUID} ${accessory.displayName}`); this.log.debug('%j', accessory); this.homebridgeAccessories[accessory.UUID] = accessory; } connectToApi() { return __awaiter(this, void 0, void 0, function* () { const hatchBabyApi = new api_1.HatchBabyApi(this.config), lights = yield hatchBabyApi.getRestPlusLights(), { api } = this, cachedAccessoryIds = Object.keys(this.homebridgeAccessories), platformAccessories = [], activeAccessoryIds = [], debugPrefix = debug ? 'TEST ' : ''; if (this.config.removeAll) { this.log.info('REMOVING ALL Hatch Baby Rest+ lights. You can now stop your homebridge server and restart it without removeAll set.'); lights.length = 0; } else { this.log.info(`Configuring ${lights.length} Hatch Baby Rest+ lights`); } lights.forEach((light) => { const uuid = hap_1.hap.uuid.generate(debugPrefix + light.id + 'volume'), displayName = debugPrefix + light.name + ' Volume', createHomebridgeAccessory = () => { const accessory = new api.platformAccessory(displayName, uuid, 5 /* LIGHTBULB */); this.log.info(`Adding new Hatch Baby Rest+ - ${displayName}`); platformAccessories.push(accessory); return accessory; }, homebridgeAccessory = this.homebridgeAccessories[uuid] || createHomebridgeAccessory(); new hatch_baby_rest_plus_volume_1.HatchBabyRestPlusVolumeAccessory(light, homebridgeAccessory, this.log, this.config.audioTrack); this.homebridgeAccessories[uuid] = homebridgeAccessory; activeAccessoryIds.push(uuid); }); if (platformAccessories.length) { api.registerPlatformAccessories(hap_1.pluginName, hap_1.platformName, platformAccessories); } const staleAccessories = cachedAccessoryIds .filter((cachedId) => !activeAccessoryIds.includes(cachedId)) .map((id) => this.homebridgeAccessories[id]); staleAccessories.forEach((staleAccessory) => { this.log.info(`Removing stale cached accessory ${staleAccessory.UUID} ${staleAccessory.displayName}`); }); if (staleAccessories.length) { this.api.unregisterPlatformAccessories(hap_1.pluginName, hap_1.platformName, staleAccessories); } }); } } exports.HatchBabyRestPlatform = HatchBabyRestPlatform;