Skip to content

Commit 2313750

Browse files
authored
Add TCA by target sort like item collections (#28192)
1 parent 63e7ed2 commit 2313750

File tree

1 file changed

+71
-8
lines changed

1 file changed

+71
-8
lines changed

src/panels/config/automation/add-automation-element-dialog.ts

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,61 @@ class DialogAddAutomationElement
13501350
this._labelRegistry?.find(({ label_id }) => label_id === labelId)
13511351
);
13521352

1353+
private _getDomainType(domain: string) {
1354+
return ENTITY_DOMAINS_MAIN.has(domain) ||
1355+
(this._manifests?.[domain].integration_type === "entity" &&
1356+
!ENTITY_DOMAINS_OTHER.has(domain))
1357+
? "dynamicGroups"
1358+
: this._manifests?.[domain].integration_type === "helper"
1359+
? "helpers"
1360+
: "other";
1361+
}
1362+
1363+
private _sortDomainsByCollection(
1364+
type: AddAutomationElementDialogParams["type"],
1365+
entries: [
1366+
string,
1367+
{ title: string; items: AddAutomationElementListItem[] },
1368+
][]
1369+
): { title: string; items: AddAutomationElementListItem[] }[] {
1370+
const order: string[] = [];
1371+
1372+
TYPES[type].collections.forEach((collection) => {
1373+
order.push(...Object.keys(collection.groups));
1374+
});
1375+
1376+
return entries
1377+
.sort((a, b) => {
1378+
const domainA = a[0];
1379+
const domainB = b[0];
1380+
1381+
if (order.includes(domainA) && order.includes(domainB)) {
1382+
return order.indexOf(domainA) - order.indexOf(domainB);
1383+
}
1384+
1385+
let typeA = domainA;
1386+
let typeB = domainB;
1387+
1388+
if (!order.includes(domainA)) {
1389+
typeA = this._getDomainType(domainA);
1390+
}
1391+
1392+
if (!order.includes(domainB)) {
1393+
typeB = this._getDomainType(domainB);
1394+
}
1395+
1396+
if (typeA === typeB) {
1397+
return stringCompare(
1398+
a[1].title,
1399+
b[1].title,
1400+
this.hass.locale.language
1401+
);
1402+
}
1403+
return order.indexOf(typeA) - order.indexOf(typeB);
1404+
})
1405+
.map((entry) => entry[1]);
1406+
}
1407+
13531408
// #endregion data
13541409

13551410
// #region data memoize
@@ -1435,8 +1490,9 @@ class DialogAddAutomationElement
14351490
);
14361491
});
14371492

1438-
return Object.values(items).sort((a, b) =>
1439-
stringCompare(a.title, b.title, this.hass.locale.language)
1493+
return this._sortDomainsByCollection(
1494+
this._params!.type,
1495+
Object.entries(items)
14401496
);
14411497
}
14421498

@@ -1545,8 +1601,9 @@ class DialogAddAutomationElement
15451601
);
15461602
});
15471603

1548-
return Object.values(items).sort((a, b) =>
1549-
stringCompare(a.title, b.title, this.hass.locale.language)
1604+
return this._sortDomainsByCollection(
1605+
this._params!.type,
1606+
Object.entries(items)
15501607
);
15511608
}
15521609

@@ -1577,8 +1634,9 @@ class DialogAddAutomationElement
15771634
);
15781635
});
15791636

1580-
return Object.values(items).sort((a, b) =>
1581-
stringCompare(a.title, b.title, this.hass.locale.language)
1637+
return this._sortDomainsByCollection(
1638+
this._params!.type,
1639+
Object.entries(items)
15821640
);
15831641
}
15841642

@@ -1675,14 +1733,19 @@ class DialogAddAutomationElement
16751733
}
16761734

16771735
if (this._params!.type === "action") {
1678-
const items = await getServicesForTarget(
1736+
const items: string[] = await getServicesForTarget(
16791737
this.hass.callWS,
16801738
this._selectedTarget
16811739
);
16821740

1741+
const filteredItems = items.filter(
1742+
// homeassistant services are too generic to be applied on the selected target
1743+
(service) => !service.startsWith("homeassistant.")
1744+
);
1745+
16831746
this._targetItems = this._getDomainGroupedActionListItems(
16841747
this.hass.localize,
1685-
items
1748+
filteredItems
16861749
);
16871750
}
16881751
} catch (err) {

0 commit comments

Comments
 (0)