Simplify list selection (#3148)

* water_heater: Use attr-for-selected for operation mode

* climate: Use attr-for-selected for operation, fan, swing mode

* fan: Use attr-for-selected for speed

* fan: skip extra property

* climate: drop extra property

* water_heater: avoid extra property

* media_player: drop extra variable for source and sound_mode

* water_heater: missed change
This commit is contained in:
Joakim Plate 2019-05-02 16:09:06 +02:00 committed by Paulus Schoutsen
parent 5b12ca94e9
commit 8a86dd8426
4 changed files with 78 additions and 162 deletions

View File

@ -200,14 +200,15 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
> >
<paper-listbox <paper-listbox
slot="dropdown-content" slot="dropdown-content"
selected="{{operationIndex}}" selected="[[stateObj.attributes.operation_mode]]"
attr-for-selected="item-name"
on-selected-changed="handleOperationmodeChanged"
> >
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.operation_list]]" items="[[stateObj.attributes.operation_list]]"
on-dom-change="handleOperationListUpdate"
> >
<paper-item <paper-item item-name$="[[item]]"
>[[_localizeOperationMode(localize, item)]]</paper-item >[[_localizeOperationMode(localize, item)]]</paper-item
> >
</template> </template>
@ -224,13 +225,19 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
dynamic-align="" dynamic-align=""
label="[[localize('ui.card.climate.fan_mode')]]" label="[[localize('ui.card.climate.fan_mode')]]"
> >
<paper-listbox slot="dropdown-content" selected="{{fanIndex}}"> <paper-listbox
slot="dropdown-content"
selected="[[stateObj.attributes.fan_mode]]"
attr-for-selected="item-name"
on-selected-changed="handleFanmodeChanged"
>
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.fan_list]]" items="[[stateObj.attributes.fan_list]]"
on-dom-change="handleFanListUpdate"
> >
<paper-item>[[_localizeFanMode(localize, item)]]</paper-item> <paper-item item-name$="[[item]]"
>[[_localizeFanMode(localize, item)]]
</paper-item>
</template> </template>
</paper-listbox> </paper-listbox>
</ha-paper-dropdown-menu> </ha-paper-dropdown-menu>
@ -244,13 +251,17 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
dynamic-align="" dynamic-align=""
label="[[localize('ui.card.climate.swing_mode')]]" label="[[localize('ui.card.climate.swing_mode')]]"
> >
<paper-listbox slot="dropdown-content" selected="{{swingIndex}}"> <paper-listbox
slot="dropdown-content"
selected="[[stateObj.attributes.swing_mode]]"
attr-for-selected="item-name"
on-selected-changed="handleSwingmodeChanged"
>
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.swing_list]]" items="[[stateObj.attributes.swing_list]]"
on-dom-change="handleSwingListUpdate"
> >
<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>
@ -297,23 +308,6 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
observer: "stateObjChanged", observer: "stateObjChanged",
}, },
operationIndex: {
type: Number,
value: -1,
observer: "handleOperationmodeChanged",
},
fanIndex: {
type: Number,
value: -1,
observer: "handleFanmodeChanged",
},
swingIndex: {
type: Number,
value: -1,
observer: "handleSwingmodeChanged",
},
awayToggleChecked: Boolean, awayToggleChecked: Boolean,
auxToggleChecked: Boolean, auxToggleChecked: Boolean,
onToggleChecked: Boolean, onToggleChecked: Boolean,
@ -346,36 +340,6 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
} }
} }
handleOperationListUpdate() {
// force polymer to recognize selected item change (to update actual label)
this.operationIndex = -1;
if (this.stateObj.attributes.operation_list) {
this.operationIndex = this.stateObj.attributes.operation_list.indexOf(
this.stateObj.attributes.operation_mode
);
}
}
handleSwingListUpdate() {
// force polymer to recognize selected item change (to update actual label)
this.swingIndex = -1;
if (this.stateObj.attributes.swing_list) {
this.swingIndex = this.stateObj.attributes.swing_list.indexOf(
this.stateObj.attributes.swing_mode
);
}
}
handleFanListUpdate() {
// force polymer to recognize selected item change (to update actual label)
this.fanIndex = -1;
if (this.stateObj.attributes.fan_list) {
this.fanIndex = this.stateObj.attributes.fan_list.indexOf(
this.stateObj.attributes.fan_mode
);
}
}
computeTemperatureStepSize(hass, stateObj) { computeTemperatureStepSize(hass, stateObj) {
if (stateObj.attributes.target_temp_step) { if (stateObj.attributes.target_temp_step) {
return stateObj.attributes.target_temp_step; return stateObj.attributes.target_temp_step;
@ -517,33 +481,27 @@ class MoreInfoClimate extends LocalizeMixin(EventsMixin(PolymerElement)) {
this.callServiceHelper(newVal ? "turn_on" : "turn_off", {}); this.callServiceHelper(newVal ? "turn_on" : "turn_off", {});
} }
handleFanmodeChanged(fanIndex) { handleFanmodeChanged(ev) {
// Selected Option will transition to '' before transitioning to new value const oldVal = this.stateObj.attributes.fan_mode;
if (fanIndex === "" || fanIndex === -1) return; const newVal = ev.detail.value;
const fanInput = this.stateObj.attributes.fan_list[fanIndex]; if (!newVal || oldVal === newVal) return;
if (fanInput === this.stateObj.attributes.fan_mode) return; this.callServiceHelper("set_fan_mode", { fan_mode: newVal });
this.callServiceHelper("set_fan_mode", { fan_mode: fanInput });
} }
handleOperationmodeChanged(operationIndex) { handleOperationmodeChanged(ev) {
// Selected Option will transition to '' before transitioning to new value const oldVal = this.stateObj.attributes.operation_mode;
if (operationIndex === "" || operationIndex === -1) return; const newVal = ev.detail.value;
const operationInput = this.stateObj.attributes.operation_list[ if (!newVal || oldVal === newVal) return;
operationIndex
];
if (operationInput === this.stateObj.attributes.operation_mode) return;
this.callServiceHelper("set_operation_mode", { this.callServiceHelper("set_operation_mode", {
operation_mode: operationInput, operation_mode: newVal,
}); });
} }
handleSwingmodeChanged(swingIndex) { handleSwingmodeChanged(ev) {
// Selected Option will transition to '' before transitioning to new value const oldVal = this.stateObj.attributes.swing_mode;
if (swingIndex === "" || swingIndex === -1) return; const newVal = ev.detail.value;
const swingInput = this.stateObj.attributes.swing_list[swingIndex]; if (!newVal || oldVal === newVal) return;
if (swingInput === this.stateObj.attributes.swing_mode) return; this.callServiceHelper("set_swing_mode", { swing_mode: newVal });
this.callServiceHelper("set_swing_mode", { swing_mode: swingInput });
} }
callServiceHelper(service, data) { callServiceHelper(service, data) {

View File

@ -49,12 +49,17 @@ class MoreInfoFan extends LocalizeMixin(EventsMixin(PolymerElement)) {
dynamic-align="" dynamic-align=""
label="[[localize('ui.card.fan.speed')]]" label="[[localize('ui.card.fan.speed')]]"
> >
<paper-listbox slot="dropdown-content" selected="{{speedIndex}}"> <paper-listbox
slot="dropdown-content"
selected="[[stateObj.attributes.speed]]"
on-selected-changed="speedChanged"
attr-for-selected="item-name"
>
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.speed_list]]" items="[[stateObj.attributes.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>
@ -108,12 +113,6 @@ class MoreInfoFan extends LocalizeMixin(EventsMixin(PolymerElement)) {
observer: "stateObjChanged", observer: "stateObjChanged",
}, },
speedIndex: {
type: Number,
value: -1,
observer: "speedChanged",
},
oscillationToggleChecked: { oscillationToggleChecked: {
type: Boolean, type: Boolean,
}, },
@ -124,9 +123,6 @@ class MoreInfoFan extends LocalizeMixin(EventsMixin(PolymerElement)) {
if (newVal) { if (newVal) {
this.setProperties({ this.setProperties({
oscillationToggleChecked: newVal.attributes.oscillating, oscillationToggleChecked: newVal.attributes.oscillating,
speedIndex: newVal.attributes.speed_list
? newVal.attributes.speed_list.indexOf(newVal.attributes.speed)
: -1,
}); });
} }
@ -144,17 +140,15 @@ class MoreInfoFan extends LocalizeMixin(EventsMixin(PolymerElement)) {
); );
} }
speedChanged(speedIndex) { speedChanged(ev) {
var speedInput; var oldVal = this.stateObj.attributes.speed;
// Selected Option will transition to '' before transitioning to new value var newVal = ev.detail.value;
if (speedIndex === "" || speedIndex === -1) return;
speedInput = this.stateObj.attributes.speed_list[speedIndex]; if (!newVal || oldVal === newVal) return;
if (speedInput === this.stateObj.attributes.speed) return;
this.hass.callService("fan", "turn_on", { this.hass.callService("fan", "turn_on", {
entity_id: this.stateObj.entity_id, entity_id: this.stateObj.entity_id,
speed: speedInput, speed: newVal,
}); });
} }

View File

@ -157,7 +157,8 @@ class MoreInfoMediaPlayer extends LocalizeMixin(EventsMixin(PolymerElement)) {
<paper-listbox <paper-listbox
slot="dropdown-content" slot="dropdown-content"
attr-for-selected="item-name" attr-for-selected="item-name"
selected="{{SourceInput}}" selected="[[playerObj.source]]"
on-selected-changed="handleSourceChanged"
> >
<template is="dom-repeat" items="[[playerObj.sourceList]]"> <template is="dom-repeat" items="[[playerObj.sourceList]]">
<paper-item item-name$="[[item]]">[[item]]</paper-item> <paper-item item-name$="[[item]]">[[item]]</paper-item>
@ -178,7 +179,8 @@ class MoreInfoMediaPlayer extends LocalizeMixin(EventsMixin(PolymerElement)) {
<paper-listbox <paper-listbox
slot="dropdown-content" slot="dropdown-content"
attr-for-selected="item-name" attr-for-selected="item-name"
selected="{{SoundModeInput}}" selected="[[playerObj.soundMode]]"
on-selected-changed="handleSoundModeChanged"
> >
<template is="dom-repeat" items="[[playerObj.soundModeList]]"> <template is="dom-repeat" items="[[playerObj.soundModeList]]">
<paper-item item-name$="[[item]]">[[item]]</paper-item> <paper-item item-name$="[[item]]">[[item]]</paper-item>
@ -218,18 +220,6 @@ class MoreInfoMediaPlayer extends LocalizeMixin(EventsMixin(PolymerElement)) {
observer: "playerObjChanged", observer: "playerObjChanged",
}, },
SourceInput: {
type: String,
value: "",
observer: "handleSourceChanged",
},
SoundModeInput: {
type: String,
value: "",
observer: "handleSoundModeChanged",
},
ttsLoaded: { ttsLoaded: {
type: Boolean, type: Boolean,
computed: "computeTTSLoaded(hass)", computed: "computeTTSLoaded(hass)",
@ -252,14 +242,6 @@ class MoreInfoMediaPlayer extends LocalizeMixin(EventsMixin(PolymerElement)) {
} }
playerObjChanged(newVal, oldVal) { playerObjChanged(newVal, oldVal) {
if (newVal && newVal.sourceList !== undefined) {
this.SourceInput = newVal.source;
}
if (newVal && newVal.soundModeList !== undefined) {
this.SoundModeInput = newVal.soundMode;
}
if (oldVal) { if (oldVal) {
setTimeout(() => { setTimeout(() => {
this.fire("iron-resize"); this.fire("iron-resize");
@ -346,26 +328,26 @@ class MoreInfoMediaPlayer extends LocalizeMixin(EventsMixin(PolymerElement)) {
this.playerObj.nextTrack(); this.playerObj.nextTrack();
} }
handleSourceChanged(newVal, oldVal) { handleSourceChanged(ev) {
// Selected Option will transition to '' before transitioning to new value if (!this.playerObj) return;
if (
oldVal && var oldVal = this.playerObj.source;
newVal && var newVal = ev.detail.value;
newVal !== this.playerObj.source &&
this.playerObj.supportsSelectSource if (!newVal || oldVal === newVal) return;
) {
this.playerObj.selectSource(newVal); this.playerObj.selectSource(newVal);
}
} }
handleSoundModeChanged(newVal, oldVal) { handleSoundModeChanged(ev) {
if ( if (!this.playerObj) return;
oldVal &&
newVal !== this.playerObj.soundMode && var oldVal = this.playerObj.soundMode;
this.playerObj.supportsSelectSoundMode var newVal = ev.detail.value;
) {
this.playerObj.selectSoundMode(newVal); if (!newVal || oldVal === newVal) return;
}
this.playerObj.selectSoundMode(newVal);
} }
handleVolumeTap() { handleVolumeTap() {

View File

@ -100,14 +100,15 @@ class MoreInfoWaterHeater extends LocalizeMixin(EventsMixin(PolymerElement)) {
> >
<paper-listbox <paper-listbox
slot="dropdown-content" slot="dropdown-content"
selected="{{operationIndex}}" selected="[[stateObj.attributes.operation_mode]]"
attr-for-selected="item-name"
on-selected-changed="handleOperationmodeChanged"
> >
<template <template
is="dom-repeat" is="dom-repeat"
items="[[stateObj.attributes.operation_list]]" items="[[stateObj.attributes.operation_list]]"
on-dom-change="handleOperationListUpdate"
> >
<paper-item <paper-item item-name$="[[item]]"
>[[_localizeOperationMode(localize, item)]]</paper-item >[[_localizeOperationMode(localize, item)]]</paper-item
> >
</template> </template>
@ -146,11 +147,6 @@ class MoreInfoWaterHeater extends LocalizeMixin(EventsMixin(PolymerElement)) {
observer: "stateObjChanged", observer: "stateObjChanged",
}, },
operationIndex: {
type: Number,
value: -1,
observer: "handleOperationmodeChanged",
},
awayToggleChecked: Boolean, awayToggleChecked: Boolean,
}; };
} }
@ -173,16 +169,6 @@ class MoreInfoWaterHeater extends LocalizeMixin(EventsMixin(PolymerElement)) {
} }
} }
handleOperationListUpdate() {
// force polymer to recognize selected item change (to update actual label)
this.operationIndex = -1;
if (this.stateObj.attributes.operation_list) {
this.operationIndex = this.stateObj.attributes.operation_list.indexOf(
this.stateObj.attributes.operation_mode
);
}
}
computeTemperatureStepSize(hass, stateObj) { computeTemperatureStepSize(hass, stateObj) {
if (stateObj.attributes.target_temp_step) { if (stateObj.attributes.target_temp_step) {
return stateObj.attributes.target_temp_step; return stateObj.attributes.target_temp_step;
@ -239,16 +225,12 @@ class MoreInfoWaterHeater extends LocalizeMixin(EventsMixin(PolymerElement)) {
this.callServiceHelper("set_away_mode", { away_mode: newVal }); this.callServiceHelper("set_away_mode", { away_mode: newVal });
} }
handleOperationmodeChanged(operationIndex) { handleOperationmodeChanged(ev) {
// Selected Option will transition to '' before transitioning to new value const oldVal = this.stateObj.attributes.operation_mode;
if (operationIndex === "" || operationIndex === -1) return; const newVal = ev.detail.value;
const operationInput = this.stateObj.attributes.operation_list[ if (!newVal || oldVal === newVal) return;
operationIndex
];
if (operationInput === this.stateObj.attributes.operation_mode) return;
this.callServiceHelper("set_operation_mode", { this.callServiceHelper("set_operation_mode", {
operation_mode: operationInput, operation_mode: newVal,
}); });
} }