UNPKG

homebridge-nibe

Version:
130 lines (129 loc) 7.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NibePlatform = exports.PLUGIN_NAME = exports.PLATFORM_NAME = void 0; const MyUplinkApiFetcher_1 = require("./myuplink/MyUplinkApiFetcher"); const Locale_1 = require("./util/Locale"); const TemperatureSensorAccessory_1 = require("./nibeaccessory/TemperatureSensorAccessory"); const HotWaterAccessory_1 = require("./nibeaccessory/HotWaterAccessory"); const NewFirmwareAccessory_1 = require("./nibeaccessory/NewFirmwareAccessory"); exports.PLATFORM_NAME = 'Nibe'; exports.PLUGIN_NAME = 'homebridge-nibe'; /** * 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. */ class NibePlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.accessories = []; this.locale = new Locale_1.Locale(config.language, log); this.serviceResolver = { resolveCharacteristic(type) { return api.hap.Characteristic[type]; }, resolveService(type) { return api.hap.Service[type]; }, }; this.dataFetcher = new MyUplinkApiFetcher_1.MyUplinkApiFetcher({ clientId: config.identifier, clientSecret: config.secret, interval: config.pollingPeriod || 60, language: config.language, showApiResponse: config.showApiResponse || false, }, log); this.accessoryDefinitions = [ new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40067', 'average-outdoor-temperature-40067', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40004', 'outdoor-temperature-40004', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('44362', 'outdoor-temperature-44362', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40025', 'ventilation-exhaust-air-40025', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40026', 'ventilation-extract-air-40026', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40075', 'ventilation-supply-air-40075', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40183', 'ventilation-outdoor-40183', 1, this.locale, this.serviceResolver, this.log), new TemperatureSensorAccessory_1.TemperatureSensorAccessory('40013', 'hot-water-top-40013', 1, this.locale, this.serviceResolver, this.log), new HotWaterAccessory_1.HotWaterAccessory('hot-water', 3, this.locale, this.serviceResolver, this.log, async (deviceId, paramId, value) => { return await this.dataFetcher.setValue(deviceId, paramId, value); }), new NewFirmwareAccessory_1.NewFirmwareAccessory('new-firmware', 1, this.locale, this.serviceResolver, this.log), ]; this.log.debug('Finished initializing platform'); // 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" /* APIEvent.DID_FINISH_LAUNCHING */, () => { log.debug('Executed didFinishLaunching callback'); this.dataFetcher.start(); this.dataFetcher .on('data', (data) => { this.handleData(data); }).on('error', (data) => { this.log.error('Error:', data); }); }); this.api.on("shutdown" /* APIEvent.SHUTDOWN */, () => { this.dataFetcher.stop(); }); } handleData(data) { const touchedAccessoriesIds = Array(); this.accessoryDefinitions.forEach(accessoryDefinition => { const isApplicable = accessoryDefinition.isApplicable(data); const accessoryId = accessoryDefinition.buildIdentifier(data); const isDisabled = this.isDisabled(accessoryId); if (isApplicable && !isDisabled) { const platformAccessory = this.accessories.find(a => a.context.accessoryId === accessoryId); if (platformAccessory) { this.updateAccessory(accessoryDefinition, platformAccessory, data); } else { this.createAccessory(accessoryId, accessoryDefinition, data); } touchedAccessoriesIds.push(accessoryId); } }); this.removeNotExistingAccessories(data.system.systemId, data.device.id, touchedAccessoriesIds); } createAccessory(accessoryId, accessoryDefinition, data) { this.log.info('Adding new accessory: [%s]', accessoryId); const platformAccessory = new this.api.platformAccessory(accessoryDefinition.buildName(data), this.api.hap.uuid.generate(exports.PLUGIN_NAME + '-' + accessoryId)); accessoryDefinition.create(platformAccessory, data); this.api.registerPlatformAccessories(exports.PLUGIN_NAME, exports.PLATFORM_NAME, [platformAccessory]); this.accessories.push(platformAccessory); } updateAccessory(accessoryDefinition, platformAccessory, data) { if (!accessoryDefinition.isCurrentVersion(platformAccessory)) { this.log.info('Old version of accessory, recreating: [%s]', platformAccessory.context.accessoryId); accessoryDefinition.create(platformAccessory, data); } this.log.debug('Updating accessory: [%s]', platformAccessory.context.accessoryId); accessoryDefinition.update(platformAccessory, data); } isDisabled(accessoryId) { if (this.config.disabledAccessories && this.config.disabledAccessories.some(da => (da.indexOf(';') > 0 ? da.substring(0, da.indexOf(';')) : da) === accessoryId)) { this.log.debug('Disabled accessory: [%s]', accessoryId); return true; } return false; } removeNotExistingAccessories(systemId, deviceId, existingAccessoriesIds) { this.accessories .filter(accessory => accessory.context.systemId === systemId) .filter(accessory => accessory.context.deviceId === deviceId) .filter(accessory => !existingAccessoriesIds.includes(accessory.context.accessoryId)) .forEach(accessory => this.api.unregisterPlatformAccessories(exports.PLUGIN_NAME, exports.PLATFORM_NAME, [accessory])); } /** * 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}], UUID: [${accessory.UUID}]`); this.accessories.push(accessory); } } exports.NibePlatform = NibePlatform;