ha-selector-selector implementation (#18421)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
karwosts
2023-11-29 03:09:49 -08:00
committed by GitHub
parent a5a9bcafa7
commit 0c042079ed
8 changed files with 384 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import { HassEntity } from "home-assistant-js-websocket";
import { html, LitElement, PropertyValues, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { ensureArray } from "../../common/array/ensure-array";
import { fireEvent } from "../../common/dom/fire_event";
import {
EntitySources,
fetchEntitySourcesWithCache,
@@ -37,6 +38,18 @@ export class HaEntitySelector extends LitElement {
);
}
protected willUpdate(changedProperties: PropertyValues): void {
if (changedProperties.has("selector") && this.value !== undefined) {
if (this.selector.entity?.multiple && !Array.isArray(this.value)) {
this.value = [this.value];
fireEvent(this, "value-changed", { value: this.value });
} else if (!this.selector.entity?.multiple && Array.isArray(this.value)) {
this.value = this.value[0];
fireEvent(this, "value-changed", { value: this.value });
}
}
}
protected render() {
if (this._hasIntegration(this.selector) && !this._entitySources) {
return nothing;