diff --git a/src/panels/config/devices/ha-config-device-page.ts b/src/panels/config/devices/ha-config-device-page.ts index 44530ab333..4220645aaa 100644 --- a/src/panels/config/devices/ha-config-device-page.ts +++ b/src/panels/config/devices/ha-config-device-page.ts @@ -15,6 +15,7 @@ import { LitElement, css, html, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; import { ifDefined } from "lit/directives/if-defined"; import memoizeOne from "memoize-one"; +import type { HassEntity } from "home-assistant-js-websocket"; import { isComponentLoaded } from "../../../common/config/is_component_loaded"; import { ASSIST_ENTITIES, SENSOR_ENTITIES } from "../../../common/const"; import { computeDeviceNameDisplay } from "../../../common/entity/compute_device_name"; @@ -185,6 +186,27 @@ export class HaConfigDevicePage extends LitElement { ) ); + private _getEntitiesSorted = (entities: HassEntity[]) => + entities.sort((ent1, ent2) => + stringCompare( + ent1.attributes.friendly_name || `zzz${ent1.entity_id}`, + ent2.attributes.friendly_name || `zzz${ent2.entity_id}`, + this.hass.locale.language + ) + ); + + private _getRelated = memoizeOne((related?: RelatedResult) => ({ + automation: this._getEntitiesSorted( + (related?.automation ?? []).map((entityId) => this.hass.states[entityId]) + ), + scene: this._getEntitiesSorted( + (related?.scene ?? []).map((entityId) => this.hass.states[entityId]) + ), + script: this._getEntitiesSorted( + (related?.script ?? []).map((entityId) => this.hass.states[entityId]) + ), + })); + private _deviceIdInList = memoizeOne((deviceId: string) => [deviceId]); private _entityIds = memoizeOne( @@ -433,23 +455,25 @@ export class HaConfigDevicePage extends LitElement { ${this._related?.automation?.length ? html`
- ${this._related.automation.map((automation) => { - const entityState = this.hass.states[automation]; - return entityState - ? html` - - ${computeStateName(entityState)} - - - ` - : nothing; - })} + ${this._getRelated(this._related).automation.map( + (automation) => { + const entityState = automation; + return entityState + ? html` + + ${computeStateName(entityState)} + + + ` + : nothing; + } + )}
` : html` @@ -510,8 +534,8 @@ export class HaConfigDevicePage extends LitElement { ${this._related?.scene?.length ? html`
- ${this._related.scene.map((scene) => { - const entityState = this.hass.states[scene]; + ${this._getRelated(this._related).scene.map((scene) => { + const entityState = scene; return entityState && entityState.attributes.id ? html` - ${this._related.script.map((script) => { - const entityState = this.hass.states[script]; + ${this._getRelated(this._related).script.map((script) => { + const entityState = script; const entry = this._entityReg.find( - (e) => e.entity_id === script + (e) => e.entity_id === script.entity_id ); let url = `/config/script/show/${entityState.entity_id}`; if (entry) {