mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +00:00
Use core area sorting everywhere (#28304)
This commit is contained in:
committed by
Bram Kragten
parent
109c81a00d
commit
24b16360a6
@@ -4,7 +4,6 @@ import { LitElement, html } from "lit";
|
||||
import { customElement, property } from "lit/decorators";
|
||||
import { fireEvent } from "../common/dom/fire_event";
|
||||
import { getAreaContext } from "../common/entity/context/get_area_context";
|
||||
import { areaCompare } from "../data/area_registry";
|
||||
import type { HomeAssistant } from "../types";
|
||||
import "./ha-expansion-panel";
|
||||
import "./ha-items-display-editor";
|
||||
@@ -37,11 +36,7 @@ export class HaAreasDisplayEditor extends LitElement {
|
||||
public showNavigationButton = false;
|
||||
|
||||
protected render(): TemplateResult {
|
||||
const compare = areaCompare(this.hass.areas);
|
||||
|
||||
const areas = Object.values(this.hass.areas).sort((areaA, areaB) =>
|
||||
compare(areaA.area_id, areaB.area_id)
|
||||
);
|
||||
const areas = Object.values(this.hass.areas);
|
||||
|
||||
const items: DisplayItem[] = areas.map((area) => {
|
||||
const { floor } = getAreaContext(area, this.hass.floors);
|
||||
|
||||
@@ -7,7 +7,6 @@ import memoizeOne from "memoize-one";
|
||||
import { fireEvent } from "../common/dom/fire_event";
|
||||
import { computeFloorName } from "../common/entity/compute_floor_name";
|
||||
import { getAreaContext } from "../common/entity/context/get_area_context";
|
||||
import { areaCompare } from "../data/area_registry";
|
||||
import type { FloorRegistryEntry } from "../data/floor_registry";
|
||||
import { getFloors } from "../panels/lovelace/strategies/areas/helpers/areas-strategy-helper";
|
||||
import type { HomeAssistant } from "../types";
|
||||
@@ -131,11 +130,8 @@ export class HaAreasFloorsDisplayEditor extends LitElement {
|
||||
// update items if floors change
|
||||
_hassFloors: HomeAssistant["floors"]
|
||||
): Record<string, DisplayItem[]> => {
|
||||
const compare = areaCompare(hassAreas);
|
||||
const areas = Object.values(hassAreas);
|
||||
|
||||
const areas = Object.values(hassAreas).sort((areaA, areaB) =>
|
||||
compare(areaA.area_id, areaB.area_id)
|
||||
);
|
||||
const groupedItems: Record<string, DisplayItem[]> = areas.reduce(
|
||||
(acc, area) => {
|
||||
const { floor } = getAreaContext(area, this.hass.floors);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { stringCompare } from "../common/string/compare";
|
||||
import type { HomeAssistant } from "../types";
|
||||
import type { DeviceRegistryEntry } from "./device_registry";
|
||||
import type {
|
||||
@@ -105,22 +104,3 @@ export const getAreaDeviceLookup = (
|
||||
}
|
||||
return areaDeviceLookup;
|
||||
};
|
||||
|
||||
export const areaCompare =
|
||||
(entries?: HomeAssistant["areas"], order?: string[]) =>
|
||||
(a: string, b: string) => {
|
||||
const indexA = order ? order.indexOf(a) : -1;
|
||||
const indexB = order ? order.indexOf(b) : -1;
|
||||
if (indexA === -1 && indexB === -1) {
|
||||
const nameA = entries?.[a]?.name ?? a;
|
||||
const nameB = entries?.[b]?.name ?? b;
|
||||
return stringCompare(nameA, nameB);
|
||||
}
|
||||
if (indexA === -1) {
|
||||
return 1;
|
||||
}
|
||||
if (indexB === -1) {
|
||||
return -1;
|
||||
}
|
||||
return indexA - indexB;
|
||||
};
|
||||
|
||||
@@ -5,10 +5,9 @@ import { computeStateDomain } from "../../../common/entity/compute_state_domain"
|
||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||
import { splitByGroups } from "../../../common/entity/split_by_groups";
|
||||
import { stripPrefixFromEntityName } from "../../../common/entity/strip_prefix_from_entity_name";
|
||||
import { stringCompare } from "../../../common/string/compare";
|
||||
import { orderCompare, stringCompare } from "../../../common/string/compare";
|
||||
import type { LocalizeFunc } from "../../../common/translations/localize";
|
||||
import type { AreasDisplayValue } from "../../../components/ha-areas-display-editor";
|
||||
import { areaCompare } from "../../../data/area_registry";
|
||||
import type {
|
||||
EnergyPreferences,
|
||||
GridSourceTypeEnergyPreference,
|
||||
@@ -572,13 +571,21 @@ export const generateDefaultViewConfig = (
|
||||
|
||||
const areaCards: LovelaceCardConfig[] = [];
|
||||
|
||||
const sortedAreas = Object.keys(splittedByAreaDevice.areasWithEntities).sort(
|
||||
areaCompare(areaEntries, areasPrefs?.order)
|
||||
);
|
||||
const areaIds = Object.keys(areaEntries);
|
||||
|
||||
for (const areaId of sortedAreas) {
|
||||
if (areasPrefs?.order) {
|
||||
const areaOrder = areasPrefs.order;
|
||||
areaIds.sort(orderCompare(areaOrder));
|
||||
}
|
||||
|
||||
for (const areaId of areaIds) {
|
||||
// Skip areas with no entities
|
||||
if (!(areaId in splittedByAreaDevice.areasWithEntities)) {
|
||||
continue;
|
||||
}
|
||||
const areaEntities = splittedByAreaDevice.areasWithEntities[areaId];
|
||||
const area = areaEntries[areaId];
|
||||
|
||||
areaCards.push(
|
||||
...computeCards(
|
||||
hass,
|
||||
|
||||
@@ -5,7 +5,6 @@ import { generateEntityFilter } from "../../../../../common/entity/entity_filter
|
||||
import { stripPrefixFromEntityName } from "../../../../../common/entity/strip_prefix_from_entity_name";
|
||||
import { orderCompare } from "../../../../../common/string/compare";
|
||||
import type { AreaRegistryEntry } from "../../../../../data/area_registry";
|
||||
import { areaCompare } from "../../../../../data/area_registry";
|
||||
import type { FloorRegistryEntry } from "../../../../../data/floor_registry";
|
||||
import type { LovelaceCardConfig } from "../../../../../data/lovelace/config/card";
|
||||
import type { HomeAssistant } from "../../../../../types";
|
||||
@@ -287,7 +286,11 @@ export const getAreas = (
|
||||
? areas.filter((area) => !hiddenAreas!.includes(area.area_id))
|
||||
: areas.concat();
|
||||
|
||||
const compare = areaCompare(entries, areasOrder);
|
||||
if (!areasOrder) {
|
||||
return filteredAreas;
|
||||
}
|
||||
|
||||
const compare = orderCompare(areasOrder);
|
||||
|
||||
const sortedAreas = filteredAreas.sort((areaA, areaB) =>
|
||||
compare(areaA.area_id, areaB.area_id)
|
||||
|
||||
Reference in New Issue
Block a user