mirror of
https://github.com/home-assistant/frontend.git
synced 2026-12-02 17:50:19 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd3bc90edd | ||
|
|
d04ea5c231 |
No files matched your search
@@ -18,6 +18,67 @@ import "../ha-generic-picker";
|
||||
import type { PickerComboBoxItem } from "../ha-picker-combo-box";
|
||||
import type { PickerValueRenderer } from "../ha-picker-field";
|
||||
|
||||
export const computeEntityStateItems = (
|
||||
states: HassEntities | null | undefined,
|
||||
formatters: HomeAssistantFormatters,
|
||||
language: string,
|
||||
entityId: string | string[] | undefined,
|
||||
attribute: string | undefined,
|
||||
hideStates: string[] | undefined,
|
||||
extraOptions: PickerComboBoxItem[] | undefined
|
||||
): PickerComboBoxItem[] => {
|
||||
const entityIds = entityId ? ensureArray(entityId) : [];
|
||||
|
||||
const entitiesOptions = entityIds.map<PickerComboBoxItem[]>(
|
||||
(entityIdItem) => {
|
||||
const stateObj =
|
||||
states?.[entityIdItem] ||
|
||||
({ entity_id: entityIdItem, attributes: {} } as HassEntity);
|
||||
|
||||
const options = getStates(
|
||||
states ?? {},
|
||||
language,
|
||||
stateObj,
|
||||
attribute
|
||||
).filter((s) => !hideStates?.includes(s));
|
||||
|
||||
return options
|
||||
.map((s) => {
|
||||
const primary = attribute
|
||||
? formatters.formatEntityAttributeValue(stateObj, attribute, s)
|
||||
: formatters.formatEntityState(stateObj, s);
|
||||
return {
|
||||
id: s,
|
||||
primary,
|
||||
sorting_label: primary,
|
||||
};
|
||||
})
|
||||
.filter((option) => option.id && option.primary);
|
||||
}
|
||||
);
|
||||
|
||||
const options: PickerComboBoxItem[] = [];
|
||||
const optionsSet = new Set<string>();
|
||||
for (const entityOptions of entitiesOptions) {
|
||||
for (const option of entityOptions) {
|
||||
if (!optionsSet.has(option.id)) {
|
||||
optionsSet.add(option.id);
|
||||
options.push(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (extraOptions) {
|
||||
// Filter out any extraOptions with empty primary or id fields
|
||||
const validExtraOptions = extraOptions.filter(
|
||||
(option) => option.id && option.primary
|
||||
);
|
||||
options.unshift(...validExtraOptions);
|
||||
}
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
@customElement("ha-entity-state-picker")
|
||||
export class HaEntityStatePicker extends LitElement {
|
||||
@property({ attribute: false }) public entityId?: string | string[];
|
||||
@@ -60,68 +121,7 @@ export class HaEntityStatePicker extends LitElement {
|
||||
@consume({ context: internationalizationContext, subscribe: true })
|
||||
private _i18n!: ContextType<typeof internationalizationContext>;
|
||||
|
||||
private _getItems = memoizeOne(
|
||||
(
|
||||
states: HassEntities | null | undefined,
|
||||
formatters: HomeAssistantFormatters,
|
||||
language: string,
|
||||
entityId: string | string[] | undefined,
|
||||
attribute: string | undefined,
|
||||
hideStates: string[] | undefined,
|
||||
extraOptions: PickerComboBoxItem[] | undefined
|
||||
): PickerComboBoxItem[] => {
|
||||
const entityIds = entityId ? ensureArray(entityId) : [];
|
||||
|
||||
const entitiesOptions = entityIds.map<PickerComboBoxItem[]>(
|
||||
(entityIdItem) => {
|
||||
const stateObj =
|
||||
states?.[entityIdItem] ||
|
||||
({ entity_id: entityIdItem, attributes: {} } as HassEntity);
|
||||
|
||||
const options = getStates(
|
||||
states ?? {},
|
||||
language,
|
||||
stateObj,
|
||||
attribute
|
||||
).filter((s) => !hideStates?.includes(s));
|
||||
|
||||
return options
|
||||
.map((s) => {
|
||||
const primary = attribute
|
||||
? formatters.formatEntityAttributeValue(stateObj, attribute, s)
|
||||
: formatters.formatEntityState(stateObj, s);
|
||||
return {
|
||||
id: s,
|
||||
primary,
|
||||
sorting_label: primary,
|
||||
};
|
||||
})
|
||||
.filter((option) => option.id && option.primary);
|
||||
}
|
||||
);
|
||||
|
||||
const options: PickerComboBoxItem[] = [];
|
||||
const optionsSet = new Set<string>();
|
||||
for (const entityOptions of entitiesOptions) {
|
||||
for (const option of entityOptions) {
|
||||
if (!optionsSet.has(option.id)) {
|
||||
optionsSet.add(option.id);
|
||||
options.push(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (extraOptions) {
|
||||
// Filter out any extraOptions with empty primary or id fields
|
||||
const validExtraOptions = extraOptions.filter(
|
||||
(option) => option.id && option.primary
|
||||
);
|
||||
options.unshift(...validExtraOptions);
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
);
|
||||
private _getItems = memoizeOne(computeEntityStateItems);
|
||||
|
||||
private _getFilteredItems = (
|
||||
_searchString?: string,
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
import type { ContextType } from "@lit/context";
|
||||
import type { HassEntities, HassEntity } from "home-assistant-js-websocket";
|
||||
import type { PropertyValues } from "lit";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, property } from "lit/decorators";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { keyed } from "lit/directives/keyed";
|
||||
import { repeat } from "lit/directives/repeat";
|
||||
import { fireEvent } from "../../common/dom/fire_event";
|
||||
import { ANY_STATE_VALUE } from "./const";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { ensureArray } from "../../common/array/ensure-array";
|
||||
import "./ha-entity-state-picker";
|
||||
import { consume } from "../../common/decorators/consume";
|
||||
import { consumeEntityStates } from "../../common/decorators/consume-context-entry";
|
||||
import { fireEvent } from "../../common/dom/fire_event";
|
||||
import {
|
||||
formattersContext,
|
||||
internationalizationContext,
|
||||
statesContext,
|
||||
} from "../../data/context";
|
||||
import type { SelectSelector } from "../../data/selector";
|
||||
import type { HomeAssistantFormatters } from "../../types";
|
||||
import type { PickerComboBoxItem } from "../ha-picker-combo-box";
|
||||
import "../ha-selector/ha-selector-select";
|
||||
import { ANY_STATE_VALUE } from "./const";
|
||||
import { computeEntityStateItems } from "./ha-entity-state-picker";
|
||||
|
||||
@customElement("ha-entity-states-picker")
|
||||
export class HaEntityStatesPicker extends LitElement {
|
||||
@@ -32,6 +46,60 @@ export class HaEntityStatesPicker extends LitElement {
|
||||
@property({ attribute: false })
|
||||
public hideStates?: string[];
|
||||
|
||||
@property({ type: Boolean }) public reorder = false;
|
||||
|
||||
@state()
|
||||
@consumeEntityStates({ entityIdPath: ["entityId"] })
|
||||
private _stateObjs?: Record<string, HassEntity>;
|
||||
|
||||
@consume({ context: statesContext, subscribe: true })
|
||||
private _states?: ContextType<typeof statesContext>;
|
||||
|
||||
@state()
|
||||
@consume({ context: formattersContext, subscribe: true })
|
||||
private _formatters!: ContextType<typeof formattersContext>;
|
||||
|
||||
@state()
|
||||
@consume({ context: internationalizationContext, subscribe: true })
|
||||
private _i18n!: ContextType<typeof internationalizationContext>;
|
||||
|
||||
private _chipsSelector = memoizeOne(
|
||||
(
|
||||
states: HassEntities | null | undefined,
|
||||
formatters: HomeAssistantFormatters,
|
||||
language: string,
|
||||
entityId: string | undefined,
|
||||
attribute: string | undefined,
|
||||
hideStates: string[] | undefined,
|
||||
extraOptions: PickerComboBoxItem[] | undefined,
|
||||
customValue: boolean,
|
||||
// Not read, only there to refresh the options when the entity changes
|
||||
_stateObjs: Record<string, HassEntity> | undefined
|
||||
): SelectSelector => {
|
||||
const items = computeEntityStateItems(
|
||||
states,
|
||||
formatters,
|
||||
language,
|
||||
entityId,
|
||||
attribute,
|
||||
hideStates,
|
||||
extraOptions
|
||||
);
|
||||
const options = items.map((item) => ({
|
||||
value: item.id,
|
||||
label: item.primary,
|
||||
}));
|
||||
return {
|
||||
select: {
|
||||
multiple: true,
|
||||
reorder: true,
|
||||
custom_value: customValue,
|
||||
options,
|
||||
},
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
private _keys: string[] = [];
|
||||
|
||||
private _getKey(index: number) {
|
||||
@@ -48,7 +116,36 @@ export class HaEntityStatesPicker extends LitElement {
|
||||
}
|
||||
}
|
||||
|
||||
private _renderChips() {
|
||||
const selector = this._chipsSelector(
|
||||
this._states,
|
||||
this._formatters,
|
||||
this._i18n.locale.language,
|
||||
this.entityId,
|
||||
this.attribute,
|
||||
this.hideStates,
|
||||
this.extraOptions,
|
||||
Boolean(this.allowCustomValue),
|
||||
this._stateObjs
|
||||
);
|
||||
|
||||
return html`
|
||||
<ha-selector-select
|
||||
.selector=${selector}
|
||||
.value=${this.value}
|
||||
.label=${this.label}
|
||||
.helper=${this.helper}
|
||||
.disabled=${this.disabled}
|
||||
.required=${this.required}
|
||||
></ha-selector-select>
|
||||
`;
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (this.reorder) {
|
||||
return this._renderChips();
|
||||
}
|
||||
|
||||
const value = this.value || [];
|
||||
const hide = [...(this.hideStates || []), ...value];
|
||||
const hideValue = value.includes(ANY_STATE_VALUE);
|
||||
@@ -56,18 +153,18 @@ export class HaEntityStatesPicker extends LitElement {
|
||||
return html`
|
||||
${repeat(
|
||||
value,
|
||||
(_state, index) => this._getKey(index),
|
||||
(state, index) => html`
|
||||
(_item, index) => this._getKey(index),
|
||||
(item, index) => html`
|
||||
<div>
|
||||
<ha-entity-state-picker
|
||||
.index=${index}
|
||||
.entityId=${this.entityId}
|
||||
.attribute=${this.attribute}
|
||||
.extraOptions=${this.extraOptions}
|
||||
.hideStates=${hide.filter((v) => v !== state)}
|
||||
.hideStates=${hide.filter((v) => v !== item)}
|
||||
.allowCustomValue=${this.allowCustomValue}
|
||||
.label=${this.label}
|
||||
.value=${state}
|
||||
.value=${item}
|
||||
.disabled=${this.disabled}
|
||||
.helper=${
|
||||
this.disabled && index === value.length - 1
|
||||
|
||||
@@ -86,6 +86,7 @@ export class HaSelectorState extends SubscribeMixin(LitElement) {
|
||||
.required=${this.required}
|
||||
allow-custom-value
|
||||
.hideStates=${this.selector.state?.hide_states}
|
||||
.reorder=${this.selector.state?.reorder ?? false}
|
||||
></ha-entity-states-picker>
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -576,6 +576,7 @@ export interface StateSelector {
|
||||
attribute?: string;
|
||||
hide_states?: string[];
|
||||
multiple?: boolean;
|
||||
reorder?: boolean;
|
||||
} | null;
|
||||
}
|
||||
|
||||
|
||||
+22
-39
@@ -1,23 +1,19 @@
|
||||
import { html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { consume } from "../../../../common/decorators/consume";
|
||||
import {
|
||||
consumeEntityState,
|
||||
consumeLocalize,
|
||||
} from "../../../../common/decorators/consume-context-entry";
|
||||
import { transform } from "../../../../common/decorators/transform";
|
||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||
import type { FormatEntityAttributeValueFunc } from "../../../../common/translations/entity-state";
|
||||
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
||||
import "../../../../components/ha-form/ha-form";
|
||||
import type { SchemaUnion } from "../../../../components/ha-form/types";
|
||||
import { formattersContext } from "../../../../data/context";
|
||||
import type { LightEntity } from "../../../../data/light";
|
||||
import type {
|
||||
HomeAssistantFormatters,
|
||||
ValueChangedEvent,
|
||||
} from "../../../../types";
|
||||
HaFormSchema,
|
||||
SchemaUnion,
|
||||
} from "../../../../components/ha-form/types";
|
||||
import type { LightEntity } from "../../../../data/light";
|
||||
import type { ValueChangedEvent } from "../../../../types";
|
||||
import type {
|
||||
LightEffectCardFeatureConfig,
|
||||
LovelaceCardFeatureContext,
|
||||
@@ -25,10 +21,20 @@ import type {
|
||||
import type { LovelaceCardFeatureEditor } from "../../types";
|
||||
import {
|
||||
customizableListData,
|
||||
customizableListSchema,
|
||||
processCustomizableListValue,
|
||||
} from "./customizable-list-feature";
|
||||
|
||||
const SCHEMA = [
|
||||
{ name: "customize", selector: { boolean: {} } },
|
||||
{
|
||||
name: "effects",
|
||||
visible: { field: "customize", value: true },
|
||||
selector: {
|
||||
state: { attribute: "effect", multiple: true, reorder: true },
|
||||
},
|
||||
},
|
||||
] as const satisfies readonly HaFormSchema[];
|
||||
|
||||
@customElement("hui-light-effect-card-feature-editor")
|
||||
export class HuiLightEffectCardFeatureEditor
|
||||
extends LitElement
|
||||
@@ -40,13 +46,6 @@ export class HuiLightEffectCardFeatureEditor
|
||||
@consumeEntityState({ entityIdPath: ["context", "entity_id"] })
|
||||
private _stateObj?: LightEntity;
|
||||
|
||||
@state()
|
||||
@consume({ context: formattersContext, subscribe: true })
|
||||
@transform<HomeAssistantFormatters, FormatEntityAttributeValueFunc>({
|
||||
transformer: (formatters) => formatters?.formatEntityAttributeValue,
|
||||
})
|
||||
private _formatEntityAttributeValue!: FormatEntityAttributeValueFunc;
|
||||
|
||||
@state() @consumeLocalize() private _localize!: LocalizeFunc;
|
||||
|
||||
@state() private _config?: LightEffectCardFeatureConfig;
|
||||
@@ -55,20 +54,9 @@ export class HuiLightEffectCardFeatureEditor
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
private _schema = memoizeOne(
|
||||
(
|
||||
formatEntityAttributeValue: FormatEntityAttributeValueFunc,
|
||||
stateObj: LightEntity | undefined
|
||||
) =>
|
||||
customizableListSchema({
|
||||
field: "effects",
|
||||
options:
|
||||
stateObj?.attributes.effect_list?.map((effect) => ({
|
||||
value: effect,
|
||||
label: formatEntityAttributeValue(stateObj, "effect", effect),
|
||||
})) ?? [],
|
||||
})
|
||||
);
|
||||
private _formContext = memoizeOne((entityId: string | undefined) => ({
|
||||
filter_entity: entityId,
|
||||
}));
|
||||
|
||||
protected render() {
|
||||
if (!this._config) {
|
||||
@@ -76,15 +64,12 @@ export class HuiLightEffectCardFeatureEditor
|
||||
}
|
||||
|
||||
const data = customizableListData(this._config, "effects");
|
||||
const schema = this._schema(
|
||||
this._formatEntityAttributeValue,
|
||||
this._stateObj
|
||||
);
|
||||
|
||||
return html`
|
||||
<ha-form
|
||||
.data=${data}
|
||||
.schema=${schema}
|
||||
.schema=${SCHEMA}
|
||||
.context=${this._formContext(this.context?.entity_id)}
|
||||
.computeLabel=${this._computeLabelCallback}
|
||||
@value-changed=${this._valueChanged}
|
||||
></ha-form>
|
||||
@@ -103,9 +88,7 @@ export class HuiLightEffectCardFeatureEditor
|
||||
fireEvent(this, "config-changed", { config });
|
||||
}
|
||||
|
||||
private _computeLabelCallback = (
|
||||
schema: SchemaUnion<ReturnType<typeof this._schema>>
|
||||
) =>
|
||||
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
|
||||
this._localize(
|
||||
`ui.panel.lovelace.editor.features.types.light-effect.${schema.name}`
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user