diff --git a/src/data/history.ts b/src/data/history.ts index 29dfa5a285bf..f7a2cc20326f 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -469,15 +469,13 @@ export const computeHistory = ( let unit: string | undefined; - const isNumeric = - forceNumeric || - isNumericFromDomain(domain) || - (currentState != null && - isNumericFromAttributes(currentState.attributes)) || - (currentState != null && - domain === "sensor" && - isNumericSensorEntity(currentState, sensorNumericalDeviceClasses)) || - numericStateFromHistory != null; + const isNumeric = isNumericEntity( + domain, + currentState, + numericStateFromHistory, + sensorNumericalDeviceClasses, + forceNumeric + ); if (isNumeric) { unit = @@ -551,3 +549,18 @@ export const computeGroupKey = ( device_class: string | undefined, splitDeviceClasses: boolean ) => (splitDeviceClasses ? `${unit}_${device_class || ""}` : unit); + +export const isNumericEntity = ( + domain: string, + currentState: HassEntity | undefined, + numericStateFromHistory: EntityHistoryState | undefined, + sensorNumericalDeviceClasses: string[], + forceNumeric = false +): boolean => + forceNumeric || + isNumericFromDomain(domain) || + (currentState != null && isNumericFromAttributes(currentState.attributes)) || + (currentState != null && + domain === "sensor" && + isNumericSensorEntity(currentState, sensorNumericalDeviceClasses)) || + numericStateFromHistory != null; diff --git a/src/dialogs/more-info/const.ts b/src/dialogs/more-info/const.ts index e75fd38516d8..76b51ae79123 100644 --- a/src/dialogs/more-info/const.ts +++ b/src/dialogs/more-info/const.ts @@ -5,6 +5,7 @@ import type { GroupEntity } from "../../data/group"; import { computeGroupDomain } from "../../data/group"; import { CONTINUOUS_DOMAINS } from "../../data/logbook"; import type { HomeAssistant } from "../../types"; +import { isNumericEntity } from "../../data/history"; export const DOMAINS_NO_INFO = ["camera", "configurator"]; /** @@ -98,20 +99,27 @@ export const computeShowHistoryComponent = ( export const computeShowLogBookComponent = ( hass: HomeAssistant, - entityId: string + entityId: string, + sensorNumericalDeviceClasses: string[] = [] ): boolean => { if (!isComponentLoaded(hass, "logbook")) { return false; } const stateObj = hass.states[entityId]; - if (!stateObj || stateObj.attributes.unit_of_measurement) { + if (!stateObj) { return false; } const domain = computeDomain(entityId); if ( - CONTINUOUS_DOMAINS.includes(domain) || + (CONTINUOUS_DOMAINS.includes(domain) && + isNumericEntity( + domain, + stateObj, + undefined, + sensorNumericalDeviceClasses + )) || DOMAINS_MORE_INFO_NO_HISTORY.includes(domain) ) { return false; diff --git a/src/dialogs/more-info/ha-more-info-dialog.ts b/src/dialogs/more-info/ha-more-info-dialog.ts index 51d14f1cce77..c37e337aa3d0 100644 --- a/src/dialogs/more-info/ha-more-info-dialog.ts +++ b/src/dialogs/more-info/ha-more-info-dialog.ts @@ -52,6 +52,7 @@ import "./ha-more-info-info"; import type { MoreInfoInfo } from "./ha-more-info-info"; import "./ha-more-info-settings"; import "./more-info-content"; +import { getSensorNumericDeviceClasses } from "../../data/sensor"; export interface MoreInfoDialogParams { entityId: string | null; @@ -101,6 +102,8 @@ export class MoreInfoDialog extends LitElement { @query("ha-more-info-info, ha-more-info-history-and-logbook") private _history?: MoreInfoInfo | MoreInfoHistoryAndLogbook; + @state() private _sensorNumericDeviceClasses?: string[] = []; + public showDialog(params: MoreInfoDialogParams) { this._entityId = params.entityId; if (!this._entityId) { @@ -161,7 +164,11 @@ export class MoreInfoDialog extends LitElement { return ( DOMAINS_WITH_MORE_INFO.includes(domain) && (computeShowHistoryComponent(this.hass, this._entityId!) || - computeShowLogBookComponent(this.hass, this._entityId!)) + computeShowLogBookComponent( + this.hass, + this._entityId!, + this._sensorNumericDeviceClasses + )) ); } @@ -258,6 +265,11 @@ export class MoreInfoDialog extends LitElement { this._setView("related"); } + private async _loadNumericDeviceClasses() { + const deviceClasses = await getSensorNumericDeviceClasses(this.hass); + this._sensorNumericDeviceClasses = deviceClasses.numeric_device_classes; + } + protected render() { if (!this._entityId) { return nothing; @@ -515,6 +527,7 @@ export class MoreInfoDialog extends LitElement { protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); this.addEventListener("close-dialog", () => this.closeDialog()); + this._loadNumericDeviceClasses(); } protected updated(changedProps: PropertyValues) { diff --git a/src/dialogs/more-info/ha-more-info-history-and-logbook.ts b/src/dialogs/more-info/ha-more-info-history-and-logbook.ts index a4aa7698698e..3ce6261995a6 100644 --- a/src/dialogs/more-info/ha-more-info-history-and-logbook.ts +++ b/src/dialogs/more-info/ha-more-info-history-and-logbook.ts @@ -1,6 +1,6 @@ import type { CSSResultGroup } from "lit"; import { css, html, LitElement } from "lit"; -import { customElement, property, query } from "lit/decorators"; +import { customElement, property, query, state } from "lit/decorators"; import type { ChartResizeOptions } from "../../components/chart/ha-chart-base"; import type { HomeAssistant } from "../../types"; import { @@ -10,6 +10,7 @@ import { import "./ha-more-info-history"; import type { MoreInfoHistory } from "./ha-more-info-history"; import "./ha-more-info-logbook"; +import { getSensorNumericDeviceClasses } from "../../data/sensor"; @customElement("ha-more-info-history-and-logbook") export class MoreInfoHistoryAndLogbook extends LitElement { @@ -20,6 +21,18 @@ export class MoreInfoHistoryAndLogbook extends LitElement { @query("ha-more-info-history") private _history?: MoreInfoHistory; + @state() private _sensorNumericDeviceClasses?: string[] = []; + + private async _loadNumericDeviceClasses() { + const deviceClasses = await getSensorNumericDeviceClasses(this.hass); + this._sensorNumericDeviceClasses = deviceClasses.numeric_device_classes; + } + + protected firstUpdated(changedProps) { + super.firstUpdated(changedProps); + this._loadNumericDeviceClasses(); + } + public resize(options?: ChartResizeOptions) { this._history?.resize(options); } @@ -34,7 +47,11 @@ export class MoreInfoHistoryAndLogbook extends LitElement { > ` : ""} - ${computeShowLogBookComponent(this.hass, this.entityId) + ${computeShowLogBookComponent( + this.hass, + this.entityId, + this._sensorNumericDeviceClasses + ) ? html` `} ${DOMAINS_WITH_MORE_INFO.includes(domain) || - !computeShowLogBookComponent(this.hass, entityId) + !computeShowLogBookComponent( + this.hass, + entityId, + this._sensorNumericDeviceClasses + ) ? "" : html`