mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 10:16:46 +00:00
Refactor options
This commit is contained in:
parent
06f653117a
commit
3d6d4bd15e
@ -2,15 +2,19 @@ import type { HassEntity } from "home-assistant-js-websocket";
|
|||||||
import type { PropertyValues } from "lit";
|
import type { PropertyValues } from "lit";
|
||||||
import { LitElement, html, nothing } from "lit";
|
import { LitElement, html, nothing } from "lit";
|
||||||
import { customElement, property, query, state } from "lit/decorators";
|
import { customElement, property, query, state } from "lit/decorators";
|
||||||
import { computeAttributeNameDisplay } from "../../common/entity/compute_attribute_display";
|
import { ensureArray } from "../../common/array/ensure-array";
|
||||||
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import type { HomeAssistant, ValueChangedEvent } from "../../types";
|
import type { HomeAssistant, ValueChangedEvent } from "../../types";
|
||||||
import "../ha-combo-box";
|
import "../ha-combo-box";
|
||||||
import type { HaComboBox } from "../ha-combo-box";
|
import type { HaComboBox } from "../ha-combo-box";
|
||||||
import { ensureArray } from "../../common/array/ensure-array";
|
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
|
||||||
|
|
||||||
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
|
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
|
||||||
|
|
||||||
|
interface AttributeOption {
|
||||||
|
value: string;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
|
|
||||||
@customElement("ha-entity-attribute-picker")
|
@customElement("ha-entity-attribute-picker")
|
||||||
class HaEntityAttributePicker extends LitElement {
|
class HaEntityAttributePicker extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -56,26 +60,29 @@ class HaEntityAttributePicker extends LitElement {
|
|||||||
changedProps.has("attribute")
|
changedProps.has("attribute")
|
||||||
) {
|
) {
|
||||||
const entityIds = this.entityId ? ensureArray(this.entityId) : [];
|
const entityIds = this.entityId ? ensureArray(this.entityId) : [];
|
||||||
const options: { value: string; label: string }[] = [];
|
const entitiesOptions = entityIds.map<AttributeOption[]>((entityId) => {
|
||||||
const attributesSet = new Set<string>();
|
|
||||||
|
|
||||||
for (const entityId of entityIds) {
|
|
||||||
const stateObj = this.hass.states[entityId];
|
const stateObj = this.hass.states[entityId];
|
||||||
|
if (!stateObj) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const attributes = Object.keys(stateObj.attributes).filter(
|
const attributes = Object.keys(stateObj.attributes).filter(
|
||||||
(a) => !this.hideAttributes?.includes(a)
|
(a) => !this.hideAttributes?.includes(a)
|
||||||
);
|
);
|
||||||
for (const a of attributes) {
|
|
||||||
if (!attributesSet.has(a)) {
|
return attributes.map((a) => ({
|
||||||
attributesSet.add(a);
|
value: a,
|
||||||
options.push({
|
label: this.hass.formatEntityAttributeName(stateObj, a),
|
||||||
value: a,
|
}));
|
||||||
label: computeAttributeNameDisplay(
|
});
|
||||||
this.hass.localize,
|
|
||||||
stateObj,
|
const options: AttributeOption[] = [];
|
||||||
this.hass.entities,
|
const optionsSet = new Set<string>();
|
||||||
a
|
for (const entityOptions of entitiesOptions) {
|
||||||
),
|
for (const option of entityOptions) {
|
||||||
});
|
if (!optionsSet.has(option.value)) {
|
||||||
|
optionsSet.add(option.value);
|
||||||
|
options.push(option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,10 +91,11 @@ class HaEntityStatePicker extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(this._comboBox as any).filteredItems = [
|
if (this.extraOptions) {
|
||||||
...(this.extraOptions ?? []),
|
options.unshift(...this.extraOptions);
|
||||||
...options,
|
}
|
||||||
];
|
|
||||||
|
(this._comboBox as any).filteredItems = options;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ export class HaServiceControl extends LitElement {
|
|||||||
).map(([key, value]) => ({
|
).map(([key, value]) => ({
|
||||||
key,
|
key,
|
||||||
...value,
|
...value,
|
||||||
selector: (value.selector || undefined) as Selector | undefined,
|
selector: value.selector as Selector | undefined,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const flatFields: Field[] = [];
|
const flatFields: Field[] = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user