Add state_color support to entity card (#9617)

This commit is contained in:
Philip Allgaier 2021-09-06 11:26:16 +02:00 committed by GitHub
parent e7e27e794c
commit c6e83cb7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 159 additions and 121 deletions

View File

@ -129,38 +129,39 @@ export class StateBadge extends LitElement {
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return [
:host { iconColorCSS,
position: relative; css`
display: inline-block; :host {
width: 40px; position: relative;
color: var(--paper-item-icon-color, #44739e); display: inline-block;
border-radius: 50%; width: 40px;
height: 40px; color: var(--paper-item-icon-color, #44739e);
text-align: center; border-radius: 50%;
background-size: cover; height: 40px;
line-height: 40px; text-align: center;
vertical-align: middle; background-size: cover;
box-sizing: border-box; line-height: 40px;
} vertical-align: middle;
:host(:focus) { box-sizing: border-box;
outline: none; }
} :host(:focus) {
:host(:not([icon]):focus) { outline: none;
border: 2px solid var(--divider-color); }
} :host(:not([icon]):focus) {
:host([icon]:focus) { border: 2px solid var(--divider-color);
background: var(--divider-color); }
} :host([icon]:focus) {
ha-icon { background: var(--divider-color);
transition: color 0.3s ease-in-out, filter 0.3s ease-in-out; }
} ha-icon {
.missing { transition: color 0.3s ease-in-out, filter 0.3s ease-in-out;
color: #fce588; }
} .missing {
color: #fce588;
${iconColorCSS} }
`; `,
];
} }
} }

View File

@ -250,48 +250,49 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return [
ha-card { iconColorCSS,
cursor: pointer; css`
display: flex; ha-card {
flex-direction: column; cursor: pointer;
align-items: center; display: flex;
text-align: center; flex-direction: column;
padding: 4% 0; align-items: center;
font-size: 1.2rem; text-align: center;
height: 100%; padding: 4% 0;
box-sizing: border-box; font-size: 1.2rem;
justify-content: center; height: 100%;
position: relative; box-sizing: border-box;
} justify-content: center;
position: relative;
}
ha-card:focus { ha-card:focus {
outline: none; outline: none;
} }
ha-icon { ha-icon {
width: 40%; width: 40%;
height: auto; height: auto;
color: var(--paper-item-icon-color, #44739e); color: var(--paper-item-icon-color, #44739e);
--mdc-icon-size: 100%; --mdc-icon-size: 100%;
} }
ha-icon + span { ha-icon + span {
margin-top: 8px; margin-top: 8px;
} }
ha-icon, ha-icon,
span { span {
outline: none; outline: none;
} }
.state { .state {
font-size: 0.9rem; font-size: 0.9rem;
color: var(--secondary-text-color); color: var(--secondary-text-color);
} }
`,
${iconColorCSS} ];
`;
} }
private _computeBrightness(stateObj: HassEntity | LightEntity): string { private _computeBrightness(stateObj: HassEntity | LightEntity): string {

View File

@ -7,13 +7,17 @@ import {
TemplateResult, TemplateResult,
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
import { computeActiveState } from "../../../common/entity/compute_active_state";
import { computeStateDisplay } from "../../../common/entity/compute_state_display"; import { computeStateDisplay } from "../../../common/entity/compute_state_display";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
import { computeStateName } from "../../../common/entity/compute_state_name"; import { computeStateName } from "../../../common/entity/compute_state_name";
import { stateIcon } from "../../../common/entity/state_icon"; import { stateIcon } from "../../../common/entity/state_icon";
import { isValidEntityId } from "../../../common/entity/valid_entity_id"; import { isValidEntityId } from "../../../common/entity/valid_entity_id";
import { formatNumber } from "../../../common/string/format_number"; import { formatNumber } from "../../../common/string/format_number";
import { iconColorCSS } from "../../../common/style/icon_color_css";
import "../../../components/ha-card"; import "../../../components/ha-card";
import "../../../components/ha-icon"; import "../../../components/ha-icon";
import { UNAVAILABLE_STATES } from "../../../data/entity"; import { UNAVAILABLE_STATES } from "../../../data/entity";
@ -106,6 +110,7 @@ export class HuiEntityCard extends LitElement implements LovelaceCard {
`; `;
} }
const domain = computeStateDomain(stateObj);
const showUnit = this._config.attribute const showUnit = this._config.attribute
? this._config.attribute in stateObj.attributes ? this._config.attribute in stateObj.attributes
: !UNAVAILABLE_STATES.includes(stateObj.state); : !UNAVAILABLE_STATES.includes(stateObj.state);
@ -119,6 +124,13 @@ export class HuiEntityCard extends LitElement implements LovelaceCard {
<div class="icon"> <div class="icon">
<ha-icon <ha-icon
.icon=${this._config.icon || stateIcon(stateObj)} .icon=${this._config.icon || stateIcon(stateObj)}
data-domain=${ifDefined(
this._config.state_color ||
(domain === "light" && this._config.state_color !== false)
? domain
: undefined
)}
data-state=${stateObj ? computeActiveState(stateObj) : ""}
></ha-icon> ></ha-icon>
</div> </div>
</div> </div>
@ -189,56 +201,59 @@ export class HuiEntityCard extends LitElement implements LovelaceCard {
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return [
ha-card { iconColorCSS,
height: 100%; css`
display: flex; ha-card {
flex-direction: column; height: 100%;
justify-content: space-between; display: flex;
cursor: pointer; flex-direction: column;
outline: none; justify-content: space-between;
} cursor: pointer;
outline: none;
}
.header { .header {
display: flex; display: flex;
padding: 8px 16px 0; padding: 8px 16px 0;
justify-content: space-between; justify-content: space-between;
} }
.name { .name {
color: var(--secondary-text-color); color: var(--secondary-text-color);
line-height: 40px; line-height: 40px;
font-weight: 500; font-weight: 500;
font-size: 16px; font-size: 16px;
overflow: hidden; overflow: hidden;
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.icon { .icon {
color: var(--state-icon-color, #44739e); color: var(--state-icon-color, #44739e);
line-height: 40px; line-height: 40px;
} }
.info { .info {
padding: 0px 16px 16px; padding: 0px 16px 16px;
margin-top: -4px; margin-top: -4px;
overflow: hidden; overflow: hidden;
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
line-height: 28px; line-height: 28px;
} }
.value { .value {
font-size: 28px; font-size: 28px;
margin-right: 4px; margin-right: 4px;
} }
.measurement { .measurement {
font-size: 18px; font-size: 18px;
color: var(--secondary-text-color); color: var(--secondary-text-color);
} }
`; `,
];
} }
} }

View File

@ -39,6 +39,7 @@ export interface EntityCardConfig extends LovelaceCardConfig {
attribute?: string; attribute?: string;
unit?: string; unit?: string;
theme?: string; theme?: string;
state_color?: boolean;
} }
export interface EntitiesCardEntityConfig extends EntityConfig { export interface EntitiesCardEntityConfig extends EntityConfig {

View File

@ -1,7 +1,7 @@
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { assert, object, optional, string, assign } from "superstruct"; import { assert, assign, boolean, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { stateIcon } from "../../../../common/entity/state_icon"; import { stateIcon } from "../../../../common/entity/state_icon";
import "../../../../components/entity/ha-entity-attribute-picker"; import "../../../../components/entity/ha-entity-attribute-picker";
@ -13,9 +13,9 @@ import "../../components/hui-entity-editor";
import "../../components/hui-theme-select-editor"; import "../../components/hui-theme-select-editor";
import { headerFooterConfigStructs } from "../../header-footer/structs"; import { headerFooterConfigStructs } from "../../header-footer/structs";
import { LovelaceCardEditor } from "../../types"; import { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EditorTarget, EntitiesEditorEvent } from "../types"; import { EditorTarget, EntitiesEditorEvent } from "../types";
import { configElementStyle } from "./config-elements-style"; import { configElementStyle } from "./config-elements-style";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign( const cardConfigStruct = assign(
baseLovelaceCardConfig, baseLovelaceCardConfig,
@ -26,6 +26,7 @@ const cardConfigStruct = assign(
attribute: optional(string()), attribute: optional(string()),
unit: optional(string()), unit: optional(string()),
theme: optional(string()), theme: optional(string()),
state_color: optional(boolean()),
footer: optional(headerFooterConfigStructs), footer: optional(headerFooterConfigStructs),
}) })
); );
@ -64,6 +65,10 @@ export class HuiEntityCardEditor
return this._config!.unit || ""; return this._config!.unit || "";
} }
get _state_color(): boolean {
return this._config!.state_color ?? false;
}
get _theme(): string { get _theme(): string {
return this._config!.theme || ""; return this._config!.theme || "";
} }
@ -135,12 +140,27 @@ export class HuiEntityCardEditor
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
></paper-input> ></paper-input>
</div> </div>
<hui-theme-select-editor <div class="side-by-side">
.hass=${this.hass} <hui-theme-select-editor
.value=${this._theme} .hass=${this.hass}
.configValue=${"theme"} .value=${this._theme}
@value-changed=${this._valueChanged} .configValue=${"theme"}
></hui-theme-select-editor> @value-changed=${this._valueChanged}
>
</hui-theme-select-editor>
<ha-formfield
.label=${this.hass.localize(
"ui.panel.lovelace.editor.card.generic.state_color"
)}
>
<ha-switch
.checked=${this._config!.state_color}
.configValue=${"state_color"}
@change=${this._valueChanged}
>
</ha-switch>
</ha-formfield>
</div>
</div> </div>
`; `;
} }