mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 01:36:49 +00:00
Convert entity picker to ha-combo (#11560)
* Convert entity picker to ha-combo * Update ha-entity-picker.ts * Handle empty better * Clear value when no device/area/entity
This commit is contained in:
parent
d05f807b9d
commit
76af6e48cd
@ -38,7 +38,7 @@ export type HaDevicePickerDeviceFilterFunc = (
|
|||||||
) => boolean;
|
) => boolean;
|
||||||
|
|
||||||
const rowRenderer: ComboBoxLitRenderer<Device> = (item) => html`<mwc-list-item
|
const rowRenderer: ComboBoxLitRenderer<Device> = (item) => html`<mwc-list-item
|
||||||
twoline
|
.twoline=${!!item.area}
|
||||||
>
|
>
|
||||||
<span>${item.name}</span>
|
<span>${item.name}</span>
|
||||||
<span slot="secondary">${item.area}</span>
|
<span slot="secondary">${item.area}</span>
|
||||||
@ -105,7 +105,7 @@ export class HaDevicePicker extends SubscribeMixin(LitElement) {
|
|||||||
if (!devices.length) {
|
if (!devices.length) {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
id: "",
|
id: "no_devices",
|
||||||
area: "",
|
area: "",
|
||||||
name: this.hass.localize("ui.components.device-picker.no_devices"),
|
name: this.hass.localize("ui.components.device-picker.no_devices"),
|
||||||
},
|
},
|
||||||
@ -201,7 +201,7 @@ export class HaDevicePicker extends SubscribeMixin(LitElement) {
|
|||||||
if (!outputDevices.length) {
|
if (!outputDevices.length) {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
id: "",
|
id: "no_devices",
|
||||||
area: "",
|
area: "",
|
||||||
name: this.hass.localize("ui.components.device-picker.no_match"),
|
name: this.hass.localize("ui.components.device-picker.no_match"),
|
||||||
},
|
},
|
||||||
@ -270,7 +270,6 @@ export class HaDevicePicker extends SubscribeMixin(LitElement) {
|
|||||||
.renderer=${rowRenderer}
|
.renderer=${rowRenderer}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
item-value-path="id"
|
item-value-path="id"
|
||||||
item-id-path="id"
|
|
||||||
item-label-path="name"
|
item-label-path="name"
|
||||||
@opened-changed=${this._openedChanged}
|
@opened-changed=${this._openedChanged}
|
||||||
@value-changed=${this._deviceChanged}
|
@value-changed=${this._deviceChanged}
|
||||||
@ -284,7 +283,11 @@ export class HaDevicePicker extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
private _deviceChanged(ev: PolymerChangedEvent<string>) {
|
private _deviceChanged(ev: PolymerChangedEvent<string>) {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
const newValue = ev.detail.value;
|
let newValue = ev.detail.value;
|
||||||
|
|
||||||
|
if (newValue === "no_devices") {
|
||||||
|
newValue = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (newValue !== this._value) {
|
if (newValue !== this._value) {
|
||||||
this._setValue(newValue);
|
this._setValue(newValue);
|
||||||
|
@ -1,25 +1,16 @@
|
|||||||
import { mdiCheck, mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js";
|
import "@material/mwc-list/mwc-list-item";
|
||||||
import "@polymer/paper-input/paper-input";
|
|
||||||
import "@polymer/paper-item/paper-icon-item";
|
|
||||||
import "@polymer/paper-item/paper-item-body";
|
|
||||||
import "@vaadin/combo-box/theme/material/vaadin-combo-box-light";
|
|
||||||
import { HassEntity } from "home-assistant-js-websocket";
|
import { HassEntity } from "home-assistant-js-websocket";
|
||||||
import {
|
import { html, LitElement, PropertyValues, TemplateResult } from "lit";
|
||||||
css,
|
import { ComboBoxLitRenderer } from "lit-vaadin-helpers";
|
||||||
CSSResultGroup,
|
import { customElement, property, query, state } from "lit/decorators";
|
||||||
html,
|
|
||||||
LitElement,
|
|
||||||
PropertyValues,
|
|
||||||
TemplateResult,
|
|
||||||
} from "lit";
|
|
||||||
import { ComboBoxLitRenderer, comboBoxRenderer } from "lit-vaadin-helpers";
|
|
||||||
import { customElement, property, query } from "lit/decorators";
|
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { computeDomain } from "../../common/entity/compute_domain";
|
import { computeDomain } from "../../common/entity/compute_domain";
|
||||||
import { computeStateName } from "../../common/entity/compute_state_name";
|
import { computeStateName } from "../../common/entity/compute_state_name";
|
||||||
import { PolymerChangedEvent } from "../../polymer-types";
|
import { PolymerChangedEvent } from "../../polymer-types";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
|
import "../ha-combo-box";
|
||||||
|
import type { HaComboBox } from "../ha-combo-box";
|
||||||
import "../ha-icon-button";
|
import "../ha-icon-button";
|
||||||
import "../ha-svg-icon";
|
import "../ha-svg-icon";
|
||||||
import "./state-badge";
|
import "./state-badge";
|
||||||
@ -27,35 +18,15 @@ import "./state-badge";
|
|||||||
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
|
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
|
||||||
|
|
||||||
// eslint-disable-next-line lit/prefer-static-styles
|
// eslint-disable-next-line lit/prefer-static-styles
|
||||||
const rowRenderer: ComboBoxLitRenderer<HassEntity> = (item) => html`<style>
|
const rowRenderer: ComboBoxLitRenderer<HassEntity & { friendly_name: string }> =
|
||||||
paper-icon-item {
|
(item) =>
|
||||||
padding: 0;
|
html`<mwc-list-item graphic="avatar" .twoline=${!!item.entity_id}>
|
||||||
margin: -8px;
|
${item.state
|
||||||
}
|
? html`<state-badge slot="graphic" .stateObj=${item}></state-badge>`
|
||||||
#content {
|
: ""}
|
||||||
display: flex;
|
<span>${item.friendly_name}</span>
|
||||||
align-items: center;
|
<span slot="secondary">${item.entity_id}</span>
|
||||||
}
|
</mwc-list-item>`;
|
||||||
ha-svg-icon {
|
|
||||||
padding-left: 2px;
|
|
||||||
color: var(--secondary-text-color);
|
|
||||||
}
|
|
||||||
:host(:not([selected])) ha-svg-icon {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
:host([selected]) paper-icon-item {
|
|
||||||
margin-left: 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<ha-svg-icon .path=${mdiCheck}></ha-svg-icon>
|
|
||||||
<paper-icon-item>
|
|
||||||
<state-badge slot="item-icon" .stateObj=${item}></state-badge>
|
|
||||||
<paper-item-body two-line="">
|
|
||||||
${computeStateName(item)}
|
|
||||||
<span secondary>${item.entity_id}</span>
|
|
||||||
</paper-item-body>
|
|
||||||
</paper-icon-item>`;
|
|
||||||
|
|
||||||
@customElement("ha-entity-picker")
|
@customElement("ha-entity-picker")
|
||||||
export class HaEntityPicker extends LitElement {
|
export class HaEntityPicker extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -107,19 +78,19 @@ export class HaEntityPicker extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public hideClearIcon = false;
|
@property({ type: Boolean }) public hideClearIcon = false;
|
||||||
|
|
||||||
@property({ type: Boolean }) private _opened = false;
|
@state() private _opened = false;
|
||||||
|
|
||||||
@query("vaadin-combo-box-light", true) private comboBox!: HTMLElement;
|
@query("ha-combo-box", true) public comboBox!: HaComboBox;
|
||||||
|
|
||||||
public open() {
|
public open() {
|
||||||
this.updateComplete.then(() => {
|
this.updateComplete.then(() => {
|
||||||
(this.shadowRoot?.querySelector("vaadin-combo-box-light") as any)?.open();
|
this.comboBox?.open();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
this.updateComplete.then(() => {
|
this.updateComplete.then(() => {
|
||||||
this.shadowRoot?.querySelector("paper-input")?.focus();
|
this.comboBox?.focus();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,6 +115,27 @@ export class HaEntityPicker extends LitElement {
|
|||||||
}
|
}
|
||||||
let entityIds = Object.keys(hass.states);
|
let entityIds = Object.keys(hass.states);
|
||||||
|
|
||||||
|
if (!entityIds.length) {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
entity_id: "",
|
||||||
|
state: "",
|
||||||
|
last_changed: "",
|
||||||
|
last_updated: "",
|
||||||
|
context: { id: "", user_id: null },
|
||||||
|
friendly_name: this.hass!.localize(
|
||||||
|
"ui.components.entity.entity-picker.no_entities"
|
||||||
|
),
|
||||||
|
attributes: {
|
||||||
|
friendly_name: this.hass!.localize(
|
||||||
|
"ui.components.entity.entity-picker.no_entities"
|
||||||
|
),
|
||||||
|
icon: "mdi:magnify",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
if (includeDomains) {
|
if (includeDomains) {
|
||||||
entityIds = entityIds.filter((eid) =>
|
entityIds = entityIds.filter((eid) =>
|
||||||
includeDomains.includes(computeDomain(eid))
|
includeDomains.includes(computeDomain(eid))
|
||||||
@ -156,7 +148,10 @@ export class HaEntityPicker extends LitElement {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
states = entityIds.sort().map((key) => hass!.states[key]);
|
states = entityIds.sort().map((key) => ({
|
||||||
|
...hass!.states[key],
|
||||||
|
friendly_name: computeStateName(hass!.states[key]) || key,
|
||||||
|
}));
|
||||||
|
|
||||||
if (includeDeviceClasses) {
|
if (includeDeviceClasses) {
|
||||||
states = states.filter(
|
states = states.filter(
|
||||||
@ -196,6 +191,9 @@ export class HaEntityPicker extends LitElement {
|
|||||||
last_changed: "",
|
last_changed: "",
|
||||||
last_updated: "",
|
last_updated: "",
|
||||||
context: { id: "", user_id: null },
|
context: { id: "", user_id: null },
|
||||||
|
friendly_name: this.hass!.localize(
|
||||||
|
"ui.components.entity.entity-picker.no_match"
|
||||||
|
),
|
||||||
attributes: {
|
attributes: {
|
||||||
friendly_name: this.hass!.localize(
|
friendly_name: this.hass!.localize(
|
||||||
"ui.components.entity.entity-picker.no_match"
|
"ui.components.entity.entity-picker.no_match"
|
||||||
@ -241,64 +239,25 @@ export class HaEntityPicker extends LitElement {
|
|||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<vaadin-combo-box-light
|
<ha-combo-box
|
||||||
item-value-path="entity_id"
|
item-value-path="entity_id"
|
||||||
item-label-path="entity_id"
|
item-label-path="friendly_name"
|
||||||
|
.hass=${this.hass}
|
||||||
.value=${this._value}
|
.value=${this._value}
|
||||||
|
.label=${this.label === undefined
|
||||||
|
? this.hass.localize("ui.components.entity.entity-picker.entity")
|
||||||
|
: this.label}
|
||||||
.allowCustomValue=${this.allowCustomEntity}
|
.allowCustomValue=${this.allowCustomEntity}
|
||||||
.filteredItems=${this._states}
|
.filteredItems=${this._states}
|
||||||
${comboBoxRenderer(rowRenderer)}
|
.renderer=${rowRenderer}
|
||||||
@opened-changed=${this._openedChanged}
|
@opened-changed=${this._openedChanged}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._valueChanged}
|
||||||
@filter-changed=${this._filterChanged}
|
@filter-changed=${this._filterChanged}
|
||||||
>
|
>
|
||||||
<paper-input
|
</ha-combo-box>
|
||||||
.autofocus=${this.autofocus}
|
|
||||||
.label=${this.label === undefined
|
|
||||||
? this.hass.localize("ui.components.entity.entity-picker.entity")
|
|
||||||
: this.label}
|
|
||||||
.disabled=${this.disabled}
|
|
||||||
class="input"
|
|
||||||
autocapitalize="none"
|
|
||||||
autocomplete="off"
|
|
||||||
autocorrect="off"
|
|
||||||
spellcheck="false"
|
|
||||||
>
|
|
||||||
<div class="suffix" slot="suffix">
|
|
||||||
${this.value && !this.hideClearIcon
|
|
||||||
? html`
|
|
||||||
<ha-icon-button
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
"ui.components.entity.entity-picker.clear"
|
|
||||||
)}
|
|
||||||
.path=${mdiClose}
|
|
||||||
class="clear-button"
|
|
||||||
tabindex="-1"
|
|
||||||
@click=${this._clearValue}
|
|
||||||
no-ripple
|
|
||||||
></ha-icon-button>
|
|
||||||
`
|
|
||||||
: ""}
|
|
||||||
|
|
||||||
<ha-icon-button
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
"ui.components.entity.entity-picker.show_entities"
|
|
||||||
)}
|
|
||||||
.path=${this._opened ? mdiMenuUp : mdiMenuDown}
|
|
||||||
class="toggle-button"
|
|
||||||
tabindex="-1"
|
|
||||||
></ha-icon-button>
|
|
||||||
</div>
|
|
||||||
</paper-input>
|
|
||||||
</vaadin-combo-box-light>
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _clearValue(ev: Event) {
|
|
||||||
ev.stopPropagation();
|
|
||||||
this._setValue("");
|
|
||||||
}
|
|
||||||
|
|
||||||
private get _value() {
|
private get _value() {
|
||||||
return this.value || "";
|
return this.value || "";
|
||||||
}
|
}
|
||||||
@ -308,6 +267,7 @@ export class HaEntityPicker extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: PolymerChangedEvent<string>) {
|
private _valueChanged(ev: PolymerChangedEvent<string>) {
|
||||||
|
ev.stopPropagation();
|
||||||
const newValue = ev.detail.value;
|
const newValue = ev.detail.value;
|
||||||
if (newValue !== this._value) {
|
if (newValue !== this._value) {
|
||||||
this._setValue(newValue);
|
this._setValue(newValue);
|
||||||
@ -317,9 +277,9 @@ export class HaEntityPicker extends LitElement {
|
|||||||
private _filterChanged(ev: CustomEvent): void {
|
private _filterChanged(ev: CustomEvent): void {
|
||||||
const filterString = ev.detail.value.toLowerCase();
|
const filterString = ev.detail.value.toLowerCase();
|
||||||
(this.comboBox as any).filteredItems = this._states.filter(
|
(this.comboBox as any).filteredItems = this._states.filter(
|
||||||
(state) =>
|
(entityState) =>
|
||||||
state.entity_id.toLowerCase().includes(filterString) ||
|
entityState.entity_id.toLowerCase().includes(filterString) ||
|
||||||
computeStateName(state).toLowerCase().includes(filterString)
|
computeStateName(entityState).toLowerCase().includes(filterString)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,22 +290,6 @@ export class HaEntityPicker extends LitElement {
|
|||||||
fireEvent(this, "change");
|
fireEvent(this, "change");
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
|
||||||
return css`
|
|
||||||
.suffix {
|
|
||||||
display: flex;
|
|
||||||
}
|
|
||||||
ha-icon-button {
|
|
||||||
--mdc-icon-button-size: 24px;
|
|
||||||
padding: 0px 2px;
|
|
||||||
color: var(--secondary-text-color);
|
|
||||||
}
|
|
||||||
[hidden] {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
@ -67,7 +67,7 @@ export class HaStatisticPicker extends LitElement {
|
|||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
state?: HassEntity;
|
state?: HassEntity;
|
||||||
}> = (item) => html` <mwc-list-item graphic="avatar" twoline>
|
}> = (item) => html`<mwc-list-item graphic="avatar" twoline>
|
||||||
${item.state
|
${item.state
|
||||||
? html`<state-badge slot="graphic" .stateObj=${item.state}></state-badge>`
|
? html`<state-badge slot="graphic" .stateObj=${item.state}></state-badge>`
|
||||||
: ""}
|
: ""}
|
||||||
|
@ -139,7 +139,7 @@ export class HaAreaPicker extends SubscribeMixin(LitElement) {
|
|||||||
if (!areas.length) {
|
if (!areas.length) {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
area_id: "",
|
area_id: "no_areas",
|
||||||
name: this.hass.localize("ui.components.area-picker.no_areas"),
|
name: this.hass.localize("ui.components.area-picker.no_areas"),
|
||||||
picture: null,
|
picture: null,
|
||||||
},
|
},
|
||||||
@ -263,7 +263,7 @@ export class HaAreaPicker extends SubscribeMixin(LitElement) {
|
|||||||
if (!outputAreas.length) {
|
if (!outputAreas.length) {
|
||||||
outputAreas = [
|
outputAreas = [
|
||||||
{
|
{
|
||||||
area_id: "",
|
area_id: "no_areas",
|
||||||
name: this.hass.localize("ui.components.area-picker.no_match"),
|
name: this.hass.localize("ui.components.area-picker.no_match"),
|
||||||
picture: null,
|
picture: null,
|
||||||
},
|
},
|
||||||
@ -369,7 +369,11 @@ export class HaAreaPicker extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
private _areaChanged(ev: PolymerChangedEvent<string>) {
|
private _areaChanged(ev: PolymerChangedEvent<string>) {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
const newValue = ev.detail.value;
|
let newValue = ev.detail.value;
|
||||||
|
|
||||||
|
if (newValue === "no_areas") {
|
||||||
|
newValue = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (!["add_new_suggestion", "add_new"].includes(newValue)) {
|
if (!["add_new_suggestion", "add_new"].includes(newValue)) {
|
||||||
if (newValue !== this._value) {
|
if (newValue !== this._value) {
|
||||||
|
@ -31,11 +31,7 @@ export class HaSelector extends LitElement {
|
|||||||
@property({ type: Boolean }) public disabled = false;
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
const input = this.shadowRoot!.getElementById("selector");
|
this.shadowRoot!.getElementById("selector")?.focus();
|
||||||
if (!input) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
(input as HTMLElement).focus();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private get _type() {
|
private get _type() {
|
||||||
|
@ -358,6 +358,7 @@
|
|||||||
"entity": "Entity",
|
"entity": "Entity",
|
||||||
"edit": "Edit",
|
"edit": "Edit",
|
||||||
"clear": "Clear",
|
"clear": "Clear",
|
||||||
|
"no_entities": "You don't have any entities",
|
||||||
"no_match": "No matching entities found",
|
"no_match": "No matching entities found",
|
||||||
"show_entities": "Show entities"
|
"show_entities": "Show entities"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user