From e01cb3ca829b5d19cb65e382844da47b6f1b1785 Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 14 Mar 2022 14:22:45 -0500 Subject: [PATCH] Utilize Hide Hidden Entities --- src/data/entity_registry.ts | 2 + .../device-detail/ha-device-entities-card.ts | 124 +++++++++--------- .../config/devices/ha-config-device-page.ts | 2 +- .../entities/entity-registry-settings.ts | 111 +++++++++++----- .../config/entities/ha-config-entities.ts | 27 ++++ .../common/generate-lovelace-config.ts | 5 +- src/translations/en.json | 14 +- 7 files changed, 186 insertions(+), 99 deletions(-) diff --git a/src/data/entity_registry.ts b/src/data/entity_registry.ts index 83fcc601b5..eaefcee6e3 100644 --- a/src/data/entity_registry.ts +++ b/src/data/entity_registry.ts @@ -14,6 +14,7 @@ export interface EntityRegistryEntry { device_id: string | null; area_id: string | null; disabled_by: string | null; + hidden_by: string | null; entity_category: "config" | "diagnostic" | null; } @@ -38,6 +39,7 @@ export interface EntityRegistryEntryUpdateParams { device_class?: string | null; area_id?: string | null; disabled_by?: string | null; + hidden_by: string | null; new_entity_id?: string; } diff --git a/src/panels/config/devices/device-detail/ha-device-entities-card.ts b/src/panels/config/devices/device-detail/ha-device-entities-card.ts index ebfbd159dd..4c352d3c21 100644 --- a/src/panels/config/devices/device-detail/ha-device-entities-card.ts +++ b/src/panels/config/devices/device-detail/ha-device-entities-card.ts @@ -40,7 +40,7 @@ export class HaDeviceEntitiesCard extends LitElement { @property() public entities!: EntityRegistryStateEntry[]; - @property() public showDisabled = false; + @property() public showHidden = false; @state() private _extDisabledEntityEntries?: Record< string, @@ -60,77 +60,75 @@ export class HaDeviceEntitiesCard extends LitElement { } protected render(): TemplateResult { - const disabledEntities: EntityRegistryStateEntry[] = []; + if (!this.entities.length) { + return html` +
+ ${this.hass.localize("ui.panel.config.devices.entities.none")} +
+ `; + } + + const shownEntities: EntityRegistryStateEntry[] = []; + const hiddenEntities: EntityRegistryStateEntry[] = []; this._entityRows = []; + + this.entities.forEach((entry) => { + if (entry.disabled_by || entry.hidden_by) { + if (this._extDisabledEntityEntries) { + hiddenEntities.push( + this._extDisabledEntityEntries[entry.entity_id] || entry + ); + } else { + hiddenEntities.push(entry); + } + } else { + shownEntities.push(entry); + } + }); + return html` - ${this.entities.length - ? html` -
- ${this.entities.map((entry: EntityRegistryStateEntry) => { - if (entry.disabled_by) { - if (this._extDisabledEntityEntries) { - disabledEntities.push( - this._extDisabledEntityEntries[entry.entity_id] || entry - ); - } else { - disabledEntities.push(entry); - } - return ""; - } - return this.hass.states[entry.entity_id] - ? this._renderEntity(entry) - : this._renderEntry(entry); - })} -
- ${disabledEntities.length - ? !this.showDisabled - ? html` - - ` - : html` - ${disabledEntities.map((entry) => - this._renderEntry(entry) - )} - - ` - : ""} -
- +
+ ${shownEntities.map((entry) => + this.hass.states[entry.entity_id] + ? this._renderEntity(entry) + : this._renderEntry(entry) + )} +
+ ${hiddenEntities.length + ? !this.showHidden + ? html` +
- ` - : html` -
- ${this.hass.localize("ui.panel.config.devices.entities.none")} -
- `} + + ` + : html` + ${hiddenEntities.map((entry) => this._renderEntry(entry))} + + ` + : ""} +
+ + ${this.hass.localize( + "ui.panel.config.devices.entities.add_entities_lovelace" + )} + +
`; } - private _toggleShowDisabled() { - this.showDisabled = !this.showDisabled; - if (!this.showDisabled || this._extDisabledEntityEntries !== undefined) { + private _toggleShowHidden() { + this.showHidden = !this.showHidden; + if (!this.showHidden || this._extDisabledEntityEntries !== undefined) { return; } this._extDisabledEntityEntries = {}; diff --git a/src/panels/config/devices/ha-config-device-page.ts b/src/panels/config/devices/ha-config-device-page.ts index 7512b3067f..54d926b978 100644 --- a/src/panels/config/devices/ha-config-device-page.ts +++ b/src/panels/config/devices/ha-config-device-page.ts @@ -557,7 +557,7 @@ export class HaConfigDevicePage extends LitElement { )} .deviceName=${deviceName} .entities=${entitiesByCategory[category]} - .showDisabled=${device.disabled_by !== null} + .showHidden=${device.disabled_by !== null} > ` diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index 542a730d32..d66fb83e23 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -76,6 +76,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { @state() private _disabledBy!: string | null; + @state() private _hiddenBy!: string | null; + private _deviceLookup?: Record; @state() private _device?: DeviceRegistryEntry; @@ -112,6 +114,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { this._areaId = this.entry.area_id; this._entityId = this.entry.entity_id; this._disabledBy = this.entry.disabled_by; + this._hiddenBy = this.entry.hidden_by; this._device = this.entry.device_id && this._deviceLookup ? this._deviceLookup[this.entry.device_id] @@ -244,41 +247,75 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { - ${this.entry.device_id - ? html` + + +
+
+ ${this.hass.localize( + "ui.dialogs.entity_registry.editor.hidden_label" )} - outlined - > -

- ${this.hass.localize( - "ui.dialogs.entity_registry.editor.area_note" - )} -

- ${this._areaId - ? html`${this.hass.localize( - "ui.dialogs.entity_registry.editor.follow_device_area" - )}` - : this._device - ? html`${this.hass.localize( - "ui.dialogs.entity_registry.editor.change_device_area" - )}` +
+
+ ${this._hiddenBy && this._hiddenBy !== "user" + ? this.hass.localize( + "ui.dialogs.entity_registry.editor.hidden_cause", + "cause", + this.hass.localize( + `config_entry.hidden_by.${this._hiddenBy}` + ) + ) : ""} - ${this.hass.localize( + "ui.dialogs.entity_registry.editor.note" + )} +
+
+ + ${this.entry.device_id + ? html` + ` + outlined + > +

+ ${this.hass.localize( + "ui.dialogs.entity_registry.editor.area_note" + )} +

+ ${this._areaId + ? html`${this.hass.localize( + "ui.dialogs.entity_registry.editor.follow_device_area" + )}` + : this._device + ? html`${this.hass.localize( + "ui.dialogs.entity_registry.editor.change_device_area" + )}` + : ""} + +
+ ` : ""}
@@ -354,6 +391,12 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { ) { params.disabled_by = this._disabledBy; } + if ( + this.entry.hidden_by !== this._hiddenBy && + (this._hiddenBy === null || this._hiddenBy === "user") + ) { + params.hidden_by = this._hiddenBy; + } try { const result = await updateEntityRegistryEntry( this.hass!, @@ -409,6 +452,10 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { this._disabledBy = (ev.target as HaSwitch).checked ? null : "user"; } + private _hiddenByChanged(ev: Event): void { + this._hiddenBy = (ev.target as HaSwitch).checked ? "user" : null; + } + static get styles(): CSSResultGroup { return [ haStyle, diff --git a/src/panels/config/entities/ha-config-entities.ts b/src/panels/config/entities/ha-config-entities.ts index f93930d2d3..87c9fb2336 100644 --- a/src/panels/config/entities/ha-config-entities.ts +++ b/src/panels/config/entities/ha-config-entities.ts @@ -101,6 +101,8 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { @state() private _showDisabled = false; + @state() private _showHidden = false; + @state() private _showUnavailable = true; @state() private _showReadOnly = true; @@ -301,6 +303,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { showDisabled: boolean, showUnavailable: boolean, showReadOnly: boolean, + showHidden: boolean, entries?: ConfigEntry[] ) => { const result: EntityRow[] = []; @@ -362,6 +365,12 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { ); } + if (!showHidden) { + filteredEntities = filteredEntities.filter( + (entity) => !entity.hidden_by + ); + } + for (const entry of filteredEntities) { const entity = this.hass.states[entry.entity_id]; const unavailable = entity?.state === UNAVAILABLE; @@ -465,6 +474,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { this._showDisabled, this._showUnavailable, this._showReadOnly, + this._showHidden, this._entries ); @@ -603,6 +613,15 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { "ui.panel.config.entities.picker.filter.show_disabled" )} + + ${this.hass!.localize( + "ui.panel.config.entities.picker.filter.show_hidden" + )} + ) { + if (ev.detail.source !== "property") { + return; + } + this._showHidden = ev.detail.selected; + } + private _showRestoredChanged(ev: CustomEvent) { if (ev.detail.source !== "property") { return; diff --git a/src/panels/lovelace/common/generate-lovelace-config.ts b/src/panels/lovelace/common/generate-lovelace-config.ts index 0d3fd0cb9a..8ffdd43709 100644 --- a/src/panels/lovelace/common/generate-lovelace-config.ts +++ b/src/panels/lovelace/common/generate-lovelace-config.ts @@ -238,7 +238,10 @@ const computeDefaultViewStates = ( const hiddenEntities = new Set( entityEntries .filter( - (entry) => entry.entity_category || HIDE_PLATFORM.has(entry.platform) + (entry) => + entry.entity_category || + HIDE_PLATFORM.has(entry.platform) || + entry.hidden_by ) .map((entry) => entry.entity_id) ); diff --git a/src/translations/en.json b/src/translations/en.json index 4eaa79d614..7d5e1440ac 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -102,6 +102,12 @@ "integration": "Integration", "config_entry": "Config entry", "device": "Device" + }, + "hidden_by": { + "user": "User", + "integration": "Integration", + "config_entry": "Config entry", + "device": "Device" } }, "ui": { @@ -785,11 +791,14 @@ "unavailable": "This entity is unavailable.", "enabled_label": "Enable entity", "enabled_cause": "Disabled by {cause}.", + "hidden_label": "Hide entity", + "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", "open_device_settings": "Open device settings", "enabled_description": "Disabled entities will not be added to Home Assistant.", "enabled_delay_confirm": "The enabled entities will be added to Home Assistant in {delay} seconds", "enabled_restart_confirm": "Restart Home Assistant to finish enabling the entities", + "hidden_description": "Hidden entities will not be shown in Home Assistant UI.", "delete": "Delete", "confirm_delete": "Are you sure you want to delete this entity?", "update": "Update", @@ -2364,8 +2373,8 @@ "config": "Configuration", "add_entities_lovelace": "Add to Lovelace", "none": "This device has no entities", - "hide_disabled": "Hide disabled", - "disabled_entities": "+{count} {count, plural,\n one {disabled entity}\n other {disabled entities}\n}" + "hide_disabled": "Hide hidden", + "disabled_entities": "+{count} {count, plural,\n one {hidden entity}\n other {hidden entities}\n}" }, "confirm_rename_entity_ids": "Do you also want to rename the entity IDs of your entities?", "confirm_rename_entity_ids_warning": "This will not change any configuration (like automations, scripts, scenes, dashboards) that is currently using these entities! You will have to update them yourself to use the new entity IDs!", @@ -2405,6 +2414,7 @@ "search": "Search entities", "filter": { "filter": "Filter", + "show_hidden": "Show hidden entities", "show_disabled": "Show disabled entities", "show_unavailable": "Show unavailable entities", "show_readonly": "Show read-only entities",