homebridge-smartthings-ac
Version:
Control your Samsung SmartThings AC using Homebridge.
90 lines • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartThingsPlatform = void 0;
const settings_1 = require("./settings");
const platformAccessory_1 = require("./platformAccessory");
const core_sdk_1 = require("@smartthings/core-sdk");
const deviceAdapter_1 = require("./deviceAdapter");
class SmartThingsPlatform {
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 = [];
const token = this.config.token;
this.client = new core_sdk_1.SmartThingsClient(new core_sdk_1.BearerTokenAuthenticator(token));
if (token === null || token === void 0 ? void 0 : token.trim()) {
this.log.debug('Loading devices with token:', token);
this.api.on('didFinishLaunching', () => {
this.client.devices.list()
.then((devices) => this.handleDevices(devices))
.catch(err => log.error('Cannot load devices', err));
});
}
else {
this.log.warn('Please congigure your API token and restart homebridge.');
}
}
handleDevices(devices) {
for (const device of devices) {
if (device.components) {
const capabilities = this.getCapabilities(device);
const missingCapabilities = this.getMissingCapabilities(capabilities);
if (device.deviceId && missingCapabilities.length === 0) {
this.log.info('Registering device', device.deviceId);
this.handleSupportedDevice(device);
}
else {
this.log.info('Skipping device', device.deviceId, device.label, 'Missing capabilities', missingCapabilities);
}
}
}
}
getMissingCapabilities(capabilities) {
return platformAccessory_1.SmartThingsAirConditionerAccessory.requiredCapabilities
.filter((el) => !capabilities.includes(el));
}
handleSupportedDevice(device) {
const existingAccessory = this.accessories.find(accessory => accessory.UUID === device.deviceId);
if (existingAccessory) {
this.handleExistingDevice(device, existingAccessory);
}
else {
this.handleNewDevice(device);
}
}
getCapabilities(device) {
var _a, _b;
return (_b = (_a = device.components) === null || _a === void 0 ? void 0 : _a.flatMap((component) => component.capabilities).map((capabilityReference) => capabilityReference.id)) !== null && _b !== void 0 ? _b : [];
}
handleExistingDevice(device, accessory) {
this.log.info('Restoring existing accessory from cache:', device.label);
this.createSmartThingsAccessory(accessory, device);
}
handleNewDevice(device) {
this.log.info('Adding new accessory:', device.label);
const accessory = this.createPlatformAccessory(device);
this.createSmartThingsAccessory(accessory, device);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
createPlatformAccessory(device) {
if (device.label && device.deviceId) {
const accessory = new this.api.platformAccessory(device.label, device.deviceId);
accessory.context.device = device;
return accessory;
}
throw new Error('Missing label and id.');
}
createSmartThingsAccessory(accessory, device) {
const deviceAdapter = new deviceAdapter_1.DeviceAdapter(device, this.log, this.client);
new platformAccessory_1.SmartThingsAirConditionerAccessory(this, accessory, deviceAdapter);
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
}
exports.SmartThingsPlatform = SmartThingsPlatform;
//# sourceMappingURL=platform.js.map