Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort related automations, scripts and scenes on device info page #23740

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 6 additions & 39 deletions src/components/ha-related-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import { customElement, property, state } from "lit/decorators";
import { styleMap } from "lit/directives/style-map";
import memoizeOne from "memoize-one";
import { fireEvent } from "../common/dom/fire_event";
import { caseInsensitiveStringCompare } from "../common/string/compare";
import type { Blueprints } from "../data/blueprint";
import { fetchBlueprints } from "../data/blueprint";
import type { ConfigEntry } from "../data/config_entries";
import { getConfigEntries } from "../data/config_entries";
import type { ItemType, RelatedResult } from "../data/search";
import { findRelated } from "../data/search";
import { sortRelated, findRelated } from "../data/search";
import { haStyle } from "../resources/styles";
import type { HomeAssistant } from "../types";
import { brandsUrl } from "../util/brands-url";
Expand Down Expand Up @@ -74,38 +73,6 @@ export class HaRelatedItems extends LitElement {
}
}

private _relatedEntities = memoizeOne((entityIds: string[]) =>
this._toEntities(entityIds)
);

private _relatedAutomations = memoizeOne((automationEntityIds: string[]) =>
this._toEntities(automationEntityIds)
);

private _relatedScripts = memoizeOne((scriptEntityIds: string[]) =>
this._toEntities(scriptEntityIds)
);

private _relatedGroups = memoizeOne((groupEntityIds: string[]) =>
this._toEntities(groupEntityIds)
);

private _relatedScenes = memoizeOne((sceneEntityIds: string[]) =>
this._toEntities(sceneEntityIds)
);

private _toEntities = (entityIds: string[]) =>
entityIds
.map((entityId) => this.hass.states[entityId])
.filter((entity) => entity)
.sort((a, b) =>
caseInsensitiveStringCompare(
a.attributes.friendly_name ?? a.entity_id,
b.attributes.friendly_name ?? b.entity_id,
this.hass.language
)
);

