Skip to content

Commit

Permalink
fix when no devices
Browse files Browse the repository at this point in the history
  • Loading branch information
kreso975 committed Sep 23, 2024
1 parent 24ffb55 commit f1df854
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 75 deletions.
38 changes: 19 additions & 19 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,85 +65,85 @@
"type": "string",
"required": false
},
"deviceFirmwareVersion": {
"deviceFirmwareVersion": {
"title": "Device Firmware version",
"description": "Device Firmware version",
"type": "string",
"required": false
},
"urlON": {
"urlON": {
"title": "URL ON",
"description": "URL to Turn On the Switch",
"type": "string",
"required": false
},
"urlOFF": {
"urlOFF": {
"title": "URL OFF",
"description": "URL to Turn OFF the Switch",
"type": "string",
"required": false
},
"urlStatus": {
"urlStatus": {
"title": "URL Status",
"description": "URL to retrieve the switch status (on/off)",
"type": "string",
"required": false
},
"stateName": {
"stateName": {
"title": "State Name",
"description": "State JASON param Name",
"type": "string",
"required": false
},
"onStatusValue": {
"onStatusValue": {
"title": "On Status Value",
"description": "JSON return Value for status ON",
"type": "string",
"required": false
},
"offStatusValue": {
"offStatusValue": {
"title": "OFF Status Value",
"description": "JSON return Value for status OFF",
"type": "string",
"required": false
},
"sensorUrl": {
"sensorUrl": {
"title": "Sensor JSON URL",
"description": "JSON file containing sensor readings (temperature, humidity)",
"type": "string",
"required": false
},
"temperatureName": {
"temperatureName": {
"title": "Temperature Name",
"description": "JSON param name for Temperature reading",
"type": "string",
"required": false
},
"humidityName": {
"humidityName": {
"title": "Humidity Name",
"description": "JSON param name for Humidity reading",
"type": "string",
"required": false
},
"updateInterval": {
"updateInterval": {
"title": "Update interval",
"description": "update interval for reading Sensors, default is 60000 = 60 seconds = 1 minute",
"type": "integer",
"required": false
},
"mqttBroker": {
"mqttBroker": {
"title": "MQTT Broker",
"description": "URL of MQTT Broker",
"type": "string",
"required": false
},
"mqttPort": {
"mqttPort": {
"title": "MQTT port",
"description": "1883",
"type": "number",
"required": false
},
"mqttUsername": {
"mqttUsername": {
"title": "MQTT Username",
"description": "MQTT Username",
"type": "string",
Expand All @@ -155,31 +155,31 @@
"type": "string",
"required": false
},
"mqttTemperature": {
"mqttTemperature": {
"title": "mqtt Topic Temperature",
"description": "mqtt Topic Temperature",
"type": "string",
"required": false
},
"mqttHumidity": {
"mqttHumidity": {
"title": "mqtt Topic Humidity",
"description": "mqtt Topic Humidity",
"type": "string",
"required": false
},
"mqttSwitch": {
"mqttSwitch": {
"title": "MQTT Topic Switch",
"description": "MQTT Topic Switch",
"type": "string",
"required": false
},
"discordWebhook": {
"discordWebhook": {
"title": "Discord WebHook",
"description": "URL to Discord WebHook",
"type": "string",
"required": false
},
"discordUsername": {
"discordUsername": {
"title": "Discord Username",
"description": "Name for publisher",
"type": "string",
Expand Down
114 changes: 58 additions & 56 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class HttpSensorsAndSwitchesHomebridgePlatform implements DynamicPlatform

// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];

constructor(
public readonly log: Logging,
public readonly config: PlatformConfig,
Expand Down Expand Up @@ -56,63 +56,65 @@ export class HttpSensorsAndSwitchesHomebridgePlatform implements DynamicPlatform
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
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
switch ( device.deviceType ) {
case 'Switch':
new platformSwitch(this, existingAccessory);
break;
case 'Sensor':
new platformSensors(this, existingAccessory);
}

// 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`
switch ( accessory.context.device.deviceType ) {
case 'Switch':
new platformSwitch(this, accessory);
break;
case 'Sensor':
new platformSensors(this, accessory);
// 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
switch (device.deviceType) {
case 'Switch':
new platformSwitch(this, existingAccessory);
break;
case 'Sensor':
new platformSensors(this, existingAccessory);
}

// 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`
switch (accessory.context.device.deviceType) {
case 'Switch':
new platformSwitch(this, accessory);
break;
case 'Sensor':
new platformSensors(this, accessory);
}

// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}

// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
Expand Down

0 comments on commit f1df854

Please sign in to comment.