UNPKG

homebridge-localtuya

Version:
149 lines 8.28 kB
import { TuyaAccessory } from './platformAccessory.js'; import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'; // We don't use TuyAPI directly in this file, it's used in platformAccessory.ts // This is only required when using Custom Services and Characteristics not support by HomeKit import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes'; /** * 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. */ export class LocalTuyaPlatform { log; config; api; Service; Characteristic; // this is used to track restored cached accessories and their handlers accessories = new Map(); accessoryHandlers = new Map(); discoveredCacheUUIDs = []; // This is only required when using Custom Services and Characteristics not support by HomeKit // eslint-disable-next-line @typescript-eslint/no-explicit-any CustomServices; // eslint-disable-next-line @typescript-eslint/no-explicit-any CustomCharacteristics; constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = api.hap.Service; this.Characteristic = api.hap.Characteristic; // This is only required when using Custom Services and Characteristics not support by HomeKit this.CustomServices = new EveHomeKitTypes(this.api).Services; this.CustomCharacteristics = new EveHomeKitTypes(this.api).Characteristics; this.log.debug('Finished initializing platform:', this.config.name); // 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', () => { log.debug('Executed didFinishLaunching callback'); // run the method to discover / register your devices as accessories this.discoverDevices(); }); } /** * This function is invoked when homebridge restores cached accessories from disk at startup. * It should be used to set up event handlers for characteristics and update respective values. */ configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); // add the restored accessory to the accessories cache, so we can track if it has already been registered this.accessories.set(accessory.UUID, accessory); } /** * This is an example method showing how to register discovered accessories. * Accessories must only be registered once, previously created accessories * must not be registered again to prevent "duplicate UUID" errors. */ async discoverDevices() { // Get devices from config const configDevices = this.config.devices || []; // Read devices.json if it exists try { const fs = await import('fs'); const path = await import('path'); // Get directory of current module const moduleDir = path.dirname(new URL(import.meta.url).pathname); const devicesPath = path.resolve(moduleDir, '../../devices.json'); if (fs.existsSync(devicesPath)) { const devicesJson = JSON.parse(fs.readFileSync(devicesPath, 'utf8')); if (Array.isArray(devicesJson)) { for (const device of devicesJson) { if (!configDevices.find((d) => d.id === device.id)) { configDevices.push({ name: device.name, id: device.id, key: device.key, ip: device.ip, type: 'fanLight', // Default type for our fan/light combo }); } } } } } catch (error) { this.log.debug('No devices.json found or error reading it:', error); } // loop over the discovered devices and register each one if it has not already been registered for (const device of configDevices) { // generate a unique id for the accessory this should be generated from // something globally unique, but constant, for example, the device serial // number or MAC address const uuid = this.api.hap.uuid.generate(device.id); // see if an accessory with the same uuid has already been registered and restored from // the cached devices we stored in the `configureAccessory` method above const existingAccessory = this.accessories.get(uuid); if (existingAccessory) { // the accessory already exists this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. e.g.: // existingAccessory.context.device = device; // this.api.updatePlatformAccessories([existingAccessory]); // create the accessory handler for the restored accessory // this is imported from `platformAccessory.ts` const handler = new TuyaAccessory(this, existingAccessory); this.accessoryHandlers.set(uuid, handler); // it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, e.g.: // remove platform accessories when no longer present // this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]); // this.log.info('Removing existing accessory from cache:', existingAccessory.displayName); } else { // the accessory does not yet exist, so we need to create it this.log.info('Adding new accessory:', device.name); // create a new accessory const accessory = new this.api.platformAccessory(device.name, uuid); // store a copy of the device object in the `accessory.context` // the `context` property can be used to store any data about the accessory you may need accessory.context.device = device; // create the accessory handler for the newly create accessory // this is imported from `platformAccessory.ts` const handler = new TuyaAccessory(this, accessory); this.accessoryHandlers.set(uuid, handler); // link the accessory to your platform this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } // push into discoveredCacheUUIDs this.discoveredCacheUUIDs.push(uuid); } // you can also deal with accessories from the cache which are no longer present by removing them from Homebridge // for example, if your plugin logs into a cloud account to retrieve a device list, and a user has previously removed a device // from this cloud account, then this device will no longer be present in the device list but will still be in the Homebridge cache for (const [uuid, accessory] of this.accessories) { if (!this.discoveredCacheUUIDs.includes(uuid)) { this.log.info('Removing existing accessory from cache:', accessory.displayName); // Clean up the accessory handler const handler = this.accessoryHandlers.get(uuid); if (handler) { handler.destroy(); this.accessoryHandlers.delete(uuid); } this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } } } } //# sourceMappingURL=platform.js.map