Skip to content

Commit

Permalink
Added possibility to use dynamic tile icon
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed Mar 31, 2023
1 parent 575f9c8 commit 7665e67
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/model/generators/tiles-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class TilesGenerator {
entity: vacuumEntity,
label: localize("tile.battery_level.label", language),
attribute: "battery_level",
icon: state.attributes["battery_icon"],
icon_source: `${vacuumEntity}.attributes.battery_icon`,
unit: "%",
});
if ("battery_level" in state.attributes && !("battery_icon" in state.attributes))
Expand Down
13 changes: 11 additions & 2 deletions src/renderers/tile-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TileRenderer {
const stateObj = config.entity ? card.hass.states[config.entity] : undefined;
const title = this.getTileLabel(card.hass, config, stateObj);
const value = this.getTileValue(card.hass, config, internalVariables, stateObj);
const icon = this.getIcon(config, stateObj);
const icon = this.getIcon(card.hass, config, stateObj);
const domain = stateObj ? computeStateDomain(stateObj) : undefined;

return html`
Expand Down Expand Up @@ -100,7 +100,16 @@ export class TileRenderer {
return `${value}${unit}`;
}

private static getIcon(config: TileConfig, stateObject?: HassEntity) {
private static getIcon(hass: HomeAssistantFixed, config: TileConfig, stateObject?: HassEntity) {
if (config.icon_source) {
const split = config.icon_source.split(".attributes.");
const entity = hass.states[split[0]];
let icon = entity.state;
if (split.length === 2) {
icon = entity.attributes[split[1]];
}
return icon;
}
if (config.icon === undefined && stateObject) {
return stateObject.attributes.icon ?? null;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface TileConfig extends ActionableObjectConfig, ConditionalObjectCon
readonly label?: string;
readonly tooltip?: string;
readonly icon?: string;
readonly icon_source?: string;
readonly internal_variable?: string;
readonly entity?: string;
readonly attribute?: string;
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export function getWatchedEntitiesForPreset(config: CardPresetConfig, language:
(config.tiles ?? []).forEach(s => {
if (s.entity) watchedEntities.add(s.entity);
});
(config.tiles ?? []).forEach(s => {
if (s.icon_source) watchedEntities.add(s.icon_source.split(".attributes.")[0]);
});
(config.tiles ?? [])
.filter(s => s.conditions)
.flatMap(s => s.conditions)
Expand Down

0 comments on commit 7665e67

Please sign in to comment.