UNPKG

homebridge-raspberry-pi-sensehat

Version:
102 lines 4.58 kB
import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes'; import { RaspberryTemperatureAccessory } from './raspberryTemperatureAccessory.js'; import { SensehatLightAccessory } from './sensehatLightAccessory.js'; import { SensehatSensorAccessory } from './sensehatSensorAccessory.js'; import { SynologyTemperatureAccessory } from './synologyTemperatureAccessory.js'; import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; /** * Homebridge Platform */ export class SensehatPlatform { log; config; api; Service; Characteristic; accessories = new Map(); discoveredCacheUUIDs = []; // eslint-disable-next-line @typescript-eslint/no-explicit-any CustomService; // eslint-disable-next-line @typescript-eslint/no-explicit-any CustomCharacteristic; scriptPath; pythonName; constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = api.hap.Service; this.Characteristic = api.hap.Characteristic; this.CustomService = new EveHomeKitTypes(this.api).Services; this.CustomCharacteristic = new EveHomeKitTypes(this.api).Characteristics; this.scriptPath = config.scriptPath || path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'script'); this.pythonName = config.pythonName || 'python'; this.api.on('didFinishLaunching', async () => { try { await this.createAccessories(); } catch (error) { this.log.error('Failed to create accessories', error); } }); } configureAccessory(accessory) { this.accessories.set(accessory.UUID, accessory); } getDevices() { return [ { uniqueId: 'Raspberry', displayName: this.config.raspberryName || 'Raspberry', type: 'Raspberry' }, { uniqueId: 'SenseHat-Light', displayName: this.config.sensehatName || 'SenseHat', type: 'Light' }, { uniqueId: 'SenseHat-Sensor', displayName: this.config.sensehatName || 'SenseHat', type: 'Sensor' }, { uniqueId: 'Synology', displayName: this.config.synologyName || 'Synology', type: 'Synology' }, ]; } initAccessory(type, accessory) { const mapping = { Raspberry: RaspberryTemperatureAccessory, Light: SensehatLightAccessory, Sensor: SensehatSensorAccessory, Synology: SynologyTemperatureAccessory, }; new mapping[type](this, accessory); } async createAccessories() { for (const device of this.getDevices()) { // check if accessory should be shown based on the settings const showType = { Raspberry: this.config.showRaspberryTemperature ?? true, Light: this.config.showSensehatLight ?? true, Sensor: this.config.showSensehatSensor ?? true, Synology: this.config.showSynologyTemperature ?? true, }; if (!showType[device.type]) { this.log.info(`${device.displayName} ${device.displayName === device.type ? '' : device.type + ' '}skipped due to settings`); continue; } // create accessory const uuid = this.api.hap.uuid.generate(device.uniqueId); const existingAccessory = this.accessories.get(uuid); const accessory = existingAccessory ?? new this.api.platformAccessory(device.displayName, uuid); accessory.context.device = device; this.initAccessory(device.type, accessory); if (!existingAccessory) { this.log.debug(`Adding new accessory: ${device.displayName}`); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } else { this.log.debug(`Restoring existing accessory from cache: ${device.displayName}`); } this.discoveredCacheUUIDs.push(uuid); } // remove any cached accessories that are no longer present for (const [uuid, accessory] of this.accessories) { if (!this.discoveredCacheUUIDs.includes(uuid)) { this.log.debug(`Removing stale accessory from cache: ${accessory.displayName}`); this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } } } } //# sourceMappingURL=sensehatPlatform.js.map