Fix empty value for state picker (#13699)

This commit is contained in:
Paul Bottein 2022-09-12 15:53:18 +02:00 committed by Bram Kragten
parent 432483b3d2
commit 032f497687
No known key found for this signature in database
GPG Key ID: FBE2DFDB363EF55B

View File

@ -8,6 +8,7 @@ import { HomeAssistant } from "../../types";
import "../ha-combo-box";
import type { HaComboBox } from "../ha-combo-box";
import { formatAttributeValue } from "../../data/entity_attributes";
import { fireEvent } from "../../common/dom/fire_event";
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
@ -70,7 +71,7 @@ class HaEntityStatePicker extends LitElement {
return html`
<ha-combo-box
.hass=${this.hass}
.value=${this.value}
.value=${this._value}
.autofocus=${this.autofocus}
.label=${this.label ??
this.hass.localize("ui.components.entity.entity-state-picker.state")}
@ -87,12 +88,28 @@ class HaEntityStatePicker extends LitElement {
`;
}
private get _value() {
return this.value || "";
}
private _openedChanged(ev: PolymerChangedEvent<boolean>) {
this._opened = ev.detail.value;
}
private _valueChanged(ev: PolymerChangedEvent<string>) {
this.value = ev.detail.value;
ev.stopPropagation();
const newValue = ev.detail.value;
if (newValue !== this._value) {
this._setValue(newValue);
}
}
private _setValue(value: string) {
this.value = value;
setTimeout(() => {
fireEvent(this, "value-changed", { value });
fireEvent(this, "change");
}, 0);
}
}