Skip to content

Commit

Permalink
Show logbook component in more info for non-numeric values (#22997)
Browse files Browse the repository at this point in the history
* if isNumeric ignore CONTINUOUS_DOMAINS

* use isNumeric  logic from history.ts

* removed check for stateObj.attributes.unit_of_measurement

* pass numericDevicesClasses to computeShowLogBookComponent

---------

Co-authored-by: Petar Petrov <[email protected]>
  • Loading branch information
boern99 and MindFreeze authored Jan 8, 2025
1 parent 85bebfc commit c50f701
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
31 changes: 22 additions & 9 deletions src/data/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
14 changes: 11 additions & 3 deletions src/dialogs/more-info/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
/**
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion src/dialogs/more-info/ha-more-info-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
))
);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 19 additions & 2 deletions src/dialogs/more-info/ha-more-info-history-and-logbook.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -34,7 +47,11 @@ export class MoreInfoHistoryAndLogbook extends LitElement {
></ha-more-info-history>
`
: ""}
${computeShowLogBookComponent(this.hass, this.entityId)
${computeShowLogBookComponent(
this.hass,
this.entityId,
this._sensorNumericDeviceClasses
)
? html`
<ha-more-info-logbook
.hass=${this.hass}
Expand Down
21 changes: 19 additions & 2 deletions src/dialogs/more-info/ha-more-info-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HassEntity } from "home-assistant-js-websocket";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, query } from "lit/decorators";
import { customElement, property, query, state } from "lit/decorators";
import { computeDomain } from "../../common/entity/compute_domain";
import type { ChartResizeOptions } from "../../components/chart/ha-chart-base";
import type { ExtEntityRegistryEntry } from "../../data/entity_registry";
Expand All @@ -17,6 +17,7 @@ import "./ha-more-info-history";
import type { MoreInfoHistory } from "./ha-more-info-history";
import "./ha-more-info-logbook";
import "./more-info-content";
import { getSensorNumericDeviceClasses } from "../../data/sensor";

@customElement("ha-more-info-info")
export class MoreInfoInfo extends LitElement {
Expand All @@ -28,9 +29,21 @@ export class MoreInfoInfo extends LitElement {

@property({ attribute: false }) public editMode?: boolean;

@state() private _sensorNumericDeviceClasses?: string[] = [];

@query("ha-more-info-history")
private _history?: MoreInfoHistory;

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);
}
Expand Down Expand Up @@ -85,7 +98,11 @@ export class MoreInfoInfo extends LitElement {
.entityId=${this.entityId}
></ha-more-info-history>`}
${DOMAINS_WITH_MORE_INFO.includes(domain) ||
!computeShowLogBookComponent(this.hass, entityId)
!computeShowLogBookComponent(
this.hass,
entityId,
this._sensorNumericDeviceClasses
)
? ""
: html`<ha-more-info-logbook
.hass=${this.hass}
Expand Down

0 comments on commit c50f701

Please sign in to comment.