Button Card: Option to show state (#6383)

This commit is contained in:
Ian Richardson 2020-07-14 16:26:21 -05:00 committed by GitHub
parent 90e14762e3
commit 8b3b40e627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import { hasAction } from "../common/has-action";
import { createEntityNotFoundWarning } from "../components/hui-warning"; import { createEntityNotFoundWarning } from "../components/hui-warning";
import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCard, LovelaceCardEditor } from "../types";
import { ButtonCardConfig } from "./types"; import { ButtonCardConfig } from "./types";
import { computeStateDisplay } from "../../../common/entity/compute_state_display";
@customElement("hui-button-card") @customElement("hui-button-card")
export class HuiButtonCard extends LitElement implements LovelaceCard { export class HuiButtonCard extends LitElement implements LovelaceCard {
@ -66,6 +67,7 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
hold_action: { action: "more-info" }, hold_action: { action: "more-info" },
show_icon: true, show_icon: true,
show_name: true, show_name: true,
show_state: false,
entity: foundEntities[0] || "", entity: foundEntities[0] || "",
}; };
} }
@ -203,6 +205,15 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
</span> </span>
` `
: ""} : ""}
${this._config.show_state && stateObj
? html`<span class="state">
${computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.language
)}
</span>`
: ""}
${this._shouldRenderRipple ? html`<mwc-ripple></mwc-ripple>` : ""} ${this._shouldRenderRipple ? html`<mwc-ripple></mwc-ripple>` : ""}
</ha-card> </ha-card>
`; `;
@ -282,6 +293,11 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
outline: none; outline: none;
} }
.state {
font-size: 0.9rem;
color: var(--secondary-text-color);
}
${iconColorCSS} ${iconColorCSS}
`; `;
} }

View File

@ -71,6 +71,7 @@ export interface ButtonCardConfig extends LovelaceCardConfig {
hold_action?: ActionConfig; hold_action?: ActionConfig;
double_tap_action?: ActionConfig; double_tap_action?: ActionConfig;
state_color?: boolean; state_color?: boolean;
show_state?: boolean;
} }
export interface EntityFilterCardConfig extends LovelaceCardConfig { export interface EntityFilterCardConfig extends LovelaceCardConfig {

View File

@ -38,6 +38,7 @@ const cardConfigStruct = struct({
tap_action: struct.optional(actionConfigStruct), tap_action: struct.optional(actionConfigStruct),
hold_action: struct.optional(actionConfigStruct), hold_action: struct.optional(actionConfigStruct),
theme: "string?", theme: "string?",
show_state: "boolean?",
}); });
@customElement("hui-button-card-editor") @customElement("hui-button-card-editor")
@ -64,6 +65,10 @@ export class HuiButtonCardEditor extends LitElement
return this._config!.show_name || true; return this._config!.show_name || true;
} }
get _show_state(): boolean {
return this._config!.show_state || false;
}
get _icon(): string { get _icon(): string {
return this._config!.icon || ""; return this._config!.icon || "";
} }
@ -153,12 +158,26 @@ export class HuiButtonCardEditor extends LitElement
.dir=${dir} .dir=${dir}
> >
<ha-switch <ha-switch
.checked="${this._config!.show_name !== false}" .checked="${this._show_name !== false}"
.configValue="${"show_name"}" .configValue="${"show_name"}"
@change="${this._valueChanged}" @change="${this._valueChanged}"
></ha-switch> ></ha-switch>
</ha-formfield> </ha-formfield>
</div> </div>
<div>
<ha-formfield
.label=${this.hass.localize(
"ui.panel.lovelace.editor.card.generic.show_state"
)}
.dir=${dir}
>
<ha-switch
.checked=${this._show_state !== false}
.configValue=${"show_state"}
@change=${this._valueChanged}
></ha-switch>
</ha-formfield>
</div>
<div> <div>
<ha-formfield <ha-formfield
.label=${this.hass.localize( .label=${this.hass.localize(
@ -167,7 +186,7 @@ export class HuiButtonCardEditor extends LitElement
.dir=${dir} .dir=${dir}
> >
<ha-switch <ha-switch
.checked="${this._config!.show_icon !== false}" .checked="${this._show_icon !== false}"
.configValue="${"show_icon"}" .configValue="${"show_icon"}"
@change="${this._valueChanged}" @change="${this._valueChanged}"
></ha-switch> ></ha-switch>