mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Improve action picker UI and search (#25525)
This commit is contained in:
parent
06270c771f
commit
61d9b0d2a3
4
src/common/entity/valid_service_id.ts
Normal file
4
src/common/entity/valid_service_id.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
const validServiceId = /^(\w+)\.(\w+)$/;
|
||||||
|
|
||||||
|
export const isValidServiceId = (actionId: string) =>
|
||||||
|
validServiceId.test(actionId);
|
@ -51,6 +51,9 @@ export class HaEntityPicker extends LitElement {
|
|||||||
@property({ type: Boolean, attribute: "allow-custom-entity" })
|
@property({ type: Boolean, attribute: "allow-custom-entity" })
|
||||||
public allowCustomEntity;
|
public allowCustomEntity;
|
||||||
|
|
||||||
|
@property({ type: Boolean, attribute: "show-entity-id" })
|
||||||
|
public showEntityId = false;
|
||||||
|
|
||||||
@property() public label?: string;
|
@property() public label?: string;
|
||||||
|
|
||||||
@property() public value?: string;
|
@property() public value?: string;
|
||||||
@ -166,11 +169,15 @@ export class HaEntityPicker extends LitElement {
|
|||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private get _showEntityId() {
|
||||||
|
return this.showEntityId || this.hass.userData?.showEntityIdPicker;
|
||||||
|
}
|
||||||
|
|
||||||
private _rowRenderer: ComboBoxLitRenderer<EntityComboBoxItem> = (
|
private _rowRenderer: ComboBoxLitRenderer<EntityComboBoxItem> = (
|
||||||
item,
|
item,
|
||||||
{ index }
|
{ index }
|
||||||
) => {
|
) => {
|
||||||
const showEntityId = this.hass.userData?.showEntityIdPicker;
|
const showEntityId = this._showEntityId;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-combo-box-item type="button" compact .borderTop=${index !== 0}>
|
<ha-combo-box-item type="button" compact .borderTop=${index !== 0}>
|
||||||
|
@ -85,8 +85,11 @@ export class HaServiceControl extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public narrow = false;
|
@property({ type: Boolean }) public narrow = false;
|
||||||
|
|
||||||
@property({ attribute: "show-advanced", type: Boolean }) public showAdvanced =
|
@property({ attribute: "show-advanced", type: Boolean })
|
||||||
false;
|
public showAdvanced = false;
|
||||||
|
|
||||||
|
@property({ attribute: "show-service-id", type: Boolean })
|
||||||
|
public showServiceId = false;
|
||||||
|
|
||||||
@property({ attribute: "hide-picker", type: Boolean, reflect: true })
|
@property({ attribute: "hide-picker", type: Boolean, reflect: true })
|
||||||
public hidePicker = false;
|
public hidePicker = false;
|
||||||
@ -435,6 +438,7 @@ export class HaServiceControl extends LitElement {
|
|||||||
.value=${this._value?.action}
|
.value=${this._value?.action}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@value-changed=${this._serviceChanged}
|
@value-changed=${this._serviceChanged}
|
||||||
|
.showServiceId=${this.showServiceId}
|
||||||
></ha-service-picker>`}
|
></ha-service-picker>`}
|
||||||
${this.hideDescription
|
${this.hideDescription
|
||||||
? nothing
|
? nothing
|
||||||
|
@ -1,15 +1,25 @@
|
|||||||
|
import { mdiRoomService } from "@mdi/js";
|
||||||
import type { ComboBoxLitRenderer } from "@vaadin/combo-box/lit";
|
import type { ComboBoxLitRenderer } from "@vaadin/combo-box/lit";
|
||||||
import { html, LitElement } from "lit";
|
import { html, LitElement, nothing, type TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
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 { isValidServiceId } from "../common/entity/valid_service_id";
|
||||||
import type { LocalizeFunc } from "../common/translations/localize";
|
import type { LocalizeFunc } from "../common/translations/localize";
|
||||||
import { domainToName } from "../data/integration";
|
|
||||||
import type { HomeAssistant } from "../types";
|
|
||||||
import "./ha-combo-box";
|
|
||||||
import "./ha-combo-box-item";
|
|
||||||
import "./ha-service-icon";
|
|
||||||
import { getServiceIcons } from "../data/icons";
|
import { getServiceIcons } from "../data/icons";
|
||||||
|
import { domainToName } from "../data/integration";
|
||||||
|
import type { HomeAssistant, ValueChangedEvent } from "../types";
|
||||||
|
import "./ha-combo-box-item";
|
||||||
|
import "./ha-generic-picker";
|
||||||
|
import type { HaGenericPicker } from "./ha-generic-picker";
|
||||||
|
import type { PickerComboBoxItem } from "./ha-picker-combo-box";
|
||||||
|
import type { PickerValueRenderer } from "./ha-picker-field";
|
||||||
|
import "./ha-service-icon";
|
||||||
|
|
||||||
|
interface ServiceComboBoxItem extends PickerComboBoxItem {
|
||||||
|
domain_name?: string;
|
||||||
|
service_id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
@customElement("ha-service-picker")
|
@customElement("ha-service-picker")
|
||||||
class HaServicePicker extends LitElement {
|
class HaServicePicker extends LitElement {
|
||||||
@ -17,66 +27,121 @@ class HaServicePicker extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public disabled = false;
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
|
@property() public label?: string;
|
||||||
|
|
||||||
|
@property() public placeholder?: string;
|
||||||
|
|
||||||
@property() public value?: string;
|
@property() public value?: string;
|
||||||
|
|
||||||
@state() private _filter?: string;
|
@property({ attribute: "show-service-id", type: Boolean })
|
||||||
|
public showServiceId = false;
|
||||||
|
|
||||||
protected willUpdate() {
|
@query("ha-generic-picker") private _picker?: HaGenericPicker;
|
||||||
if (!this.hasUpdated) {
|
|
||||||
this.hass.loadBackendTranslation("services");
|
public async open() {
|
||||||
getServiceIcons(this.hass);
|
await this.updateComplete;
|
||||||
}
|
await this._picker?.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _rowRenderer: ComboBoxLitRenderer<{ service: string; name: string }> =
|
protected firstUpdated(props) {
|
||||||
(item) => html`
|
super.firstUpdated(props);
|
||||||
<ha-combo-box-item type="button">
|
this.hass.loadBackendTranslation("services");
|
||||||
<ha-service-icon
|
getServiceIcons(this.hass);
|
||||||
slot="start"
|
}
|
||||||
.hass=${this.hass}
|
|
||||||
.service=${item.service}
|
|
||||||
></ha-service-icon>
|
|
||||||
<span slot="headline">${item.name}</span>
|
|
||||||
<span slot="supporting-text"
|
|
||||||
>${item.name === item.service ? "" : item.service}</span
|
|
||||||
>
|
|
||||||
</ha-combo-box-item>
|
|
||||||
`;
|
|
||||||
|
|
||||||
protected render() {
|
private _rowRenderer: ComboBoxLitRenderer<ServiceComboBoxItem> = (
|
||||||
return html`
|
item,
|
||||||
<ha-combo-box
|
{ index }
|
||||||
|
) => html`
|
||||||
|
<ha-combo-box-item type="button" border-top .borderTop=${index !== 0}>
|
||||||
|
<ha-service-icon
|
||||||
|
slot="start"
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.label=${this.hass.localize("ui.components.service-picker.action")}
|
.service=${item.id}
|
||||||
.filteredItems=${this._filteredServices(
|
></ha-service-icon>
|
||||||
this.hass.localize,
|
<span slot="headline">${item.primary}</span>
|
||||||
this.hass.services,
|
<span slot="supporting-text">${item.secondary}</span>
|
||||||
this._filter
|
${item.service_id && this.showServiceId
|
||||||
)}
|
? html`<span slot="supporting-text" class="code">
|
||||||
.value=${this.value}
|
${item.service_id}
|
||||||
.disabled=${this.disabled}
|
</span>`
|
||||||
.renderer=${this._rowRenderer}
|
: nothing}
|
||||||
item-value-path="service"
|
${item.domain_name
|
||||||
item-label-path="name"
|
? html`
|
||||||
|
<div slot="trailing-supporting-text" class="domain">
|
||||||
|
${item.domain_name}
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
: nothing}
|
||||||
|
</ha-combo-box-item>
|
||||||
|
`;
|
||||||
|
|
||||||
|
private _valueRenderer: PickerValueRenderer = (value) => {
|
||||||
|
const serviceId = value;
|
||||||
|
const [domain, service] = serviceId.split(".");
|
||||||
|
|
||||||
|
if (!this.hass.services[domain]?.[service]) {
|
||||||
|
return html`
|
||||||
|
<ha-svg-icon slot="start" .path=${mdiRoomService}></ha-svg-icon>
|
||||||
|
<span slot="headline">${value}</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const serviceName =
|
||||||
|
this.hass.localize(`component.${domain}.services.${service}.name`) ||
|
||||||
|
this.hass.services[domain][service].name ||
|
||||||
|
service;
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<ha-service-icon
|
||||||
|
slot="start"
|
||||||
|
.hass=${this.hass}
|
||||||
|
.service=${serviceId}
|
||||||
|
></ha-service-icon>
|
||||||
|
<span slot="headline">${serviceName}</span>
|
||||||
|
${this.showServiceId
|
||||||
|
? html`<span slot="supporting-text" class="code">${serviceId}</span>`
|
||||||
|
: nothing}
|
||||||
|
`;
|
||||||
|
};
|
||||||
|
|
||||||
|
protected render(): TemplateResult {
|
||||||
|
const placeholder =
|
||||||
|
this.placeholder ??
|
||||||
|
this.hass.localize("ui.components.service-picker.action");
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<ha-generic-picker
|
||||||
|
.hass=${this.hass}
|
||||||
|
.autofocus=${this.autofocus}
|
||||||
allow-custom-value
|
allow-custom-value
|
||||||
@filter-changed=${this._filterChanged}
|
.notFoundLabel=${this.hass.localize(
|
||||||
|
"ui.components.service-picker.no_match"
|
||||||
|
)}
|
||||||
|
.label=${this.label}
|
||||||
|
.placeholder=${placeholder}
|
||||||
|
.value=${this.value}
|
||||||
|
.getItems=${this._getItems}
|
||||||
|
.rowRenderer=${this._rowRenderer}
|
||||||
|
.valueRenderer=${this._valueRenderer}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._valueChanged}
|
||||||
></ha-combo-box>
|
>
|
||||||
|
</ha-generic-picker>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _getItems = () =>
|
||||||
|
this._services(this.hass.localize, this.hass.services);
|
||||||
|
|
||||||
private _services = memoizeOne(
|
private _services = memoizeOne(
|
||||||
(
|
(
|
||||||
localize: LocalizeFunc,
|
localize: LocalizeFunc,
|
||||||
services: HomeAssistant["services"]
|
services: HomeAssistant["services"]
|
||||||
): {
|
): ServiceComboBoxItem[] => {
|
||||||
service: string;
|
|
||||||
name: string;
|
|
||||||
}[] => {
|
|
||||||
if (!services) {
|
if (!services) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const result: { service: string; name: string }[] = [];
|
const items: ServiceComboBoxItem[] = [];
|
||||||
|
|
||||||
Object.keys(services)
|
Object.keys(services)
|
||||||
.sort()
|
.sort()
|
||||||
@ -84,56 +149,60 @@ class HaServicePicker extends LitElement {
|
|||||||
const services_keys = Object.keys(services[domain]).sort();
|
const services_keys = Object.keys(services[domain]).sort();
|
||||||
|
|
||||||
for (const service of services_keys) {
|
for (const service of services_keys) {
|
||||||
result.push({
|
const serviceId = `${domain}.${service}`;
|
||||||
service: `${domain}.${service}`,
|
const domainName = domainToName(localize, domain);
|
||||||
name: `${domainToName(localize, domain)}: ${
|
|
||||||
this.hass.localize(
|
const name =
|
||||||
`component.${domain}.services.${service}.name`
|
this.hass.localize(
|
||||||
) ||
|
`component.${domain}.services.${service}.name`
|
||||||
services[domain][service].name ||
|
) ||
|
||||||
service
|
services[domain][service].name ||
|
||||||
}`,
|
service;
|
||||||
|
|
||||||
|
const description =
|
||||||
|
this.hass.localize(
|
||||||
|
`component.${domain}.services.${service}.description`
|
||||||
|
) || services[domain][service].description;
|
||||||
|
|
||||||
|
items.push({
|
||||||
|
id: serviceId,
|
||||||
|
primary: name,
|
||||||
|
secondary: description,
|
||||||
|
domain_name: domainName,
|
||||||
|
service_id: serviceId,
|
||||||
|
search_labels: [serviceId, domainName, name, description].filter(
|
||||||
|
Boolean
|
||||||
|
),
|
||||||
|
sorting_label: serviceId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return items;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
private _filteredServices = memoizeOne(
|
private _valueChanged(ev: ValueChangedEvent<string>) {
|
||||||
(
|
ev.stopPropagation();
|
||||||
localize: LocalizeFunc,
|
const value = ev.detail.value;
|
||||||
services: HomeAssistant["services"],
|
|
||||||
filter?: string
|
|
||||||
) => {
|
|
||||||
if (!services) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
const processedServices = this._services(localize, services);
|
|
||||||
|
|
||||||
if (!filter) {
|
if (!value) {
|
||||||
return processedServices;
|
this._setValue(undefined);
|
||||||
}
|
return;
|
||||||
const split_filter = filter.split(" ");
|
|
||||||
return processedServices.filter((service) => {
|
|
||||||
const lower_service_name = service.name.toLowerCase();
|
|
||||||
const lower_service = service.service.toLowerCase();
|
|
||||||
return split_filter.every(
|
|
||||||
(f) => lower_service_name.includes(f) || lower_service.includes(f)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
private _filterChanged(ev: CustomEvent): void {
|
if (!isValidServiceId(value)) {
|
||||||
this._filter = ev.detail.value.toLowerCase();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._setValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev) {
|
private _setValue(value: string | undefined) {
|
||||||
this.value = ev.detail.value;
|
this.value = value;
|
||||||
|
|
||||||
|
fireEvent(this, "value-changed", { value });
|
||||||
fireEvent(this, "change");
|
fireEvent(this, "change");
|
||||||
fireEvent(this, "value-changed", { value: this.value });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,6 +142,7 @@ class HaPanelDevAction extends LitElement {
|
|||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.value=${this._serviceData?.action}
|
.value=${this._serviceData?.action}
|
||||||
@value-changed=${this._serviceChanged}
|
@value-changed=${this._serviceChanged}
|
||||||
|
show-service-id
|
||||||
></ha-service-picker>
|
></ha-service-picker>
|
||||||
<ha-yaml-editor
|
<ha-yaml-editor
|
||||||
id="yaml-editor"
|
id="yaml-editor"
|
||||||
@ -156,6 +157,7 @@ class HaPanelDevAction extends LitElement {
|
|||||||
.value=${this._serviceData}
|
.value=${this._serviceData}
|
||||||
.narrow=${this.narrow}
|
.narrow=${this.narrow}
|
||||||
show-advanced
|
show-advanced
|
||||||
|
show-service-id
|
||||||
@value-changed=${this._serviceDataChanged}
|
@value-changed=${this._serviceDataChanged}
|
||||||
class="card-content"
|
class="card-content"
|
||||||
></ha-service-control>
|
></ha-service-control>
|
||||||
|
@ -130,6 +130,7 @@ class HaPanelDevState extends LitElement {
|
|||||||
.value=${this._entityId}
|
.value=${this._entityId}
|
||||||
@value-changed=${this._entityIdChanged}
|
@value-changed=${this._entityIdChanged}
|
||||||
allow-custom-entity
|
allow-custom-entity
|
||||||
|
show-entity-id
|
||||||
></ha-entity-picker>
|
></ha-entity-picker>
|
||||||
${this._entityId
|
${this._entityId
|
||||||
? html`
|
? html`
|
||||||
|
@ -873,7 +873,8 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"service-picker": {
|
"service-picker": {
|
||||||
"action": "Action"
|
"action": "Action",
|
||||||
|
"no_match": "No matching actions found"
|
||||||
},
|
},
|
||||||
"service-control": {
|
"service-control": {
|
||||||
"required": "This field is required",
|
"required": "This field is required",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user