Multiple devices per platform

This commit is contained in:
dennis committed 2023-02-04 01:43:18 +01:00
1 parent 232d38ac8c
commit a396e49c8e
2 files changed
+66 -59

No files matched your search

+25 -20
View File
@@ -3,26 +3,31 @@
"pluginType": "platform", "pluginType": "platform",
"singular": true, "singular": true,
"schema": { "schema": {
"type": "object", "devices": {
"properties": { "type": "array",
"name": { "items": {
"title": "Name", "title": "Switch",
"type": "string", "type": "object",
"required": true, "properties": {
"default": "Scene Switcher" "name": {
}, "title": "Name",
"numScenes": { "type": "string",
"title": "Number of Scenes", "required": true
"type": "number", },
"required": true, "numScenes": {
"default": 4 "title": "Max. Scenes",
}, "type": "number",
"secondsToKeep": { "required": true,
"title": "Holding Time", "default": 5
"type": "number", },
"required": true, "secondsToKeep": {
"default": 5 "title": "Reset Timeout",
"type": "number",
"required": true,
"default": 10
}
}
} }
} }
} }
} }
+41 -39
View File
@@ -65,50 +65,52 @@ export class MultiTapSwitchPlatform implements DynamicPlatformPlugin {
} }
discoverDevices() { discoverDevices() {
const uuid = this.defineUuid(this.config.name, '0'); for (let i = 0; i < this.config.devices.length; i++) {
const accessoryConfig: PlatformConfigData = {
Name: this.config.devices[i].name,
MaxNumScenes: this.config.devices[i].numScenes,
SecondsBeforeReset: this.config.devices[i].secondsToKeep,
};
const accessoryConfig: PlatformConfigData = { const uuid = this.defineUuid(accessoryConfig.Name, '0');
Name: this.config.name ? this.config.name : 'Scene Switch',
MaxNumScenes: this.config.numScenes,
SecondsBeforeReset: this.config.secondsToKeep,
};
// see if an accessory with the same uuid has already been registered and restored from // 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 // the cached devices we stored in the `configureAccessory` method above
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) { if (existingAccessory) {
// Accessory already exists // Accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); 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.: // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device; // existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]); // this.api.updatePlatformAccessories([existingAccessory]);
if (existingAccessory.context.config !== accessoryConfig) { if (existingAccessory.context.config !== accessoryConfig) {
existingAccessory.context.config = accessoryConfig; existingAccessory.context.config = accessoryConfig;
this.api.updatePlatformAccessories([existingAccessory]); this.api.updatePlatformAccessories([existingAccessory]);
}
// Create the accessory handler for the restored accessory
// This is imported from `platformAccessory.ts`
new DeviceAccessory(this, existingAccessory);
} else {
// Accessory does not yet exist, so we need to create it
this.log.info('Adding INPUT:', accessoryConfig.Name, ' / Max ',
accessoryConfig.MaxNumScenes, ' scenes / ', accessoryConfig.SecondsBeforeReset, ' seconds before reset');
// Create new accessory
const accessory = new this.api.platformAccessory(accessoryConfig.Name, uuid);
// Store config in accessory context
accessory.context.config = accessoryConfig;
// Create the accessory handler for the new accessory
// This is imported from `platformAccessory.ts`
new DeviceAccessory(this, accessory);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
} }
// Create the accessory handler for the restored accessory
// This is imported from `platformAccessory.ts`
new DeviceAccessory(this, existingAccessory);
} else {
// Accessory does not yet exist, so we need to create it
this.log.info('Adding INPUT:', accessoryConfig.Name, ' / Max ',
accessoryConfig.MaxNumScenes, ' scenes / ', accessoryConfig.SecondsBeforeReset, ' seconds before reset');
// Create new accessory
const accessory = new this.api.platformAccessory(accessoryConfig.Name, uuid);
// Store config in accessory context
accessory.context.config = accessoryConfig;
// Create the accessory handler for the new accessory
// This is imported from `platformAccessory.ts`
new DeviceAccessory(this, accessory);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
} }
} }
} }