homebridge-ecobee-status
Version:
Homebridge plugin to control Ecobee thermostat Home/Away/Sleep status through HomeKit security system interface
99 lines • 5.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EcobeeAPIPlatform = void 0;
const settings_1 = require("./settings");
const awaySwitchAccessory_1 = require("./awaySwitchAccessory");
const automationSwitchAccessory_1 = require("./automationSwitchAccessory");
const auth_token_refresh_1 = require("./auth-token-refresh");
/**
* EcobeeAPIPlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
class EcobeeAPIPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
this.accessories = [];
this.log.debug('Finished initializing platform:', this.config.name);
auth_token_refresh_1.AuthTokenManager.configureForPlatform(this);
// 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', async () => {
this.log.debug('Executed didFinishLaunching callback');
// load access token
try {
await auth_token_refresh_1.AuthTokenManager.getInstance().renewAuthToken();
// run the method to discover / register your devices as accessories
this.loadControlSwitches();
}
catch (error) {
this.log.error('Error during startup:', error);
}
});
}
/**
* 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) {
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.
*/
loadControlSwitches() {
// First, handle the main security system accessory
const mainDevice = {
uniqueId: 'away',
displayName: 'Ecobee Status',
};
const mainUuid = this.api.hap.uuid.generate(mainDevice.uniqueId);
const existingMainAccessory = this.accessories.find(accessory => accessory.UUID === mainUuid);
let mainAccessory;
if (existingMainAccessory) {
this.log.info('Restoring existing accessory from cache:', existingMainAccessory.displayName);
mainAccessory = existingMainAccessory;
new awaySwitchAccessory_1.AwaySwitchAccessory(this, existingMainAccessory);
}
else {
this.log.info('Adding new accessory:', mainDevice.displayName);
mainAccessory = new this.api.platformAccessory(mainDevice.displayName, mainUuid);
mainAccessory.context.device = mainDevice;
new awaySwitchAccessory_1.AwaySwitchAccessory(this, mainAccessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [mainAccessory]);
}
// Now handle the automation switch if enabled
if (this.config.enableAutomationSwitch) {
const automationDevice = {
uniqueId: 'automation-control',
displayName: 'Ecobee Away',
};
const automationUuid = this.api.hap.uuid.generate(automationDevice.uniqueId);
const existingAutomationAccessory = this.accessories.find(accessory => accessory.UUID === automationUuid);
if (existingAutomationAccessory) {
this.log.info('Restoring existing automation accessory from cache:', existingAutomationAccessory.displayName);
new automationSwitchAccessory_1.AutomationSwitchAccessory(this, existingAutomationAccessory, mainAccessory);
}
else {
this.log.info('Adding new automation accessory:', automationDevice.displayName);
const automationAccessory = new this.api.platformAccessory(automationDevice.displayName, automationUuid);
automationAccessory.context.device = automationDevice;
new automationSwitchAccessory_1.AutomationSwitchAccessory(this, automationAccessory, mainAccessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [automationAccessory]);
}
}
}
}
exports.EcobeeAPIPlatform = EcobeeAPIPlatform;
//# sourceMappingURL=platform.js.map