1
0
mirror of https://github.com/home-assistant/frontend.git synced 2026-05-02 14:42:52 +01:00

Compute shown entity attributes in one place (#29655)

* Compute shown entity attributes in one place

* Remove type assertion

* Unit tests
This commit is contained in:
Aidan Timson
2026-02-16 11:54:01 +00:00
committed by GitHub
parent b0d272bc3d
commit 20cde0ef70
4 changed files with 70 additions and 33 deletions

View File

@@ -0,0 +1,52 @@
import type { HassEntity } from "home-assistant-js-websocket";
import { describe, expect, it } from "vitest";
import { computeShownAttributes } from "../../src/data/entity/entity_attributes";
describe("computeShownAttributes", () => {
it("filters globally hidden attributes", () => {
const stateObj = {
entity_id: "sensor.temperature",
attributes: {
friendly_name: "Office temperature",
unit_of_measurement: "°C",
temperature: 21,
custom_value: "shown",
},
} as unknown as HassEntity;
expect(computeShownAttributes(stateObj)).toEqual([
"temperature",
"custom_value",
]);
});
it("filters domain and device class specific attributes", () => {
const stateObj = {
entity_id: "sensor.status",
attributes: {
device_class: "enum",
options: ["home", "away"],
current_option: "home",
},
} as unknown as HassEntity;
expect(computeShownAttributes(stateObj)).toEqual(["current_option"]);
});
it("keeps device-class attributes for other device classes", () => {
const stateObj = {
entity_id: "sensor.status",
attributes: {
device_class: "temperature",
options: ["home", "away"],
current_option: "home",
},
} as unknown as HassEntity;
expect(computeShownAttributes(stateObj)).toEqual([
"options",
"current_option",
]);
});
});