diff --git a/src/data/data_table_filters.ts b/src/data/data_table_filters.ts index d10f6f8840e7..03c7cc7c097b 100644 --- a/src/data/data_table_filters.ts +++ b/src/data/data_table_filters.ts @@ -1,10 +1,20 @@ export interface DataTableFilters { [key: string]: { - value: string[] | { key: string[] } | undefined; + value: DataTableFiltersValue; items: Set | undefined; }; } +export type DataTableFiltersValue = string[] | { key: string[] } | undefined; + +export interface DataTableFiltersValues { + [key: string]: DataTableFiltersValue; +} + +export interface DataTableFiltersItems { + [key: string]: Set | undefined; +} + export const serializeFilters = (value: DataTableFilters) => { const serializedValue = {}; Object.entries(value).forEach(([key, val]) => { diff --git a/src/panels/config/entities/ha-config-entities.ts b/src/panels/config/entities/ha-config-entities.ts index 13745e6d221d..e1fe064d2c85 100644 --- a/src/panels/config/entities/ha-config-entities.ts +++ b/src/panels/config/entities/ha-config-entities.ts @@ -99,9 +99,8 @@ import "../integrations/ha-integration-overflow-menu"; import { showAddIntegrationDialog } from "../integrations/show-add-integration-dialog"; import { showLabelDetailDialog } from "../labels/show-dialog-label-detail"; import { - serializeFilters, - deserializeFilters, - DataTableFilters, + DataTableFiltersValues, + DataTableFiltersItems, } from "../../../data/data_table_filters"; import { formatShortDateTime } from "../../../common/datetime/format_date_time"; @@ -157,13 +156,13 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { @storage({ storage: "sessionStorage", - key: "entities-table-filters-full", + key: "entities-table-filters", state: true, subscribe: false, - serializer: serializeFilters, - deserializer: deserializeFilters, }) - private _filters: DataTableFilters = {}; + private _filters: DataTableFiltersValues = {}; + + @state() private _filteredItems: DataTableFiltersItems = {}; @state() private _selected: string[] = []; @@ -460,13 +459,14 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { devices: HomeAssistant["devices"], areas: HomeAssistant["areas"], stateEntities: StateEntity[], - filters: DataTableFilters, + filters: DataTableFiltersValues, + filteredItems: DataTableFiltersItems, entries?: ConfigEntry[], labelReg?: LabelRegistryEntry[] ) => { const result: EntityRow[] = []; - const stateFilters = filters["ha-filter-states"]?.value as string[]; + const stateFilters = filters["ha-filter-states"] as string[]; const showEnabled = !stateFilters?.length || stateFilters.includes("enabled"); @@ -491,15 +491,11 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { const filteredDomains = new Set(); Object.entries(filters).forEach(([key, filter]) => { - if ( - key === "config_entry" && - Array.isArray(filter.value) && - filter.value.length - ) { + if (key === "config_entry" && Array.isArray(filter) && filter.length) { filteredEntities = filteredEntities.filter( (entity) => entity.config_entry_id && - (filter.value as string[]).includes(entity.config_entry_id) + (filter as string[]).includes(entity.config_entry_id) ); if (!entries) { @@ -509,8 +505,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { const configEntries = entries.filter( (entry) => - entry.entry_id && - (filter.value as string[]).includes(entry.entry_id) + entry.entry_id && (filter as string[]).includes(entry.entry_id) ); configEntries.forEach((configEntry) => { @@ -521,17 +516,15 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { } } else if ( key === "ha-filter-integrations" && - Array.isArray(filter.value) && - filter.value.length + Array.isArray(filter) && + filter.length ) { if (!entries) { this._loadConfigEntries(); return; } const entryIds = entries - .filter((entry) => - (filter.value as string[]).includes(entry.domain) - ) + .filter((entry) => (filter as string[]).includes(entry.domain)) .map((entry) => entry.entry_id); const filteredEntitiesByDomain = new Set(); @@ -547,7 +540,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { entitiesByDomain[source.domain].push(entity); } - for (const val of filter.value) { + for (const val of filter) { if (val in entitiesByDomain) { entitiesByDomain[val].forEach((item) => filteredEntitiesByDomain.add(item) @@ -558,32 +551,34 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { filteredEntities = filteredEntities.filter( (entity) => filteredEntitiesByDomain.has(entity.entity_id) || - (filter.value as string[]).includes(entity.platform) || + (filter as string[]).includes(entity.platform) || (entity.config_entry_id && entryIds.includes(entity.config_entry_id)) ); - filter.value!.forEach((domain) => filteredDomains.add(domain)); + filter!.forEach((domain) => filteredDomains.add(domain)); } else if ( key === "ha-filter-domains" && - Array.isArray(filter.value) && - filter.value.length + Array.isArray(filter) && + filter.length ) { filteredEntities = filteredEntities.filter((entity) => - (filter.value as string[]).includes(computeDomain(entity.entity_id)) + (filter as string[]).includes(computeDomain(entity.entity_id)) ); } else if ( key === "ha-filter-labels" && - Array.isArray(filter.value) && - filter.value.length + Array.isArray(filter) && + filter.length ) { filteredEntities = filteredEntities.filter((entity) => - entity.labels.some((lbl) => - (filter.value as string[]).includes(lbl) - ) + entity.labels.some((lbl) => (filter as string[]).includes(lbl)) ); - } else if (filter.items) { + } + }); + + Object.values(filteredItems).forEach((items) => { + if (items) { filteredEntities = filteredEntities.filter((entity) => - filter.items!.has(entity.entity_id) + items.has(entity.entity_id) ); } }); @@ -684,6 +679,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { this.hass.areas, this._stateEntities, this._filters, + this._filteredItems, this._entries, this._labels ); @@ -749,10 +745,10 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { hasFilters .filters=${ Object.values(this._filters).filter((filter) => - Array.isArray(filter.value) - ? filter.value.length - : filter.value && - Object.values(filter.value).some((val) => + Array.isArray(filter) + ? filter.length + : filter && + Object.values(filter).some((val) => Array.isArray(val) ? val.length : val ) ).length @@ -897,21 +893,20 @@ ${ ${ - Array.isArray(this._filters.config_entry?.value) && - this._filters.config_entry?.value.length + Array.isArray(this._filters.config_entry) && + this._filters.config_entry?.length ? html` Filtering by config entry ${this._entries?.find( - (entry) => - entry.entry_id === this._filters.config_entry!.value![0] - )?.title || this._filters.config_entry.value[0]} + (entry) => entry.entry_id === this._filters.config_entry![0] + )?.title || this._filters.config_entry[0]} ` : nothing }