Skip to content

Commit

Permalink
Merge pull request #77 from algar42/master
Browse files Browse the repository at this point in the history
fixes duplicated manual states and controls update
  • Loading branch information
GermanBluefox authored Feb 13, 2020
2 parents 226dbe1 + aa568ea commit c449496
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion admin/custom_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<option value="camera" class="translate">camera</option>
<option value="plant" class="translate">plant</option>
<option value="sensor" class="translate">sensor</option>
<option value="binary_sensor" class="translate">binary_sensor</option>
<option value="climate" class="translate">climate</option>
<option value="alarm_control_panel" class="translate">alarm</option>
<option value="fan" class="translate">fan</option>
Expand Down Expand Up @@ -193,4 +194,4 @@
});
}

</script>
</script>
30 changes: 22 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ class WebServer {
_readAllEntities(cb) {
this._updateDevices()
.then(smartDevices => {
this._entities = smartDevices;
smartDevices.forEach((dev, devidx) => {
const foundIndex = this._entities.findIndex(x => x.entity_id === dev.entity_id);
if (foundIndex != -1) {this._entities[foundIndex] = dev;} else {this._entities.push(dev);}
});
this._getAllEntities(() => {
this._getAllStates(() => {
this._manageSubscribesFromConfig();
Expand Down Expand Up @@ -362,9 +365,14 @@ class WebServer {
}

this._ID2entity[obj._id] = this._ID2entity[obj._id] || [];
this._ID2entity[obj._id].push(entity);
this._addID2entity(obj._id, entity);
this._entity2ID[entity.entity_id] = entity;
this._entities.push(entity);
const foundIndex = this._entities.findIndex(x => x.entity_id === entity.entity_id);
if (foundIndex !== -1) {
this._entities[foundIndex] = entity;
} else {
this._entities.push(entity);
}
return entity;
}

Expand Down Expand Up @@ -2508,9 +2516,11 @@ class WebServer {
}
};

this._entities.map(obj => entity.entity_id === obj.entity_id ? entity : obj);
//moved update of _entities here because entity variable gets undefined after the _wss call below...

const foundIndex = this._entities.findIndex(x => x.entity_id === entity.entity_id);
if (foundIndex !== -1) {
this._entities[foundIndex] = entity;
}

this._wss.clients.forEach(ws => {
if (ws._subscribes && ws._subscribes.state_changed) {
ws._subscribes.state_changed.forEach(id => {
Expand All @@ -2530,6 +2540,11 @@ class WebServer {
if (!id) {
return;
}

// object might be deleted but still in room/func enums.
if (!objects[id]) {
return;
}

let friendlyName = this._getSmartName(objects, id);
if (typeof friendlyName === 'object' && friendlyName) {
Expand Down Expand Up @@ -2565,8 +2580,7 @@ class WebServer {

const _entity = result.find(e => e.entity_id === entity.entity_id);
if (_entity) {
this.log.debug('Duplicates found for ' + entity.entity_id);
return;
return this.log.debug('Duplicates found for ' + entity.entity_id);
}

result.push(entity);
Expand Down

0 comments on commit c449496

Please sign in to comment.