homebridge-sleeptracker
Version:
Homebridge plugin for SleepTracker smart beds - Control your bed's position and features through HomeKit
80 lines • 3.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SleepTrackerPlatform = exports.PLUGIN_NAME = exports.PLATFORM_NAME = void 0;
const sleepTrackerAccessory_1 = require("./sleepTrackerAccessory");
exports.PLATFORM_NAME = 'SleepTrackerPlatform';
exports.PLUGIN_NAME = 'homebridge-sleeptracker';
class SleepTrackerPlatform {
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.accessories = [];
// Validate configuration
if (!this.validateConfig()) {
return;
}
this.api.on('didFinishLaunching', () => {
this.discoverDevices();
});
}
validateConfig() {
if (!this.config.username) {
this.log.error('Missing username in configuration');
return false;
}
if (!this.config.password) {
this.log.error('Missing password in configuration');
return false;
}
if (!this.config.deviceId) {
this.log.error('Missing deviceId in configuration');
return false;
}
if (typeof this.config.username !== 'string' || this.config.username.length === 0) {
this.log.error('Invalid username in configuration');
return false;
}
if (typeof this.config.password !== 'string' || this.config.password.length < 8) {
this.log.error('Invalid password in configuration (minimum 8 characters)');
return false;
}
if (typeof this.config.deviceId !== 'string' || this.config.deviceId.length === 0) {
this.log.error('Invalid deviceId in configuration');
return false;
}
return true;
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
new sleepTrackerAccessory_1.SleepTrackerAccessory(this, accessory);
this.accessories.push(accessory);
}
discoverDevices() {
const deviceId = this.config.deviceId;
if (!deviceId) {
this.log.error('No device ID configured');
return;
}
const uuid = this.api.hap.uuid.generate(deviceId);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new sleepTrackerAccessory_1.SleepTrackerAccessory(this, existingAccessory);
}
else {
this.log.info('Adding new accessory');
const accessory = new this.api.platformAccessory('SleepTracker Bed', uuid);
accessory.context.device = {
id: deviceId,
name: this.config.name || 'SleepTracker Bed'
};
new sleepTrackerAccessory_1.SleepTrackerAccessory(this, accessory);
this.api.registerPlatformAccessories(exports.PLUGIN_NAME, exports.PLATFORM_NAME, [accessory]);
}
}
}
exports.SleepTrackerPlatform = SleepTrackerPlatform;
//# sourceMappingURL=platform.js.map