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 <paul.bottein@gmail.com>

* Prettier

---------

Co-authored-by: Paul Bottein <paul.bottein@gmail.com>
This commit is contained in:
ildar170975 2026-01-23 18:13:24 +03:00 committed by GitHub
parent bd33a94749
commit c8471cb623
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -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`<pre>${until(yaml, "")}</pre>`;
}
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);
}

View File

@ -67,7 +67,8 @@ class HuiAttributeRow extends LitElement implements LovelaceRow {
: attribute !== undefined
? html`
<ha-attribute-value
.hideUnit=${this._config.suffix}
.hideUnit=${this._config.suffix !== undefined &&
this._config.suffix !== ""}
.hass=${this.hass}
.stateObj=${stateObj}
.attribute=${this._config.attribute}