private _getConfigEntries = memoizeOne(
(
relatedConfigEntries: string[] | undefined,
Expand Down Expand Up @@ -275,7 +242,7 @@ export class HaRelatedItems extends LitElement {
? html`
<h3>${this.hass.localize("ui.components.related-items.entity")}</h3>
<mwc-list>
${this._relatedEntities(this._related.entity).map(
${sortRelated(this.hass, this._related.entity).map(
(entity) => html`
<ha-list-item
@click=${this._openMoreInfo}
Expand All @@ -300,7 +267,7 @@ export class HaRelatedItems extends LitElement {
? html`
<h3>${this.hass.localize("ui.components.related-items.group")}</h3>
<mwc-list>
${this._relatedGroups(this._related.group).map(
${sortRelated(this.hass, this._related.group).map(
(group) => html`
<ha-list-item
@click=${this._openMoreInfo}
Expand All @@ -325,7 +292,7 @@ export class HaRelatedItems extends LitElement {
? html`
<h3>${this.hass.localize("ui.components.related-items.scene")}</h3>
<mwc-list>
${this._relatedScenes(this._related.scene).map(
${sortRelated(this.hass, this._related.scene).map(
(scene) => html`
<ha-list-item
@click=${this._openMoreInfo}
Expand Down Expand Up @@ -378,7 +345,7 @@ export class HaRelatedItems extends LitElement {
${this.hass.localize("ui.components.related-items.automation")}
</h3>
<mwc-list>
${this._relatedAutomations(this._related.automation).map(
${sortRelated(this.hass, this._related.automation).map(
(automation) => html`
<ha-list-item
@click=${this._openMoreInfo}
Expand Down Expand Up @@ -430,7 +397,7 @@ export class HaRelatedItems extends LitElement {
? html`
<h3>${this.hass.localize("ui.components.related-items.script")}</h3>
<mwc-list>
${this._relatedScripts(this._related.script).map(
${sortRelated(this.hass, this._related.script).map(
(script) => html`
<ha-list-item
@click=${this._openMoreInfo}
Expand Down
16 changes: 16 additions & 0 deletions src/data/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import memoizeOne from "memoize-one";
import type { HomeAssistant } from "../types";
import { caseInsensitiveStringCompare } from "../common/string/compare";

export interface RelatedResult {
area?: string[];
Expand Down Expand Up @@ -45,3 +47,17 @@ export const findRelated = (
item_type: itemType,
item_id: itemId,
});

export const sortRelated = memoizeOne(
(hass: HomeAssistant, entityIds: string[]) =>
entityIds
.map((entityId) => hass.states[entityId])
.filter((entity) => entity)
.sort((a, b) =>
caseInsensitiveStringCompare(
a.attributes.friendly_name ?? a.entity_id,
b.attributes.friendly_name ?? b.entity_id,
hass.language
)
)
);
jpbede marked this conversation as resolved.
Show resolved Hide resolved
172 changes: 88 additions & 84 deletions src/panels/config/devices/ha-config-device-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { domainToName } from "../../../data/integration";
import type { SceneEntities } from "../../../data/scene";
import { showSceneEditor } from "../../../data/scene";
import type { RelatedResult } from "../../../data/search";
import { findRelated } from "../../../data/search";
import { sortRelated, findRelated } from "../../../data/search";
import {
showAlertDialog,
showConfirmationDialog,
Expand Down Expand Up @@ -441,38 +441,38 @@ export class HaConfigDevicePage extends LitElement {
${this._related?.automation?.length
? html`
<div class="items">
${this._related.automation.map((automation) => {
const entityState = this.hass.states[automation];
return entityState
? html`<div>
<a
href=${ifDefined(
entityState.attributes.id
? `/config/automation/edit/${encodeURIComponent(entityState.attributes.id)}`
: undefined
)}
>
<ha-list-item
hasMeta
.automation=${entityState}
.disabled=${!entityState.attributes.id}
${sortRelated(this.hass, this._related.automation).map(
(automationState) =>
automationState
? html`<div>
<a
href=${ifDefined(
automationState.attributes.id
? `/config/automation/edit/${encodeURIComponent(automationState.attributes.id)}`
: undefined
)}
>
${computeStateName(entityState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
${!entityState.attributes.id
? html`
<simple-tooltip animation-delay="0">
${this.hass.localize(
"ui.panel.config.devices.cant_edit"
)}
</simple-tooltip>
`
: ""}
</div> `
: "";
})}
<ha-list-item
hasMeta
.automation=${automationState}
.disabled=${!automationState.attributes.id}
>
${computeStateName(automationState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
${!automationState.attributes.id
? html`
<simple-tooltip animation-delay="0">
${this.hass.localize(
"ui.panel.config.devices.cant_edit"
)}
</simple-tooltip>
`
: ""}
</div> `
: ""
)}
</div>
`
: html`
Expand Down Expand Up @@ -532,40 +532,40 @@ export class HaConfigDevicePage extends LitElement {
${this._related?.scene?.length
? html`
<div class="items">
${this._related.scene.map((scene) => {
const entityState = this.hass.states[scene];
return entityState
? html`
<div>
<a
href=${ifDefined(
entityState.attributes.id
? `/config/scene/edit/${entityState.attributes.id}`
: undefined
)}
>
<ha-list-item
hasMeta
.scene=${entityState}
.disabled=${!entityState.attributes.id}
${sortRelated(this.hass, this._related.scene).map(
(sceneState) =>
sceneState
? html`
<div>
<a
href=${ifDefined(
sceneState.attributes.id
? `/config/scene/edit/${sceneState.attributes.id}`
: undefined
)}
>
${computeStateName(entityState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
${!entityState.attributes.id
? html`
<simple-tooltip animation-delay="0">
${this.hass.localize(
"ui.panel.config.devices.cant_edit"
)}
</simple-tooltip>
`
: ""}
</div>
`
: "";
})}
<ha-list-item
hasMeta
.scene=${sceneState}
.disabled=${!sceneState.attributes.id}
>
${computeStateName(sceneState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
${!sceneState.attributes.id
? html`
<simple-tooltip animation-delay="0">
${this.hass.localize(
"ui.panel.config.devices.cant_edit"
)}
</simple-tooltip>
`
: ""}
</div>
`
: ""
)}
</div>
`
: html`
Expand Down Expand Up @@ -626,26 +626,30 @@ export class HaConfigDevicePage extends LitElement {
${this._related?.script?.length
? html`
<div class="items">
${this._related.script.map((script) => {
const entityState = this.hass.states[script];
const entry = this._entityReg.find(
(e) => e.entity_id === script
);
let url = `/config/script/show/${entityState.entity_id}`;
if (entry) {
url = `/config/script/edit/${entry.unique_id}`;
${sortRelated(this.hass, this._related.script).map(
(scriptState) => {
const entry = this._entityReg.find(
(e) => e.entity_id === scriptState.entity_id
);
let url = `/config/script/show/${scriptState.entity_id}`;
if (entry) {
url = `/config/script/edit/${entry.unique_id}`;
}
return scriptState
? html`
<a href=${url}>
<ha-list-item
hasMeta
.script=${scriptState.entity_id}
>
${computeStateName(scriptState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
`
: "";
}
return entityState
? html`
<a href=${url}>
<ha-list-item hasMeta .script=${script}>
${computeStateName(entityState)}
<ha-icon-next slot="meta"></ha-icon-next>
</ha-list-item>
</a>
`
: "";
})}
)}
</div>
`
: html`
Expand Down
Loading