From 147b1f34ace8b90b86ad4cc46a39f1e1b771890b Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 28 Nov 2022 12:13:54 +0100 Subject: [PATCH] Fixes for disabled/unavailable entities (#14451) --- .../chart/state-history-chart-timeline.ts | 16 ++--- src/dialogs/more-info/ha-more-info-history.ts | 50 ++++++------- src/dialogs/more-info/ha-more-info-info.ts | 9 ++- .../more-info/ha-more-info-settings.ts | 16 +++-- .../dialog-device-registry-detail.ts | 1 + .../entities/entity-registry-settings.ts | 70 ++++++++++++++++--- src/translations/en.json | 2 + 7 files changed, 114 insertions(+), 50 deletions(-) diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index 538afd6b34..fd98de2125 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -21,9 +21,9 @@ let colorIndex = 0; export const getStateColorToken = ( stateString: string, - entityState: HassEntity + entityState?: HassEntity ) => { - if (!stateActive(entityState, stateString)) { + if (!entityState || !stateActive(entityState, stateString)) { return `disabled`; } const color = stateColor(entityState, stateString); @@ -35,8 +35,8 @@ export const getStateColorToken = ( const getColor = ( stateString: string, - entityState: HassEntity, - computedStyles: CSSStyleDeclaration + computedStyles: CSSStyleDeclaration, + entityState?: HassEntity ) => { const stateColorToken = getStateColorToken(stateString, entityState); @@ -273,8 +273,8 @@ export class StateHistoryChartTimeline extends LitElement { label: locState, color: getColor( prevState, - this.hass.states[stateInfo.entity_id], - computedStyles + computedStyles, + this.hass.states[stateInfo.entity_id] ), }); @@ -291,8 +291,8 @@ export class StateHistoryChartTimeline extends LitElement { label: locState, color: getColor( prevState, - this.hass.states[stateInfo.entity_id], - computedStyles + computedStyles, + this.hass.states[stateInfo.entity_id] ), }); } diff --git a/src/dialogs/more-info/ha-more-info-history.ts b/src/dialogs/more-info/ha-more-info-history.ts index f28edba8dc..105fd43c63 100644 --- a/src/dialogs/more-info/ha-more-info-history.ts +++ b/src/dialogs/more-info/ha-more-info-history.ts @@ -48,7 +48,7 @@ export class MoreInfoHistory extends LitElement { return html``; } - return html`${isComponentLoaded(this.hass, "history") + return html` ${isComponentLoaded(this.hass, "history") ? html`
${this.hass.localize("ui.dialogs.more_info_control.history")} @@ -151,33 +151,27 @@ export class MoreInfoHistory extends LitElement { setTimeout(() => fireEvent(this, "close-dialog"), 500); } - static get styles() { - return [ - css` - .header { - display: flex; - flex-direction: row; - justify-content: space-between; - align-items: center; - margin-bottom: 8px; - } - .header > a, - a:visited { - color: var(--primary-color); - } - .title { - font-family: var(--paper-font-title_-_font-family); - -webkit-font-smoothing: var( - --paper-font-title_-_-webkit-font-smoothing - ); - font-size: var(--paper-font-subhead_-_font-size); - font-weight: var(--paper-font-title_-_font-weight); - letter-spacing: var(--paper-font-title_-_letter-spacing); - line-height: var(--paper-font-title_-_line-height); - } - `, - ]; - } + static styles = css` + .header { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + margin-bottom: 8px; + } + .header > a, + a:visited { + color: var(--primary-color); + } + .title { + font-family: var(--paper-font-title_-_font-family); + -webkit-font-smoothing: var(--paper-font-title_-_-webkit-font-smoothing); + font-size: var(--paper-font-subhead_-_font-size); + font-weight: var(--paper-font-title_-_font-weight); + letter-spacing: var(--paper-font-title_-_letter-spacing); + line-height: var(--paper-font-title_-_line-height); + } + `; } declare global { diff --git a/src/dialogs/more-info/ha-more-info-info.ts b/src/dialogs/more-info/ha-more-info-info.ts index df3fe1f901..005cf6278b 100644 --- a/src/dialogs/more-info/ha-more-info-info.ts +++ b/src/dialogs/more-info/ha-more-info-info.ts @@ -30,7 +30,14 @@ export class MoreInfoInfo extends LitElement { const domain = computeDomain(entityId); return html` - ${stateObj.attributes.restored && this._entityEntry + ${!stateObj + ? html` + ${this.hass.localize( + "ui.dialogs.entity_registry.editor.unavailable" + )} + ` + : ""} + ${stateObj?.attributes.restored && this._entityEntry ? html` ${this.hass.localize( "ui.dialogs.more_info_control.restored.no_longer_provided", diff --git a/src/dialogs/more-info/ha-more-info-settings.ts b/src/dialogs/more-info/ha-more-info-settings.ts index 7a436aa86f..1f558305e1 100644 --- a/src/dialogs/more-info/ha-more-info-settings.ts +++ b/src/dialogs/more-info/ha-more-info-settings.ts @@ -54,11 +54,13 @@ export class HaMoreInfoSettings extends LitElement { } return html` - ${dynamicElement(this._settingsElementTag, { - hass: this.hass, - entry: this._entry, - entityId: this.entityId, - })} +
+ ${dynamicElement(this._settingsElementTag, { + hass: this.hass, + entry: this._entry, + entityId: this.entityId, + })} +
`; } @@ -84,6 +86,10 @@ export class HaMoreInfoSettings extends LitElement { } } + private _entryUpdated(ev: CustomEvent) { + this._entry = ev.detail; + } + private async _loadPlatformSettingTabs(): Promise { if (!this._entry) { return; diff --git a/src/panels/config/devices/device-registry-detail/dialog-device-registry-detail.ts b/src/panels/config/devices/device-registry-detail/dialog-device-registry-detail.ts index f67de2be45..c1beeb9ae3 100644 --- a/src/panels/config/devices/device-registry-detail/dialog-device-registry-detail.ts +++ b/src/panels/config/devices/device-registry-detail/dialog-device-registry-detail.ts @@ -80,6 +80,7 @@ class DialogDeviceRegistryDetail extends LitElement {
diff --git a/src/panels/config/entities/entity-registry-settings.ts b/src/panels/config/entities/entity-registry-settings.ts index 3790f9ec91..22c0bdbd53 100644 --- a/src/panels/config/entities/entity-registry-settings.ts +++ b/src/panels/config/entities/entity-registry-settings.ts @@ -299,20 +299,35 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { return html` ${!stateObj ? html` -
- ${this.hass!.localize( - "ui.dialogs.entity_registry.editor.unavailable" - )} + ${this._device?.disabled_by - ? html`
${this.hass!.localize( + ? html`${this.hass!.localize( "ui.dialogs.entity_registry.editor.device_disabled" - )}
+ )} ${this.hass!.localize( "ui.dialogs.entity_registry.editor.open_device_settings" )} ` - : ""} -
+ : this.entry.disabled_by + ? html`${this.hass!.localize( + "ui.dialogs.entity_registry.editor.entity_disabled" + )}${["user", "integration"].includes(this._disabledBy!) + ? html` + ${this.hass!.localize( + "ui.dialogs.entity_registry.editor.enable_entity" + )}` + : ""}` + : this.hass!.localize( + "ui.dialogs.entity_registry.editor.unavailable" + )} + ` : ""} ${this._error @@ -922,6 +937,39 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { }); } + private async _enableEntry() { + this._error = undefined; + this._submitting = true; + try { + const result = await updateEntityRegistryEntry( + this.hass!, + this._origEntityId, + { disabled_by: null } + ); + fireEvent(this, "entity-entry-updated", result.entity_entry); + if (result.require_restart) { + showAlertDialog(this, { + text: this.hass.localize( + "ui.dialogs.entity_registry.editor.enabled_restart_confirm" + ), + }); + } + if (result.reload_delay) { + showAlertDialog(this, { + text: this.hass.localize( + "ui.dialogs.entity_registry.editor.enabled_delay_confirm", + "delay", + result.reload_delay + ), + }); + } + } catch (err: any) { + this._error = err.message; + } finally { + this._submitting = false; + } + } + private async _updateEntry(): Promise { this._submitting = true; @@ -1163,6 +1211,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) { li[divider] { border-bottom-color: var(--divider-color); } + ha-alert mwc-button { + width: max-content; + } `, ]; } @@ -1172,4 +1223,7 @@ declare global { interface HTMLElementTagNameMap { "entity-registry-settings": EntityRegistrySettings; } + interface HASSDomEvents { + "entity-entry-updated": ExtEntityRegistryEntry; + } } diff --git a/src/translations/en.json b/src/translations/en.json index 091b488c16..696640d129 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -893,6 +893,8 @@ "hidden_label": "Hidden", "hidden_cause": "Hidden by {cause}.", "device_disabled": "The device of this entity is disabled.", + "entity_disabled": "This entity is disabled.", + "enable_entity": "Enable", "open_device_settings": "Open device settings", "switch_as_x_confirm": "This switch will be hidden and a new {domain} will be added. Your existing configurations using the switch will continue to work.", "enabled_description": "Disabled entities will not be added to Home Assistant.",