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

Add labels on entities datatable #20274

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
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
74 changes: 61 additions & 13 deletions src/panels/config/entities/ha-config-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
mdiRestoreAlert,
mdiUndo,
} from "@mdi/js";
import { HassEntity } from "home-assistant-js-websocket";
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
import {
CSSResultGroup,
LitElement,
Expand All @@ -37,6 +37,8 @@ import type {
RowClickedEvent,
SelectionChangedEvent,
} from "../../../components/data-table/ha-data-table";
import "../../../components/data-table/ha-data-table-labels";
import "../../../components/ha-alert";
import "../../../components/ha-button-menu";
import "../../../components/ha-check-list-item";
import "../../../components/ha-filter-devices";
Expand All @@ -47,7 +49,6 @@ import "../../../components/ha-filter-labels";
import "../../../components/ha-icon";
import "../../../components/ha-icon-button";
import "../../../components/ha-svg-icon";
import "../../../components/ha-alert";
import { ConfigEntry, getConfigEntries } from "../../../data/config_entries";
import { fullEntitiesContext } from "../../../data/context";
import { UNAVAILABLE } from "../../../data/entity";
Expand All @@ -58,6 +59,10 @@ import {
updateEntityRegistryEntry,
} from "../../../data/entity_registry";
import { entryIcon } from "../../../data/icons";
import {
LabelRegistryEntry,
subscribeLabelRegistry,
} from "../../../data/label_registry";
import {
showAlertDialog,
showConfirmationDialog,
Expand All @@ -66,6 +71,7 @@ import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info
import "../../../layouts/hass-loading-screen";
import "../../../layouts/hass-tabs-subpage-data-table";
import type { HaTabsSubpageDataTable } from "../../../layouts/hass-tabs-subpage-data-table";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant, Route } from "../../../types";
import { configSections } from "../ha-panel-config";
Expand All @@ -87,10 +93,11 @@ export interface EntityRow extends StateEntity {
status: string | undefined;
area?: string;
localized_platform: string;
label_entries: LabelRegistryEntry[];
}

@customElement("ha-config-entities")
export class HaConfigEntities extends LitElement {
export class HaConfigEntities extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ type: Boolean }) public isWide = false;
Expand Down Expand Up @@ -120,6 +127,9 @@ export class HaConfigEntities extends LitElement {

@state() private _expandedFilter?: string;

@state()
_labels!: LabelRegistryEntry[];

@query("hass-tabs-subpage-data-table", true)
private _dataTable!: HaTabsSubpageDataTable;

Expand Down Expand Up @@ -203,14 +213,21 @@ export class HaConfigEntities extends LitElement {
filterable: true,
direction: "asc",
grows: true,
template: narrow
? (entry) => html`
${entry.name}<br />
<div class="secondary">
template: (entry) => html`
<div style="font-size: 14px;">${entry.name}</div>
${narrow
? html`<div class="secondary">
${entry.entity_id} | ${entry.localized_platform}
</div>
`
: undefined,
</div>`
: nothing}
${entry.label_entries.length
? html`
<ha-data-table-labels
.labels=${entry.label_entries}
></ha-data-table-labels>
`
: nothing}
`,
},
entity_id: {
title: localize("ui.panel.config.entities.picker.headers.entity_id"),
Expand Down Expand Up @@ -302,6 +319,13 @@ export class HaConfigEntities extends LitElement {
`
: "—",
},
labels: {
title: "",
hidden: true,
filterable: true,
template: (entry) =>
entry.label_entries.map((lbl) => lbl.name).join(" "),
},
})
);

Expand All @@ -316,7 +340,8 @@ export class HaConfigEntities extends LitElement {
string,
{ value: string[] | undefined; items: Set<string> | undefined }
>,
entries?: ConfigEntry[]
entries?: ConfigEntry[],
labelReg?: LabelRegistryEntry[]
) => {
const result: EntityRow[] = [];

Expand Down Expand Up @@ -409,6 +434,11 @@ export class HaConfigEntities extends LitElement {
continue;
}

const labels = labelReg && entry?.labels;
const labelsEntries = (labels || []).map(
(lbl) => labelReg!.find((label) => label.label_id === lbl)!
);

result.push({
...entry,
entity,
Expand Down Expand Up @@ -436,13 +466,22 @@ export class HaConfigEntities extends LitElement {
: localize(
"ui.panel.config.entities.picker.status.available"
),
label_entries: labelsEntries,
});
}

return { filteredEntities: result, filteredConfigEntry, filteredDomains };
}
);

protected hassSubscribe(): (UnsubscribeFunc | Promise<UnsubscribeFunc>)[] {
return [
subscribeLabelRegistry(this.hass.connection, (labels) => {
this._labels = labels;
}),
];
}

protected render() {
if (!this.hass || this._entities === undefined) {
return html` <hass-loading-screen></hass-loading-screen> `;
Expand All @@ -456,7 +495,8 @@ export class HaConfigEntities extends LitElement {
this.hass.areas,
this._stateEntities,
this._filters,
this._entries
this._entries,
this._labels
);

const includeAddDeviceFab =
Expand Down Expand Up @@ -497,6 +537,7 @@ export class HaConfigEntities extends LitElement {
@row-click=${this._openEditEntry}
id="entity_id"
.hasFab=${includeAddDeviceFab}
class=${this.narrow ? "narrow" : ""}
>
<ha-integration-overflow-menu
.hass=${this.hass}
Expand Down Expand Up @@ -932,7 +973,8 @@ export class HaConfigEntities extends LitElement {
this.hass.areas,
this._stateEntities,
this._filters,
this._entries
this._entries,
this._labels
);
if (
filteredDomains.size === 1 &&
Expand All @@ -954,6 +996,12 @@ export class HaConfigEntities extends LitElement {
return [
haStyle,
css`
hass-tabs-subpage-data-table {
--data-table-row-height: 60px;
}
hass-tabs-subpage-data-table.narrow {
--data-table-row-height: 72px;
}
hass-loading-screen {
--app-header-background-color: var(--sidebar-background-color);
--app-header-text-color: var(--sidebar-text-color);
Expand Down
Loading