Simplify list selection 2 (#3156)

* light: use attr-for-selected for effect

* vacuum: use attr-for-selection for speed
This commit is contained in:
Joakim Plate 2019-05-03 17:54:38 +02:00 committed by Paulus Schoutsen
parent 34129cc7cb
commit 81088e0d07
2 changed files with 24 additions and 37 deletions

View File

@ -182,12 +182,17 @@ class MoreInfoLight extends LocalizeMixin(EventsMixin(PolymerElement)) {
dynamic-align="" dynamic-align=""
label="[[localize('ui.card.light.effect')]]" label="[[localize('ui.card.light.effect')]]"
> >
<paper-listbox slot="dropdown-content" selected="{{effectIndex}}"> <paper-listbox
slot="dropdown-content"
selected="[[stateObj.attributes.effect]]"
on-selected-changed="effectChanged"
attr-for-selected="item-name"
>
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.effect_list]]" items="[[stateObj.attributes.effect_list]]"
> >
<paper-item>[[item]]</paper-item> <paper-item item-name$="[[item]]">[[item]]</paper-item>
</template> </template>
</paper-listbox> </paper-listbox>
</ha-paper-dropdown-menu> </ha-paper-dropdown-menu>
@ -212,12 +217,6 @@ class MoreInfoLight extends LocalizeMixin(EventsMixin(PolymerElement)) {
observer: "stateObjChanged", observer: "stateObjChanged",
}, },
effectIndex: {
type: Number,
value: -1,
observer: "effectChanged",
},
brightnessSliderValue: { brightnessSliderValue: {
type: Number, type: Number,
value: 0, value: 0,
@ -264,13 +263,6 @@ class MoreInfoLight extends LocalizeMixin(EventsMixin(PolymerElement)) {
s: newVal.attributes.hs_color[1] / 100, s: newVal.attributes.hs_color[1] / 100,
}; };
} }
if (newVal.attributes.effect_list) {
props.effectIndex = newVal.attributes.effect_list.indexOf(
newVal.attributes.effect
);
} else {
props.effectIndex = -1;
}
} }
this.setProperties(props); this.setProperties(props);
@ -293,17 +285,15 @@ class MoreInfoLight extends LocalizeMixin(EventsMixin(PolymerElement)) {
return classes.join(" "); return classes.join(" ");
} }
effectChanged(effectIndex) { effectChanged(ev) {
var effectInput; var oldVal = this.stateObj.attributes.effect;
// Selected Option will transition to '' before transitioning to new value var newVal = ev.detail.value;
if (effectIndex === "" || effectIndex === -1) return;
effectInput = this.stateObj.attributes.effect_list[effectIndex]; if (!newVal || oldVal === newVal) return;
if (effectInput === this.stateObj.attributes.effect) return;
this.hass.callService("light", "turn_on", { this.hass.callService("light", "turn_on", {
entity_id: this.stateObj.entity_id, entity_id: this.stateObj.entity_id,
effect: effectInput, effect: newVal,
}); });
} }

View File

@ -109,12 +109,17 @@ class MoreInfoVacuum extends PolymerElement {
dynamic-align="" dynamic-align=""
label="Fan speed" label="Fan speed"
> >
<paper-listbox slot="dropdown-content" selected="{{fanSpeedIndex}}"> <paper-listbox
slot="dropdown-content"
selected="[[stateObj.attributes.fan_speed]]"
on-selected-changed="fanSpeedChanged"
attr-for-selected="item-name"
>
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.fan_speed_list]]" items="[[stateObj.attributes.fan_speed_list]]"
> >
<paper-item>[[item]]</paper-item> <paper-item item-name$="[[item]]">[[item]]</paper-item>
</template> </template>
</paper-listbox> </paper-listbox>
</ha-paper-dropdown-menu> </ha-paper-dropdown-menu>
@ -150,12 +155,6 @@ class MoreInfoVacuum extends PolymerElement {
stateObj: { stateObj: {
type: Object, type: Object,
}, },
fanSpeedIndex: {
type: Number,
value: -1,
observer: "fanSpeedChanged",
},
}; };
} }
@ -206,17 +205,15 @@ class MoreInfoVacuum extends PolymerElement {
); );
} }
fanSpeedChanged(fanSpeedIndex) { fanSpeedChanged(ev) {
var fanSpeedInput; var oldVal = this.stateObj.attributes.fan_speed;
// Selected Option will transition to '' before transitioning to new value var newVal = ev.detail.value;
if (fanSpeedIndex === "" || fanSpeedIndex === -1) return;
fanSpeedInput = this.stateObj.attributes.fan_speed_list[fanSpeedIndex]; if (!newVal || oldVal === newVal) return;
if (fanSpeedInput === this.stateObj.attributes.fan_speed) return;
this.hass.callService("vacuum", "set_fan_speed", { this.hass.callService("vacuum", "set_fan_speed", {
entity_id: this.stateObj.entity_id, entity_id: this.stateObj.entity_id,
fan_speed: fanSpeedInput, fan_speed: newVal,
}); });
} }