Fix wrong targetnode and nonupdating othergroupnodes (#1541)

This commit is contained in:
John Arild Berentsen 2018-08-06 10:40:31 +02:00 committed by Paulus Schoutsen
parent 60d04b5c58
commit e048060c72

View File

@ -41,19 +41,19 @@ class ZwaveGroups extends PolymerElement {
<!--TODO make api for getting groups and members--> <!--TODO make api for getting groups and members-->
<div class="device-picker"> <div class="device-picker">
<paper-dropdown-menu label="Group" dynamic-align="" class="flex"> <paper-dropdown-menu label="Group" dynamic-align="" class="flex">
<paper-listbox slot="dropdown-content" selected="{{selectedGroup}}"> <paper-listbox slot="dropdown-content" selected="{{_selectedGroup}}">
<template is="dom-repeat" items="[[groups]]" as="state"> <template is="dom-repeat" items="[[groups]]" as="state">
<paper-item>[[computeSelectCaptionGroup(state)]]</paper-item> <paper-item>[[_computeSelectCaptionGroup(state)]]</paper-item>
</template> </template>
</paper-listbox> </paper-listbox>
</paper-dropdown-menu> </paper-dropdown-menu>
</div> </div>
<template is="dom-if" if="[[computeIsGroupSelected(selectedGroup)]]"> <template is="dom-if" if="[[_computeIsGroupSelected(_selectedGroup)]]">
<div class="device-picker"> <div class="device-picker">
<paper-dropdown-menu label="Node to control" dynamic-align="" class="flex"> <paper-dropdown-menu label="Node to control" dynamic-align="" class="flex">
<paper-listbox slot="dropdown-content" selected="{{selectedTargetNode}}"> <paper-listbox slot="dropdown-content" selected="{{_selectedTargetNode}}">
<template is="dom-repeat" items="[[nodes]]" as="state"> <template is="dom-repeat" items="[[nodes]]" as="state">
<paper-item>[[computeSelectCaption(state)]]</paper-item> <paper-item>[[_computeSelectCaption(state)]]</paper-item>
</template> </template>
</paper-listbox> </paper-listbox>
</paper-dropdown-menu> </paper-dropdown-menu>
@ -61,23 +61,23 @@ class ZwaveGroups extends PolymerElement {
<div class="help-text"> <div class="help-text">
<span>Other Nodes in this group:</span> <span>Other Nodes in this group:</span>
<template is="dom-repeat" items="[[otherGroupNodes]]" as="state"> <template is="dom-repeat" items="[[_otherGroupNodes]]" as="state">
<div>[[state]]</div> <div>[[state]]</div>
</template> </template>
</div> </div>
<div class="help-text"> <div class="help-text">
<span>Max Associations:</span> <span>Max Associations:</span>
<span>[[maxAssociations]]</span> <span>[[_maxAssociations]]</span>
</div> </div>
</template> </template>
<template is="dom-if" if="[[computeIsTargetNodeSelected(selectedTargetNode)]]"> <template is="dom-if" if="[[_computeIsTargetNodeSelected(_selectedTargetNode)]]">
<div class="card-actions"> <div class="card-actions">
<template is="dom-if" if="[[!noAssociationsLeft]]"> <template is="dom-if" if="[[!_noAssociationsLeft]]">
<ha-call-service-button hass="[[hass]]" domain="zwave" service="change_association" service-data="[[computeAssocServiceData(selectedGroup, &quot;add&quot;)]]">Add To Group</ha-call-service-button> <ha-call-service-button hass="[[hass]]" domain="zwave" service="change_association" service-data="[[_computeAssocServiceData(_selectedGroup, &quot;add&quot;)]]">Add To Group</ha-call-service-button>
</template> </template>
<template is="dom-if" if="[[computeTargetInGroup(selectedGroup, selectedTargetNode)]]"> <template is="dom-if" if="[[_computeTargetInGroup(_selectedGroup, _selectedTargetNode)]]">
<ha-call-service-button hass="[[hass]]" domain="zwave" service="change_association" service-data="[[computeAssocServiceData(selectedGroup, &quot;remove&quot;)]]">Remove From Group</ha-call-service-button> <ha-call-service-button hass="[[hass]]" domain="zwave" service="change_association" service-data="[[_computeAssocServiceData(_selectedGroup, &quot;remove&quot;)]]">Remove From Group</ha-call-service-button>
</template> </template>
</div> </div>
</template> </template>
@ -87,53 +87,51 @@ class ZwaveGroups extends PolymerElement {
static get properties() { static get properties() {
return { return {
hass: { hass: Object,
type: Object,
},
nodes: { nodes: Array,
type: Array,
},
groups: { groups: Array,
type: Array,
},
selectedNode: { selectedNode: Number,
type: Number,
},
selectedTargetNode: { _selectedTargetNode: {
type: Number, type: Number,
value: -1 value: -1
}, },
selectedGroup: { _selectedGroup: {
type: Number, type: Number,
value: -1, value: -1
observer: 'selectedGroupChanged'
}, },
otherGroupNodes: { _otherGroupNodes: {
type: Array, type: Array,
value: -1, value: -1,
computed: 'computeOtherGroupNodes(selectedGroup)' computed: '_computeOtherGroupNodes(_selectedGroup)'
}, },
maxAssociations: { _maxAssociations: {
type: String, type: String,
value: '', value: '',
computed: 'computeMaxAssociations(selectedGroup)' computed: '_computeMaxAssociations(_selectedGroup)'
}, },
noAssociationsLeft: { _noAssociationsLeft: {
type: Boolean, type: Boolean,
value: true, value: true,
computed: 'computeAssociationsLeft(selectedGroup)' computed: '_computeAssociationsLeft(_selectedGroup)'
}, },
}; };
} }
static get observers() {
return [
'_selectedGroupChanged(groups, _selectedGroup)',
'_selectedTargetNodeChanged(nodes, _selectedTargetNode)'
];
}
ready() { ready() {
super.ready(); super.ready();
this.addEventListener('hass-service-called', ev => this.serviceCalled(ev)); this.addEventListener('hass-service-called', ev => this.serviceCalled(ev));
@ -141,100 +139,115 @@ class ZwaveGroups extends PolymerElement {
serviceCalled(ev) { serviceCalled(ev) {
if (ev.detail.success) { if (ev.detail.success) {
var foo = this; setTimeout(() => {
setTimeout(function () { this._refreshGroups(this.selectedNode);
foo.refreshGroups(foo.selectedNode);
}, 5000); }, 5000);
} }
} }
computeAssociationsLeft(selectedGroup) { _computeAssociationsLeft(selectedGroup) {
if (selectedGroup === -1) return true; if (selectedGroup === -1) return true;
return (this.maxAssociations === this.otherGroupNodes.length); return (this._maxAssociations === this._otherGroupNodes.length);
} }
computeMaxAssociations(selectedGroup) { _computeMaxAssociations(selectedGroup) {
if (selectedGroup === -1) return -1; if (selectedGroup === -1) return -1;
var maxAssociations = this.groups[selectedGroup].value.max_associations; const maxAssociations = this.groups[selectedGroup].value.max_associations;
if (!maxAssociations) return 'None'; if (!maxAssociations) return 'None';
return maxAssociations; return maxAssociations;
} }
computeOtherGroupNodes(selectedGroup) { _computeOtherGroupNodes(selectedGroup) {
if (selectedGroup === -1) return -1; if (selectedGroup === -1) return -1;
var associations = Object.values(this.groups[selectedGroup].value.association_instances); const associations = Object.values(this.groups[selectedGroup].value.association_instances);
if (!associations.length) return ['None']; if (!associations.length) return ['None'];
return associations.map((assoc) => { return associations.map((assoc) => {
if (!assoc.length || assoc.length !== 2) { if (!assoc.length || assoc.length !== 2) {
return 'Unknown Node: ' + assoc; return `Unknown Node: ${assoc}`;
} }
const id = assoc[0]; const id = assoc[0];
const instance = assoc[1]; const instance = assoc[1];
const node = this.nodes.find(n => n.attributes.node_id === id); const node = this.nodes.find(n => n.attributes.node_id === id);
if (!node) { if (!node) {
return 'Unknown Node (id: ' + (instance ? id + '.' + instance : id) + ')'; return `Unknown Node (${id}: (${instance} ? ${id}.${instance} : ${id}))`;
} }
let caption = this.computeSelectCaption(node); let caption = this._computeSelectCaption(node);
if (instance) { if (instance) {
caption += '/ Instance: ' + instance; caption += `/ Instance: ${instance}`;
} }
return caption; return caption;
}); });
} }
computeTargetInGroup(selectedGroup, selectedTargetNode) { _computeTargetInGroup(selectedGroup, selectedTargetNode) {
if (selectedGroup === -1 || selectedTargetNode === -1) return false; if (selectedGroup === -1 || selectedTargetNode === -1) return false;
const associations = Object.values(this.groups[selectedGroup].value.associations); const associations = Object.values(this.groups[selectedGroup].value.associations);
if (!associations.length) return false; if (!associations.length) return false;
return associations.indexOf(this.nodes[selectedTargetNode].attributes.node_id) !== -1; return associations.indexOf(this.nodes[selectedTargetNode].attributes.node_id) !== -1;
} }
computeSelectCaption(stateObj) { _computeSelectCaption(stateObj) {
return computeStateName(stateObj) + ' (Node:' + return (
stateObj.attributes.node_id + ' ' + `${computeStateName(stateObj)}
stateObj.attributes.query_stage + ')'; (Node: ${stateObj.attributes.node_id}
${stateObj.attributes.query_stage})`);
} }
computeSelectCaptionGroup(stateObj) { _computeSelectCaptionGroup(stateObj) {
return (stateObj.key + ': ' + stateObj.value.label); return (`${stateObj.key}: ${stateObj.value.label}`);
} }
computeIsTargetNodeSelected(selectedTargetNode) { _computeIsTargetNodeSelected(selectedTargetNode) {
return this.nodes && selectedTargetNode !== -1; return this.nodes && selectedTargetNode !== -1;
} }
computeIsGroupSelected(selectedGroup) { _computeIsGroupSelected(selectedGroup) {
return this.nodes && this.selectedNode !== -1 && selectedGroup !== -1; return this.nodes && this.selectedNode !== -1 && selectedGroup !== -1;
} }
computeAssocServiceData(selectedGroup, type) { _computeAssocServiceData(selectedGroup, type) {
if (!this.groups === -1 || selectedGroup === -1 || this.selectedNode === -1) return -1; if (!this.groups === -1 || selectedGroup === -1 ||
this.selectedNode === -1 || this._selectedTargetNode === -1) return -1;
return { return {
node_id: this.nodes[this.selectedNode].attributes.node_id, node_id: this.nodes[this.selectedNode].attributes.node_id,
association: type, association: type,
target_node_id: this.nodes[this.selectedTargetNode].attributes.node_id, target_node_id: this.nodes[this._selectedTargetNode].attributes.node_id,
group: this.groups[selectedGroup].key group: this.groups[selectedGroup].key
}; };
} }
refreshGroups(selectedNode) { async _refreshGroups(selectedNode) {
var groupData = []; const groupData = [];
this.hass.callApi('GET', 'zwave/groups/' + this.nodes[selectedNode].attributes.node_id).then(function (groups) { const groups = await this.hass.callApi('GET', `zwave/groups/${this.nodes[selectedNode].attributes.node_id}`);
Object.keys(groups).forEach(function (key) { Object.keys(groups).forEach((key) => {
groupData.push({ groupData.push({
key: key, key,
value: groups[key], value: groups[key],
});
}); });
this.groups = groupData; });
this.selectedGroupChanged(this.selectedGroup); this.setProperties({
}.bind(this)); groups: groupData,
_maxAssociations: groupData[this._selectedGroup].value.max_associations,
_otherGroupNodes: Object.values(groupData[this._selectedGroup].value.associations)
});
const oldGroup = this._selectedGroup;
this.setProperties({ _selectedGroup: -1 });
this.setProperties({ _selectedGroup: oldGroup });
} }
selectedGroupChanged(selectedGroup) { _selectedGroupChanged() {
if (this.selectedGroup === -1 || selectedGroup === -1) return; if (this._selectedGroup === -1) return;
this.maxAssociations = this.groups[selectedGroup].value.max_associations; this.setProperties({
this.otherGroupNodes = Object.values(this.groups[selectedGroup].value.associations); _maxAssociations: this.groups[this._selectedGroup].value.max_associations,
_otherGroupNodes: Object.values(this.groups[this._selectedGroup].value.associations) });
}
_selectedTargetNodeChanged() {
if (this._selectedGroup === -1) return;
this._computeAssocServiceData(this._selectedGroup, 'add');
if (this._computeTargetInGroup(this._selectedGroup, this._selectedTargetNode)) {
this._computeAssocServiceData(this._selectedGroup, 'remove');
}
} }
} }