homebridge-http-sensors-switches
Version:
This plugin communicates with your devices over HTTP or MQTT. Currently it supports Light Bulb, Switches, Outlets, Fan, Garage Door, Shades / Blinds, Temperature/Humidity, Motion, Contact and Occupancy sensor, Door, Sprinkler, Valve, Air Quality, Smoke, C
113 lines • 6.46 kB
JavaScript
import { PLATFORM_NAME, PLUGIN_NAME, listOfServices } from './settings.js';
/**
* Dynamically import services from listOfServices in ./settings.js
*/
const importedServices = await Promise.all(listOfServices.map(async ([deviceType, servicePath, className]) => {
const serviceModule = await import(servicePath);
return { deviceType, service: serviceModule[className] };
}));
export const serviceMap = Object.assign({}, ...importedServices.map(({ deviceType, service }) => ({ [deviceType]: service })));
/**
* HomebridgePlatform
* This class is the main constructor for plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class HttpSensorsAndSwitchesHomebridgePlatform {
log;
config;
api;
Service;
Characteristic;
// this is used to track restored cached accessories
accessories = [];
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
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.push(accessory);
}
/**
* This is an method to register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
discoverDevices() {
// Plugin a user-defined array in the platform config.
const platformConfigDevices = this.config.devices;
// loop over the discovered devices and register each one if it has not already been registered
if (Array.isArray(platformConfigDevices)) {
for (const device of platformConfigDevices) {
// 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.deviceID);
// 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.find(accessory => accessory.UUID === 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.:
// We will update the existing accessory.context to ensure that changes in the config take effect.
existingAccessory.context.device = device;
this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// dynamically import the service based on the device type set in settings.js
if (device.deviceType in serviceMap) {
const ServiceConstructor = serviceMap[device.deviceType];
new ServiceConstructor(this, existingAccessory);
}
else {
this.log.warn(`Unsupported device type: ${device.deviceType}`);
}
// 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.deviceName);
// create a new accessory
const accessory = new this.api.platformAccessory(device.deviceName, 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`
// dynamically import the service based on the device type set in settings.js
if (device.deviceType in serviceMap) {
const ServiceConstructor = serviceMap[device.deviceType];
new ServiceConstructor(this, accessory);
}
else {
this.log.warn(`Unsupported device type: ${device.deviceType}`);
}
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}
}
//# sourceMappingURL=platform.js.map