UNPKG

@smj0x/homebridge-shortcuts-buttons

Version:

Run any Apple Shortcut with just the tap of a button, and execute a custom unix command (or another shortcut!) after completion to handle its success/failure, using x-callback-url.

61 lines 3.06 kB
import { HSBService } from './service.js'; import { PLATFORM_NAME } from './settings.js'; export class HSBAccessory { platform; platformAccessory; constructor(platform, platformAccessory) { this.platform = platform; this.platformAccessory = platformAccessory; try { this.addAccessoryInformationService(); this.addShortcutsServices(); } catch (e) { platform.log.error('HSBAccessory::constructor', e); } } addAccessoryInformationService() { this.platformAccessory .getService(this.platform.api.hap.Service.AccessoryInformation) .setCharacteristic(this.platform.api.hap.Characteristic.Manufacturer, 'Homebridge.io') .setCharacteristic(this.platform.api.hap.Characteristic.Model, PLATFORM_NAME) .setCharacteristic(this.platform.api.hap.Characteristic.SerialNumber, this.platformAccessory.context.device.serialNumber); } addShortcutsServices() { const serviceType = this.platform.api.hap.Service[this.platform.config.serviceType]; const activeServicesSubtypes = new Set(); for (const serviceConfig of this.platform.config.services) { const subtype = this.platform.api.hap.uuid.generate(serviceType + JSON.stringify(serviceConfig)); let service = this.platformAccessory.getServiceById(serviceType, subtype); let logServiceOrigin = 'Restored from cache'; if (!service) { service = this.platformAccessory.addService(serviceType, serviceConfig.serviceName, subtype); logServiceOrigin = 'Created from fresh config'; } this.platform.log.debug('Accessory::addShortcutsServices', `Service.${serviceType.name}(${service.displayName})`, logServiceOrigin); activeServicesSubtypes.add(service.subtype); new HSBService(this.platform.log, service, serviceConfig, this.platform.server, this.platform.utils, this.platform.api.hap.Characteristic); } const servicesToRemove = this.platformAccessory.services.filter(({ subtype }) => typeof subtype === 'string' && !activeServicesSubtypes.has(subtype)); for (const service of servicesToRemove) { this.platformAccessory.removeService(service); this.platform.log.debug('Accessory::addShortcutsServices', `Service.${this.platform.api.hap.Service.serialize(service).constructorName}` + `(${service.displayName})`, 'Removed as outdated'); } if (servicesToRemove.length > 0) { this.platform.api.updatePlatformAccessories([this.platformAccessory]); this.platform.log.debug('Accessory::addShortcutsServices', 'Requested platform accessory update'); } } } export class HSBDevice { config; constructor(config) { this.config = config; } serialNumber = '634EA867A81D59F1898'; get displayName() { return this.config.accessoryName; } } //# sourceMappingURL=accessory.js.map