homebridge-eightsleepthermostat
Version:
Homebridge thermostat accessory for the Eight Sleep Pod smart bed.
115 lines • 6.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EightSleepThermostatPlatform = void 0;
const settings_1 = require("./settings");
const platformAccessory_1 = require("./platformAccessory");
const eightSleepConnection_1 = require("./eightSleepConnection");
const clientAdapter_1 = require("./clientAdapter");
const pluginDisplayName = 'Eight Sleep Thermostat';
class EightSleepThermostatPlatform {
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;
// track restored cached accessories
this.accessories = [];
if (this.config['email'] && this.config['password']) {
this.connection = new eightSleepConnection_1.EightSleepConnection(this, this.config['email'], this.config['password']);
this.api.on('didFinishLaunching', () => {
this.discoverDevices().catch((error) => {
this.log.error('Something went wrong...', error);
});
});
}
else {
const configError = new Error('You need to specify your Eight Sleep account credentials (email & password). Either manually update ' +
'the \'config.json\' file, or from your Homebridge dashboard -> navigate to the \'Plugins\' tab -> find ' +
`'${pluginDisplayName}' in the list of installed plugins, & click 'SETTINGS' to complete account setup.`);
this.log.error('Unable to setup plugin:', configError.message);
}
}
/**
* REQUIRED - Homebridge will call "configureAccessory" method once for each restored cached accessory
*/
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add restored accessory to the local cache to track if its already been registered
this.accessories.push(accessory);
}
async discoverDevices() {
var _a, _b;
const [primaryUserDevice, session] = [await ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.primaryUserDevice), await ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.session)];
if (!this.connection || !primaryUserDevice || !session) {
throw new Error('Unexpected failure occured during plugin load.');
}
const sharedPlatformClient = new clientAdapter_1.PlatformClientAdapter(primaryUserDevice.id, this.log);
const soloBedName = this.config['solo-bed-name'];
const leftBedName = this.config['left-bed-name'];
const rightBedName = this.config['right-bed-name'];
let eightSleepDevices;
if (primaryUserDevice.side === 'solo') {
eightSleepDevices = [
{
accessoryUUID: `${primaryUserDevice.id}:SOLO`,
sharedDeviceId: primaryUserDevice.id,
pluginSerial: primaryUserDevice.id.substring(0, 12).concat(':Solo'),
isOwner: true,
side: 'solo',
displayName: soloBedName !== null && soloBedName !== void 0 ? soloBedName : 'Pod Pro Solo',
},
];
}
else {
eightSleepDevices = [
{
accessoryUUID: `${primaryUserDevice.id}:LEFT`,
sharedDeviceId: primaryUserDevice.id,
pluginSerial: primaryUserDevice.id.substring(0, 12).concat(':Left'),
isOwner: primaryUserDevice.side === 'left' ? true : false,
side: 'left',
displayName: leftBedName !== null && leftBedName !== void 0 ? leftBedName : 'Pod Pro Left',
},
{
accessoryUUID: `${primaryUserDevice.id}:RIGHT`,
sharedDeviceId: primaryUserDevice.id,
pluginSerial: primaryUserDevice.id.substring(0, 12).concat(':Right'),
isOwner: primaryUserDevice.side === 'right' ? true : false,
side: 'right',
displayName: rightBedName !== null && rightBedName !== void 0 ? rightBedName : 'Pod Pro Right',
},
];
}
for (const device of eightSleepDevices) {
// TODO #1 -> refer to 'platform.ts' Craft document
const uuid = this.api.hap.uuid.generate(device.accessoryUUID);
// 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);
const guestId = `guest-${device.sharedDeviceId}-${device.side}`;
if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
existingAccessory.context.device = device;
existingAccessory.context.device.userId = device.isOwner ? session.userId : guestId;
this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
new platformAccessory_1.EightSleepThermostatAccessory(this, existingAccessory, sharedPlatformClient);
}
else {
this.log.info('Adding new accessory:', device.displayName);
const accessory = new this.api.platformAccessory(device.displayName, uuid);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = device;
accessory.context.device.userId = device.isOwner ? session.userId : guestId;
new platformAccessory_1.EightSleepThermostatAccessory(this, accessory, sharedPlatformClient);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
}
}
exports.EightSleepThermostatPlatform = EightSleepThermostatPlatform;
//# sourceMappingURL=platform.js.map