import fetch from 'node-fetch'; import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge'; import { PLATFORM_NAME, PLUGIN_NAME } from './settings'; import { RadiantCoolingZoneAccessory } from './platformAccessory'; import { URLSearchParams } from 'url'; /** * HomebridgePlatform * This class is the main constructor for your plugin, this is where you should * parse the user config and discover/register accessories with Homebridge. */ export class RadiantCoolingPlatformPlugin implements DynamicPlatformPlugin { public readonly Service: typeof Service = this.api.hap.Service; public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic; // this is used to track restored cached accessories public readonly accessories: PlatformAccessory[] = []; private messanaRequestHeaders; constructor( public readonly log: Logger, public readonly config: PlatformConfig, public readonly api: API, ) { if (this.config.bearer === undefined) { this.log.error('Plugin could not be initialized. Enter Bearer for Messana api in plugin configuration'); return; } this.messanaRequestHeaders = { 'authorization': 'Bearer ' + this.config.bearer, }; // When this event is fired it means Homebridge has restored all cached accessories from disk. // Dynamic Platform plugins should only register new accessories after this event was fired, // in order to ensure they weren't added to homebridge already. This event can also be used // to start discovery of new accessories. this.api.on('didFinishLaunching', () => { log.debug('Executed didFinishLaunching callback'); // run the method to discover / register your devices as accessories this.discoverDevices(); }); } messanaApiFetch(urlPath: string) { return fetch('http://'+ this.config.messanaHostNameOrIP + '/api/' + urlPath, { 'headers': this.messanaRequestHeaders, }, ).then(resp => resp.json()); } messanaApiPut(urlPath: string, params) { fetch('http://'+ this.config.messanaHostNameOrIP + '/api/' + urlPath, { method: 'PUT', 'headers': this.messanaRequestHeaders, body: new URLSearchParams(params), }); } /** * This function is invoked when homebridge restores cached accessories from disk at startup. * It should be used to setup event handlers for characteristics and update respective values. */ configureAccessory(accessory: PlatformAccessory) { this.log.info('Loading accessory from cache:', accessory.displayName); // add the restored accessory to the accessories cache so we can track if it has already been registered this.accessories.push(accessory); } /** * This is an example method showing how to register discovered accessories. * Accessories must only be registered once, previously created accessories * must not be registered again to prevent "duplicate UUID" errors. */ discoverDevices() { this.messanaApiFetch('system/zoneCount') .then(json => { const numZones = json['count']; this.log.info('Found %s zones', numZones); for (let i = 0; i < numZones; i++) { // generate a unique id for the accessory this should be generated from // something globally unique, but constant, for example, the device serial // number or MAC address const uuid = this.api.hap.uuid.generate('radiantcooling-' + i); this.log.debug('uuid: ', uuid); // see if an accessory with the same uuid has already been registered and restored from // the cached devices we stored in the `configureAccessory` method above const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); if (existingAccessory) { // the accessory already exists this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); // create the accessory handler for the restored accessory // this is imported from `platformAccessory.ts` // new RadiantCoolingZoneAccessory(this, existingAccessory); // it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.: // remove platform accessories when no longer present this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]); this.log.info('Removing existing accessory from cache:', existingAccessory.displayName); } // the accessory does not yet exist, so we need to create it this.messanaApiFetch('zone/name/' + i) .then(json => { const name = json['name']; this.log.info('Adding new accessory:', name); // create a new accessory const accessory = new this.api.platformAccessory(name, uuid); // create the accessory handler for the newly create accessory // this is imported from `platformAccessory.ts` new RadiantCoolingZoneAccessory(this, accessory, i); // link the accessory to your platform this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); }); } }); } }