Allow name in state content for badges (#21576)

This commit is contained in:
Paul Bottein 2024-08-05 17:14:10 +02:00 committed by GitHub
parent 5c30c1647c
commit 4719775926
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 66 additions and 39 deletions

View File

@ -81,6 +81,9 @@ class HaEntityStatePicker extends LitElement {
@property({ type: Boolean }) public required = false; @property({ type: Boolean }) public required = false;
@property({ type: Boolean, attribute: "allow-name" }) public allowName =
false;
@property() public label?: string; @property() public label?: string;
@property() public value?: string[] | string; @property() public value?: string[] | string;
@ -95,13 +98,24 @@ class HaEntityStatePicker extends LitElement {
return !(!changedProps.has("_opened") && this._opened); return !(!changedProps.has("_opened") && this._opened);
} }
private options = memoizeOne((entityId?: string, stateObj?: HassEntity) => { private options = memoizeOne(
(entityId?: string, stateObj?: HassEntity, allowName?: boolean) => {
const domain = entityId ? computeDomain(entityId) : undefined; const domain = entityId ? computeDomain(entityId) : undefined;
return [ return [
{ {
label: this.hass.localize("ui.components.state-content-picker.state"), label: this.hass.localize("ui.components.state-content-picker.state"),
value: "state", value: "state",
}, },
...(allowName
? [
{
label: this.hass.localize(
"ui.components.state-content-picker.name"
),
value: "name",
},
]
: []),
{ {
label: this.hass.localize( label: this.hass.localize(
"ui.components.state-content-picker.last_changed" "ui.components.state-content-picker.last_changed"
@ -131,7 +145,8 @@ class HaEntityStatePicker extends LitElement {
label: this.hass.formatEntityAttributeName(stateObj!, attribute), label: this.hass.formatEntityAttributeName(stateObj!, attribute),
})), })),
]; ];
}); }
);
private _filter = ""; private _filter = "";
@ -146,7 +161,7 @@ class HaEntityStatePicker extends LitElement {
? this.hass.states[this.entityId] ? this.hass.states[this.entityId]
: undefined; : undefined;
const options = this.options(this.entityId, stateObj); const options = this.options(this.entityId, stateObj, this.allowName);
const optionItems = options.filter( const optionItems = options.filter(
(option) => !this._value.includes(option.value) (option) => !this._value.includes(option.value)
); );

View File

@ -36,6 +36,7 @@ export class HaSelectorUiStateContent extends SubscribeMixin(LitElement) {
.helper=${this.helper} .helper=${this.helper}
.disabled=${this.disabled} .disabled=${this.disabled}
.required=${this.required} .required=${this.required}
.allowName=${this.selector.ui_state_content?.allow_name}
></ha-entity-state-content-picker> ></ha-entity-state-content-picker>
`; `;
} }

View File

@ -461,6 +461,7 @@ export interface UiStateContentSelector {
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
ui_state_content: { ui_state_content: {
entity_id?: string; entity_id?: string;
allow_name?: boolean;
} | null; } | null;
} }

View File

@ -144,6 +144,7 @@ export class HuiEntityBadge extends LitElement implements LovelaceBadge {
.stateObj=${stateObj} .stateObj=${stateObj}
.hass=${this.hass} .hass=${this.hass}
.content=${this._config.state_content} .content=${this._config.state_content}
.name=${this._config.name}
> >
</state-display> </state-display>
`; `;

View File

@ -251,6 +251,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
.stateObj=${stateObj} .stateObj=${stateObj}
.hass=${this.hass} .hass=${this.hass}
.content=${this._config.state_content} .content=${this._config.state_content}
.name=${this._config.name}
> >
</state-display> </state-display>
`; `;

View File

@ -122,7 +122,9 @@ export class HuiEntityBadgeEditor
{ {
name: "state_content", name: "state_content",
selector: { selector: {
ui_state_content: {}, ui_state_content: {
allow_name: true,
},
}, },
context: { context: {
filter_entity: "entity", filter_entity: "entity",

View File

@ -55,6 +55,8 @@ class StateDisplay extends LitElement {
@property({ attribute: false }) public content?: StateContent; @property({ attribute: false }) public content?: StateContent;
@property({ attribute: false }) public name?: string;
protected createRenderRoot() { protected createRenderRoot() {
return this; return this;
} }
@ -88,6 +90,9 @@ class StateDisplay extends LitElement {
return this.hass!.formatEntityState(stateObj); return this.hass!.formatEntityState(stateObj);
} }
if (content === "name") {
return html`${this.name || stateObj.attributes.friendly_name}`;
}
// Check last-changed for backwards compatibility // Check last-changed for backwards compatibility
if (content === "last_changed" || content === "last-changed") { if (content === "last_changed" || content === "last-changed") {
return html` return html`

View File

@ -1026,6 +1026,7 @@
}, },
"state-content-picker": { "state-content-picker": {
"state": "State", "state": "State",
"name": "Name",
"last_changed": "Last changed", "last_changed": "Last changed",
"last_updated": "Last updated", "last_updated": "Last updated",
"remaining_time": "Remaining time", "remaining_time": "Remaining time",