Skip to content

Commit

Permalink
Prevent accessories from being registered twice
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondelooff committed Apr 23, 2017
1 parent 7ecb20c commit 01f154d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/HueAccessory/Factory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const PLATFORM_VERSION = require('../../package.json').version;

const HueError = require('../HueError');

let Accessory;
Expand Down Expand Up @@ -27,6 +29,8 @@ class Factory {
const uuid = UUIDGen.generate(hueAccessory.uniqueId);
const accessory = new Accessory(hueAccessory.name, uuid);
accessory.context.uniqueId = hueAccessory.uniqueId;
accessory.context.platformVersion = PLATFORM_VERSION;
accessory.context.cached = false;

return accessory;
}
Expand Down
18 changes: 9 additions & 9 deletions lib/HuePlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DEFAULT_CONFIG = {

const OS = require('os');
const Huejay = require('huejay');
const PLATFORM_VERSION = require('../package.json').version;
const AccessoryFactory = require('./HueAccessory/Factory');

/**
Expand All @@ -28,8 +29,7 @@ class HuePlatform {

this.accessoryFactory = new AccessoryFactory(Accessory, Service, Characteristic, UUIDGen, log);

const version = require('../package.json').version;
this.log.info('HuePlatform version: ' + version);
this.log.info('HuePlatform version: ' + PLATFORM_VERSION);

this.api.on('didFinishLaunching', this.initialize.bind(this));
}
Expand All @@ -54,7 +54,7 @@ class HuePlatform {

_removeUnusableCachedAccessories() {
for (const accessory of this.cachedAccessories) {
if (accessory.context.uniqueId === undefined) {
if (accessory.context.uniqueId === undefined || accessory.context.platformVersion === undefined || accessory.context.platformVersion !== PLATFORM_VERSION) {
this.log.debug(`Unregistering accessory '${accessory.displayName}'...`);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
Expand Down Expand Up @@ -136,15 +136,14 @@ class HuePlatform {
}

_registerPlatformAccessory(accessory) {
this.log.debug(`Registering accessory '${accessory.displayName}', updating reachability...`);
if (!accessory.context.cached) {
this.log.debug(`Registering accessory '${accessory.displayName}'...`);
accessory.context.cached = true;

accessory.updateReachability(true);

try {
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
} catch (err) {
this.log.debug(`Platform accessory '${accessory.displayName}' already registered...`);
}

accessory.updateReachability(true);
}

_authenticateClients(callback) {
Expand Down Expand Up @@ -315,5 +314,6 @@ class HuePlatform {
module.exports = {
pluginName: PLUGIN_NAME,
platformName: PLATFORM_NAME,
platformVersion: PLATFORM_VERSION,
HuePlatform
};

0 comments on commit 01f154d

Please sign in to comment.