mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-31 05:06:38 +00:00
Use entity filter to get device classes in editor
This commit is contained in:
parent
0474a24df6
commit
ebb98bd196
@ -2,8 +2,8 @@ import { html, LitElement, nothing } from "lit";
|
|||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import {
|
import {
|
||||||
assert,
|
|
||||||
array,
|
array,
|
||||||
|
assert,
|
||||||
assign,
|
assign,
|
||||||
boolean,
|
boolean,
|
||||||
object,
|
object,
|
||||||
@ -11,21 +11,21 @@ import {
|
|||||||
string,
|
string,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
|
import { generateEntityFilter } from "../../../../common/entity/entity_filter";
|
||||||
|
import { caseInsensitiveStringCompare } from "../../../../common/string/compare";
|
||||||
|
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
||||||
import "../../../../components/ha-form/ha-form";
|
import "../../../../components/ha-form/ha-form";
|
||||||
import {
|
|
||||||
DEFAULT_ASPECT_RATIO,
|
|
||||||
DEVICE_CLASSES,
|
|
||||||
} from "../../cards/hui-area-card";
|
|
||||||
import type { SchemaUnion } from "../../../../components/ha-form/types";
|
import type { SchemaUnion } from "../../../../components/ha-form/types";
|
||||||
|
import type { SelectOption } from "../../../../data/selector";
|
||||||
|
import { getSensorNumericDeviceClasses } from "../../../../data/sensor";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
|
import {
|
||||||
|
DEVICE_CLASSES,
|
||||||
|
DEFAULT_ASPECT_RATIO,
|
||||||
|
} from "../../cards/hui-area-card";
|
||||||
import type { AreaCardConfig } from "../../cards/types";
|
import type { AreaCardConfig } from "../../cards/types";
|
||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { computeDomain } from "../../../../common/entity/compute_domain";
|
|
||||||
import { caseInsensitiveStringCompare } from "../../../../common/string/compare";
|
|
||||||
import type { SelectOption } from "../../../../data/selector";
|
|
||||||
import { getSensorNumericDeviceClasses } from "../../../../data/sensor";
|
|
||||||
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@ -122,42 +122,49 @@ export class HuiAreaCardEditor
|
|||||||
] as const
|
] as const
|
||||||
);
|
);
|
||||||
|
|
||||||
private _binaryClassesForArea = memoizeOne((area: string): string[] =>
|
private _binaryClassesForArea = memoizeOne(
|
||||||
this._classesForArea(area, "binary_sensor")
|
(area: string | undefined): string[] => {
|
||||||
|
if (!area) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const binarySensorFilter = generateEntityFilter(this.hass!, {
|
||||||
|
domain: "binary_sensor",
|
||||||
|
area,
|
||||||
|
entity_category: "none",
|
||||||
|
});
|
||||||
|
|
||||||
|
const classes = Object.keys(this.hass!.entities)
|
||||||
|
.filter(binarySensorFilter)
|
||||||
|
.map((id) => this.hass!.states[id]?.attributes.device_class)
|
||||||
|
.filter((c): c is string => Boolean(c));
|
||||||
|
|
||||||
|
return [...new Set(classes)];
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
private _sensorClassesForArea = memoizeOne(
|
private _sensorClassesForArea = memoizeOne(
|
||||||
(area: string, numericDeviceClasses?: string[]): string[] =>
|
(area: string | undefined, numericDeviceClasses?: string[]): string[] => {
|
||||||
this._classesForArea(area, "sensor", numericDeviceClasses)
|
if (!area) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const sensorFilter = generateEntityFilter(this.hass!, {
|
||||||
|
domain: "sensor",
|
||||||
|
area,
|
||||||
|
device_class: numericDeviceClasses,
|
||||||
|
entity_category: "none",
|
||||||
|
});
|
||||||
|
|
||||||
|
const classes = Object.keys(this.hass!.entities)
|
||||||
|
.filter(sensorFilter)
|
||||||
|
.map((id) => this.hass!.states[id]?.attributes.device_class)
|
||||||
|
.filter((c): c is string => Boolean(c));
|
||||||
|
|
||||||
|
return [...new Set(classes)];
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
private _classesForArea(
|
|
||||||
area: string,
|
|
||||||
domain: "sensor" | "binary_sensor",
|
|
||||||
numericDeviceClasses?: string[] | undefined
|
|
||||||
): string[] {
|
|
||||||
const entities = Object.values(this.hass!.entities).filter(
|
|
||||||
(e) =>
|
|
||||||
computeDomain(e.entity_id) === domain &&
|
|
||||||
!e.entity_category &&
|
|
||||||
!e.hidden &&
|
|
||||||
(e.area_id === area ||
|
|
||||||
(e.device_id && this.hass!.devices[e.device_id]?.area_id === area))
|
|
||||||
);
|
|
||||||
|
|
||||||
const classes = entities
|
|
||||||
.map((e) => this.hass!.states[e.entity_id]?.attributes.device_class || "")
|
|
||||||
.filter(
|
|
||||||
(c) =>
|
|
||||||
c &&
|
|
||||||
(domain !== "sensor" ||
|
|
||||||
!numericDeviceClasses ||
|
|
||||||
numericDeviceClasses.includes(c))
|
|
||||||
);
|
|
||||||
|
|
||||||
return [...new Set(classes)];
|
|
||||||
}
|
|
||||||
|
|
||||||
private _buildBinaryOptions = memoizeOne(
|
private _buildBinaryOptions = memoizeOne(
|
||||||
(possibleClasses: string[], currentClasses: string[]): SelectOption[] =>
|
(possibleClasses: string[], currentClasses: string[]): SelectOption[] =>
|
||||||
this._buildOptions("binary_sensor", possibleClasses, currentClasses)
|
this._buildOptions("binary_sensor", possibleClasses, currentClasses)
|
||||||
@ -207,11 +214,9 @@ export class HuiAreaCardEditor
|
|||||||
return nothing;
|
return nothing;
|
||||||
}
|
}
|
||||||
|
|
||||||
const possibleBinaryClasses = this._binaryClassesForArea(
|
const possibleBinaryClasses = this._binaryClassesForArea(this._config.area);
|
||||||
this._config.area || ""
|
|
||||||
);
|
|
||||||
const possibleSensorClasses = this._sensorClassesForArea(
|
const possibleSensorClasses = this._sensorClassesForArea(
|
||||||
this._config.area || "",
|
this._config.area,
|
||||||
this._numericDeviceClasses
|
this._numericDeviceClasses
|
||||||
);
|
);
|
||||||
const binarySelectOptions = this._buildBinaryOptions(
|
const binarySelectOptions = this._buildBinaryOptions(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user