From c8471cb6234bb6f1fbf5863a3fabeefe1b1fe7d2 Mon Sep 17 00:00:00 2001 From: ildar170975 <71872483+ildar170975@users.noreply.github.com> Date: Fri, 23 Jan 2026 18:13:24 +0300 Subject: [PATCH] Refactor processing values w/o unit in "ha-attribute-value" & "hui-attribute-row" (#28540) * add "hideUnit" to formatEntityAttributeValue() * add "hideUnit" to formatEntityAttributeValue() * add "hideUnit" to computeAttributeValueDisplay() * use formatEntityAttributeValue() with "hideUnit" * fix logic for "hideUnit" for ha-attribute-value * prettier * remove hideUnit from formatEntityAttributeValue() * revert to the initial code * revert to the initial code * revert to the initial code * use formatEntityAttributeValuePart() to get a value w/o unit * use formatEntityAttributeValueToParts() instead of formatEntityAttributeValuePart() * fix a value * fix name of a const * Update src/components/ha-attribute-value.ts Co-authored-by: Paul Bottein * Prettier --------- Co-authored-by: Paul Bottein --- src/components/ha-attribute-value.ts | 13 ++++++++----- .../lovelace/special-rows/hui-attribute-row.ts | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/components/ha-attribute-value.ts b/src/components/ha-attribute-value.ts index a419c6b99e..d586ed5707 100644 --- a/src/components/ha-attribute-value.ts +++ b/src/components/ha-attribute-value.ts @@ -2,7 +2,6 @@ import type { HassEntity } from "home-assistant-js-websocket"; import { css, html, LitElement, nothing } from "lit"; import { customElement, property } from "lit/decorators"; import { until } from "lit/directives/until"; -import { formatNumber } from "../common/number/format_number"; import type { HomeAssistant } from "../types"; @customElement("ha-attribute-value") @@ -21,10 +20,6 @@ class HaAttributeValue extends LitElement { } const attributeValue = this.stateObj.attributes[this.attribute]; - if (typeof attributeValue === "number" && this.hideUnit) { - return formatNumber(attributeValue, this.hass.locale); - } - if (typeof attributeValue === "string") { // URL handling if (attributeValue.startsWith("http")) { @@ -56,6 +51,14 @@ class HaAttributeValue extends LitElement { return html`
${until(yaml, "")}
`; } + if (this.hideUnit) { + const parts = this.hass.formatEntityAttributeValueToParts( + this.stateObj!, + this.attribute + ); + return parts.find((part) => part.type === "value")?.value; + } + return this.hass.formatEntityAttributeValue(this.stateObj!, this.attribute); } diff --git a/src/panels/lovelace/special-rows/hui-attribute-row.ts b/src/panels/lovelace/special-rows/hui-attribute-row.ts index 77b4411271..3a144df3d9 100644 --- a/src/panels/lovelace/special-rows/hui-attribute-row.ts +++ b/src/panels/lovelace/special-rows/hui-attribute-row.ts @@ -67,7 +67,8 @@ class HuiAttributeRow extends LitElement implements LovelaceRow { : attribute !== undefined ? html`