Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

icon_color option for entities #3956

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/common/entity/compute_color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HassEntity } from "home-assistant-js-websocket";

export const computeColor = (
stateObj?: HassEntity,
iconColor?: string
): string => {
if (iconColor) {
return iconColor;
}

if (!stateObj || !stateObj.attributes.hs_color) {
return "";
}
const [hue, sat] = stateObj.attributes.hs_color;
if (sat <= 10) {
return "";
}
return `hsl(${hue}, 100%, ${100 - sat / 2}%)`;
};
5 changes: 4 additions & 1 deletion src/components/entity/state-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class StateBadge extends LitElement {
public hass?: HomeAssistant;
@property() public stateObj?: HassEntity;
@property() public overrideIcon?: string;
@property() public overrideIconColor?: string;
@property() public overrideImage?: string;
@query("ha-icon") private _icon!: HaIcon;

Expand Down Expand Up @@ -67,7 +68,9 @@ class StateBadge extends LitElement {
hostStyle.backgroundImage = `url(${imageUrl})`;
iconStyle.display = "none";
} else {
if (stateObj.attributes.hs_color) {
if (this.overrideIconColor) {
iconStyle.color = this.overrideIconColor;
} else if (stateObj.attributes.hs_color) {
const hue = stateObj.attributes.hs_color[0];
const sat = stateObj.attributes.hs_color[1];
if (sat > 10) {
Expand Down
14 changes: 2 additions & 12 deletions src/panels/lovelace/cards/hui-entity-button-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { longPress } from "../common/directives/long-press-directive";
import { handleClick } from "../common/handle-click";
import { DOMAINS_TOGGLE } from "../../../common/const";
import { EntityButtonCardConfig } from "./types";
import { computeColor } from "../../../common/entity/compute_color";

@customElement("hui-entity-button-card")
class HuiEntityButtonCard extends LitElement implements LovelaceCard {
Expand Down Expand Up @@ -130,7 +131,7 @@ class HuiEntityButtonCard extends LitElement implements LovelaceCard {
.icon="${this._config.icon || stateIcon(stateObj)}"
style="${styleMap({
filter: this._computeBrightness(stateObj),
color: this._computeColor(stateObj),
color: computeColor(stateObj, this._config.icon_color),
height: this._config.icon_height
? this._config.icon_height
: "auto",
Expand Down Expand Up @@ -201,17 +202,6 @@ class HuiEntityButtonCard extends LitElement implements LovelaceCard {
return `brightness(${(brightness + 245) / 5}%)`;
}

private _computeColor(stateObj: HassEntity | LightEntity): string {
if (!stateObj.attributes.hs_color) {
return "";
}
const [hue, sat] = stateObj.attributes.hs_color;
if (sat <= 10) {
return "";
}
return `hsl(${hue}, 100%, ${100 - sat / 2}%)`;
}

private _handleTap() {
handleClick(this, this.hass!, this._config!, false);
}
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/cards/hui-glance-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class HuiGlanceCard extends LitElement implements LovelaceCard {
.stateObj=${stateObj}
.overrideIcon=${entityConf.icon}
.overrideImage=${entityConf.image}
.overrideIconColor=${entityConf.icon_color}
></state-badge>
`
: ""}
Expand Down
5 changes: 5 additions & 0 deletions src/panels/lovelace/cards/hui-picture-glance-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { longPress } from "../common/directives/long-press-directive";
import { processConfigEntities } from "../common/process-config-entities";
import { handleClick } from "../common/handle-click";
import { PictureGlanceCardConfig, ConfigEntity } from "./types";
import { styleMap } from "lit-html/directives/style-map";
import { computeColor } from "../../../common/entity/compute_color";
import { hasConfigOrEntityChanged } from "../common/has-changed";

const STATES_OFF = new Set(["closed", "locked", "not_home", "off"]);
Expand Down Expand Up @@ -205,6 +207,9 @@ class HuiPictureGlanceCard extends LitElement implements LovelaceCard {
this.hass!.language
)}
`}"
style=${styleMap({
color: computeColor(stateObj, entityConf.icon_color),
})}
></ha-icon>
`;
}
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/cards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface EntityButtonCardConfig extends LovelaceCardConfig {
name?: string;
show_name?: boolean;
icon?: string;
icon_color?: string;
show_icon?: boolean;
theme?: string;
tap_action?: ActionConfig;
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/components/hui-generic-entity-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class HuiGenericEntityRow extends LitElement {
.stateObj=${stateObj}
.overrideIcon=${this.config.icon}
.overrideImage=${this.config.image}
.overrideIconColor=${this.config.icon_color}
></state-badge>
<div class="flex">
<div class="info">
Expand Down
5 changes: 5 additions & 0 deletions src/panels/lovelace/elements/hui-icon-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { handleClick } from "../common/handle-click";
import { longPress } from "../common/directives/long-press-directive";
import { LovelaceElement, IconElementConfig } from "./types";
import { HomeAssistant } from "../../../types";
import { styleMap } from "lit-html/directives/style-map";
import { computeColor } from "../../../common/entity/compute_color";

@customElement("hui-icon-element")
export class HuiIconElement extends LitElement implements LovelaceElement {
Expand All @@ -41,6 +43,9 @@ export class HuiIconElement extends LitElement implements LovelaceElement {
@ha-click="${this._handleTap}"
@ha-hold="${this._handleHold}"
.longPress="${longPress()}"
style=${styleMap({
color: computeColor(undefined, this._config.icon_color),
})}
></ha-icon>
`;
}
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/elements/hui-state-icon-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class HuiStateIconElement extends LitElement implements LovelaceElement {
@ha-hold="${this._handleHold}"
.longPress="${longPress()}"
.overrideIcon=${this._config.icon}
.overrideIconColor=${this._config.icon_color}
></state-badge>
`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/panels/lovelace/elements/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IconElementConfig extends LovelaceElementConfig {
tap_action?: ActionConfig;
hold_action?: ActionConfig;
icon: string;
icon_color?: string;
}

export interface ImageElementConfig extends LovelaceElementConfig {
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface StateIconElementConfig extends LovelaceElementConfig {
tap_action?: ActionConfig;
hold_action?: ActionConfig;
icon?: string;
icon_color?: string;
}

export interface StateLabelElementConfig extends LovelaceElementConfig {
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/entity-rows/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface EntityConfig {
type?: string;
name?: string;
icon?: string;
icon_color?: string;
image?: string;
}
export interface EntityFilterEntityConfig extends EntityConfig {
Expand Down