UNPKG

homebridge-hatch-baby-rest

Version:
114 lines (113 loc) 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HatchBabyRestPlatform = exports.platformName = exports.pluginName = void 0; const api_1 = require("./api"); const hap_1 = require("../shared/hap"); const util_1 = require("../shared/util"); const light_and_sound_machine_1 = require("../shared/light-and-sound-machine"); const sound_machine_1 = require("../shared/sound-machine"); const restore_accessory_1 = require("./restore-accessory"); const rest_iot_1 = require("./rest-iot"); const restore_1 = require("./restore"); exports.pluginName = 'homebridge-hatch-baby-rest'; exports.platformName = 'HatchBabyRest'; class HatchBabyRestPlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.homebridgeAccessories = {}; (0, 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; } async connectToApi() { const hatchBabyApi = this.config.email && this.config.password ? new api_1.HatchBabyApi(this.config) : undefined, { restPluses, restMinis, restores, restoreIots, restIots, restIotPluses, } = hatchBabyApi ? await hatchBabyApi.getDevices() : { restPluses: [], restMinis: [], restores: [], restIots: [], restIotPluses: [], restoreIots: [], }, { api } = this, cachedAccessoryIds = Object.keys(this.homebridgeAccessories), platformAccessories = [], activeAccessoryIds = [], debugPrefix = hap_1.isTestHomebridge ? 'TEST ' : '', devices = [ ...restPluses, ...restMinis, ...restIots, ...restIotPluses, ...restores, ...restoreIots, ]; this.log.info('Configuring Hatch Devices:'); if (devices.length) { const countByModel = devices.reduce((acc, device) => { const model = device.model; acc[model] = (acc[model] || 0) + 1; return acc; }, {}); Object.entries(countByModel).forEach(([model, count]) => { this.log.info(` ${model}: ${count}`); }); } else { this.log.info(' No supported devices found'); } devices.forEach((device) => { const id = device.id, uuid = hap_1.hap.uuid.generate(debugPrefix + id), displayName = debugPrefix + device.name, createHomebridgeAccessory = () => { const accessory = new api.platformAccessory(displayName, uuid, 5 /* hap.Categories.LIGHTBULB */); this.log.info(`Adding new Hatch ${device.model} - ${displayName}`); platformAccessories.push(accessory); return accessory; }, homebridgeAccessory = this.homebridgeAccessories[uuid] || createHomebridgeAccessory(); if (device instanceof restore_1.Restore || device instanceof rest_iot_1.RestIot) { new restore_accessory_1.RestoreAccessory(device, homebridgeAccessory); } else if ('onBrightness' in device) { new light_and_sound_machine_1.LightAndSoundMachineAccessory(device, homebridgeAccessory); } else { new sound_machine_1.SoundMachineAccessory(device, homebridgeAccessory); } this.homebridgeAccessories[uuid] = homebridgeAccessory; activeAccessoryIds.push(uuid); }); if (platformAccessories.length) { api.registerPlatformAccessories(exports.pluginName, exports.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(exports.pluginName, exports.platformName, staleAccessories); } } } exports.HatchBabyRestPlatform = HatchBabyRestPlatform;