homebridge#Categories TypeScript Examples
The following examples show how to use
homebridge#Categories.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: platform.ts From homebridge-tapo-p100 with Apache License 2.0 | 4 votes |
/**
* 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.
*/
discoverDevices() {
// EXAMPLE ONLY
// A real plugin you would discover accessories from the local network, cloud services
// or a user-defined array in the platform config.
const devices = this.config.discoveryOptions.devices;
if(devices){
// loop over the discovered devices and register each one if it has not already been registered
for (const device of devices) {
// 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.host + (device.name ? device.name : ''));
// 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
if (device) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
if(device.type && device.type.toLowerCase() === 'colorlight'){
new L530Accessory(this.log, this, existingAccessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else if(device.type && device.type.toLowerCase() === 'light'){
new L510EAccessory(this.log, this, existingAccessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else if(device.type && device.type.toLowerCase() === 'powerplug'){
new P110Accessory(this.log, this, existingAccessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else{
new P100Accessory(this.log, this, existingAccessory, device.timeout ? device.timeout : 2, device.updateInterval);
}
// update accessory cache with any changes to the accessory details and information
this.api.updatePlatformAccessories([existingAccessory]);
} else if (!device) {
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// 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.host);
let accessory:PlatformAccessory;
// create the accessory handler for the newly create accessory
// this is imported from `platformAccessory.ts`
if(device.type && device.type.toLowerCase() === 'colorlight'){
// create a new accessory
accessory = new this.api.platformAccessory(device.name ? device.name : device.host, uuid, Categories.LIGHTBULB);
// 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;
new L530Accessory(this.log, this, accessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else if(device.type && device.type.toLowerCase() === 'light'){
// create a new accessory
accessory = new this.api.platformAccessory(device.name ? device.name : device.host, uuid, Categories.LIGHTBULB);
// 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;
new L510EAccessory(this.log, this, accessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else if(device.type && device.type.toLowerCase() === 'powerplug'){
// create a new accessory
accessory = new this.api.platformAccessory(device.name ? device.name : device.host, uuid, Categories.OUTLET);
// 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;
new P110Accessory(this.log, this, accessory, device.timeout ? device.timeout : 2, device.updateInterval);
} else{
// create a new accessory
accessory = new this.api.platformAccessory(device.name ? device.name : device.host, uuid, Categories.OUTLET);
// 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;
new P100Accessory(this.log, this, accessory, device.timeout ? device.timeout : 2, device.updateInterval);
}
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}