Skip to content

Commit 7665e67

Browse files
Added possibility to use dynamic tile icon
1 parent 575f9c8 commit 7665e67

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/model/generators/tiles-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class TilesGenerator {
118118
entity: vacuumEntity,
119119
label: localize("tile.battery_level.label", language),
120120
attribute: "battery_level",
121-
icon: state.attributes["battery_icon"],
121+
icon_source: `${vacuumEntity}.attributes.battery_icon`,
122122
unit: "%",
123123
});
124124
if ("battery_level" in state.attributes && !("battery_icon" in state.attributes))

src/renderers/tile-renderer.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class TileRenderer {
2121
const stateObj = config.entity ? card.hass.states[config.entity] : undefined;
2222
const title = this.getTileLabel(card.hass, config, stateObj);
2323
const value = this.getTileValue(card.hass, config, internalVariables, stateObj);
24-
const icon = this.getIcon(config, stateObj);
24+
const icon = this.getIcon(card.hass, config, stateObj);
2525
const domain = stateObj ? computeStateDomain(stateObj) : undefined;
2626

2727
return html`
@@ -100,7 +100,16 @@ export class TileRenderer {
100100
return `${value}${unit}`;
101101
}
102102

103-
private static getIcon(config: TileConfig, stateObject?: HassEntity) {
103+
private static getIcon(hass: HomeAssistantFixed, config: TileConfig, stateObject?: HassEntity) {
104+
if (config.icon_source) {
105+
const split = config.icon_source.split(".attributes.");
106+
const entity = hass.states[split[0]];
107+
let icon = entity.state;
108+
if (split.length === 2) {
109+
icon = entity.attributes[split[1]];
110+
}
111+
return icon;
112+
}
104113
if (config.icon === undefined && stateObject) {
105114
return stateObject.attributes.icon ?? null;
106115
}

src/types/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export interface TileConfig extends ActionableObjectConfig, ConditionalObjectCon
124124
readonly label?: string;
125125
readonly tooltip?: string;
126126
readonly icon?: string;
127+
readonly icon_source?: string;
127128
readonly internal_variable?: string;
128129
readonly entity?: string;
129130
readonly attribute?: string;

src/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export function getWatchedEntitiesForPreset(config: CardPresetConfig, language:
8383
(config.tiles ?? []).forEach(s => {
8484
if (s.entity) watchedEntities.add(s.entity);
8585
});
86+
(config.tiles ?? []).forEach(s => {
87+
if (s.icon_source) watchedEntities.add(s.icon_source.split(".attributes.")[0]);
88+
});
8689
(config.tiles ?? [])
8790
.filter(s => s.conditions)
8891
.flatMap(s => s.conditions)

0 commit comments

Comments
 (0)