Skip to content

Commit 62c92dc

Browse files
authored
Add support for entity_id in device names
1 parent c52b2c3 commit 62c92dc

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

compact-power-card.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ class CompactPowerCard extends (window.LitElement ||
16191619
sources.forEach((src) => {
16201620
add(this._extractEntityRef(src?.entity));
16211621
add(this._extractEntityRef(src?.switch_entity));
1622+
add(this._extractEntityRef(src?.name));
16221623
});
16231624

16241625
return ids;
@@ -1793,6 +1794,16 @@ class CompactPowerCard extends (window.LitElement ||
17931794
return u ? `${s} ${u}` : s;
17941795
}
17951796

1797+
_formatEntityStateWithUnit(entityId) {
1798+
if (!this.hass || !entityId) return "";
1799+
const obj = this.hass.states[entityId];
1800+
if (!obj) return "";
1801+
const state = obj.state;
1802+
const unit = obj.attributes?.unit_of_measurement;
1803+
if (unit) return `${state} ${unit}`;
1804+
return state == null ? "" : String(state);
1805+
}
1806+
17961807
_isWattToKw(num, unit) {
17971808
return unit && unit.toLowerCase() === "w" && Number.isFinite(num) && Math.abs(num) >= 1000;
17981809
}
@@ -3431,6 +3442,8 @@ class CompactPowerCard extends (window.LitElement ||
34313442
const switchEntity = src.switch_entity || src.switchEntity || null;
34323443
const attribute = src.attribute || null;
34333444
const name = src.name || null;
3445+
const nameEntity = name && this.hass?.states?.[name] ? name : null;
3446+
const displayName = nameEntity ? this._formatEntityStateWithUnit(nameEntity) : name;
34343447
const icon = src.icon || this._getEntityIcon(entity, "mdi:power-plug");
34353448
const isPowerDevice = this._isPowerDevice(entity);
34363449
const numeric = this._getNumericMaybe(entity, attribute);
@@ -3461,7 +3474,7 @@ class CompactPowerCard extends (window.LitElement ||
34613474
entity,
34623475
switchEntity,
34633476
switchOn,
3464-
name,
3477+
name: displayName,
34653478
icon,
34663479
val,
34673480
color,

0 commit comments

Comments
 (0)