mirror of
https://github.com/home-assistant/frontend.git
synced 2026-02-04 01:10:33 -06:00
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:
parent
bd33a94749
commit
c8471cb623
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user