Fix current mode not selected in card feature (#21063)

This commit is contained in:
Paul Bottein 2024-06-12 13:40:44 +02:00
parent 06d82a4925
commit e8eefaf1d3
No known key found for this signature in database
2 changed files with 12 additions and 31 deletions

View File

@ -1,7 +1,7 @@
export const filterModes = (
supportedModes: string[] | undefined,
selectedModes: string[] | undefined
): string[] =>
export const filterModes = <T extends string = string>(
supportedModes: T[] | undefined,
selectedModes: T[] | undefined
): T[] =>
selectedModes
? selectedModes.filter((mode) => (supportedModes || []).includes(mode))
: supportedModes || [];

View File

@ -6,7 +6,6 @@ import { styleMap } from "lit/directives/style-map";
import memoizeOne from "memoize-one";
import { computeDomain } from "../../../common/entity/compute_domain";
import { stateColorCss } from "../../../common/entity/state_color";
import { supportsFeature } from "../../../common/entity/supports-feature";
import "../../../components/ha-control-button";
import "../../../components/ha-control-button-group";
import "../../../components/ha-control-select";
@ -70,37 +69,18 @@ class HuiAlarmModeCardFeature
}
}
private _modes = memoizeOne(
(
stateObj: AlarmControlPanelEntity,
selectedModes: AlarmMode[] | undefined
) => {
if (!selectedModes) {
return [];
}
return (Object.keys(ALARM_MODES) as AlarmMode[]).filter((mode) => {
const feature = ALARM_MODES[mode].feature;
return (
(!feature || supportsFeature(stateObj, feature)) &&
selectedModes.includes(mode)
);
private _getCurrentMode = memoizeOne((stateObj: AlarmControlPanelEntity) => {
const supportedModes = supportedAlarmModes(stateObj);
return supportedModes.find((mode) => mode === stateObj.state);
});
}
);
private _getCurrentMode(stateObj: AlarmControlPanelEntity) {
return this._modes(stateObj, this._config?.modes).find(
(mode) => mode === stateObj.state
);
}
private async _valueChanged(ev: CustomEvent) {
if (!this.stateObj) return;
const mode = (ev.detail as any).value as AlarmMode;
if (mode === this.stateObj!.state) return;
if (mode === this.stateObj.state) return;
const oldMode = this._getCurrentMode(this.stateObj!);
const oldMode = this._getCurrentMode(this.stateObj);
this._currentMode = mode;
try {
@ -153,6 +133,7 @@ class HuiAlarmModeCardFeature
</ha-control-button-group>
`;
}
return html`
<div class="container">
<ha-control-select