From 3addfc3548c68452f019c9e873142fad41fc4c5c Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Thu, 16 Nov 2023 09:13:31 -0800 Subject: [PATCH] Localization bugfixes in config devices/entities/helpers (#18645) --- .../devices/ha-config-devices-dashboard.ts | 264 +++++++++--------- .../config/entities/ha-config-entities.ts | 65 ++--- .../config/helpers/ha-config-helpers.ts | 21 +- 3 files changed, 170 insertions(+), 180 deletions(-) diff --git a/src/panels/config/devices/ha-config-devices-dashboard.ts b/src/panels/config/devices/ha-config-devices-dashboard.ts index 94c643282aa1..8aaeed95be7a 100644 --- a/src/panels/config/devices/ha-config-devices-dashboard.ts +++ b/src/panels/config/devices/ha-config-devices-dashboard.ts @@ -285,145 +285,143 @@ export class HaConfigDeviceDashboard extends LitElement { } ); - private _columns = memoizeOne((narrow: boolean, showDisabled: boolean) => { - type DeviceItem = ReturnType< - typeof this._devicesAndFilterDomains - >["devicesOutput"][number]; - - const columns: DataTableColumnContainer = { - icon: { - title: "", - type: "icon", - template: (device) => - device.domains.length - ? html`` - : "", - }, - }; - - if (narrow) { - columns.name = { - title: this.hass.localize("ui.panel.config.devices.data_table.device"), - main: true, + private _columns = memoizeOne( + (localize: LocalizeFunc, narrow: boolean, showDisabled: boolean) => { + type DeviceItem = ReturnType< + typeof this._devicesAndFilterDomains + >["devicesOutput"][number]; + + const columns: DataTableColumnContainer = { + icon: { + title: "", + type: "icon", + template: (device) => + device.domains.length + ? html`` + : "", + }, + }; + + if (narrow) { + columns.name = { + title: localize("ui.panel.config.devices.data_table.device"), + main: true, + sortable: true, + filterable: true, + direction: "asc", + grows: true, + template: (device) => html` + ${device.name} +
${device.area} | ${device.integration}
+ `, + }; + } else { + columns.name = { + title: localize("ui.panel.config.devices.data_table.device"), + main: true, + sortable: true, + filterable: true, + grows: true, + direction: "asc", + }; + } + + columns.manufacturer = { + title: localize("ui.panel.config.devices.data_table.manufacturer"), sortable: true, + hidden: narrow, filterable: true, - direction: "asc", - grows: true, - template: (device) => html` - ${device.name} -
${device.area} | ${device.integration}
- `, + width: "15%", }; - } else { - columns.name = { - title: this.hass.localize("ui.panel.config.devices.data_table.device"), - main: true, + columns.model = { + title: localize("ui.panel.config.devices.data_table.model"), sortable: true, + hidden: narrow, filterable: true, - grows: true, - direction: "asc", + width: "15%", }; - } - - columns.manufacturer = { - title: this.hass.localize( - "ui.panel.config.devices.data_table.manufacturer" - ), - sortable: true, - hidden: narrow, - filterable: true, - width: "15%", - }; - columns.model = { - title: this.hass.localize("ui.panel.config.devices.data_table.model"), - sortable: true, - hidden: narrow, - filterable: true, - width: "15%", - }; - columns.area = { - title: this.hass.localize("ui.panel.config.devices.data_table.area"), - sortable: true, - hidden: narrow, - filterable: true, - width: "15%", - }; - columns.integration = { - title: this.hass.localize( - "ui.panel.config.devices.data_table.integration" - ), - sortable: true, - hidden: narrow, - filterable: true, - width: "15%", - }; - columns.battery_entity = { - title: this.hass.localize("ui.panel.config.devices.data_table.battery"), - sortable: true, - filterable: true, - type: "numeric", - width: narrow ? "95px" : "15%", - maxWidth: "95px", - valueColumn: "battery_level", - template: (device) => { - const batteryEntityPair = device.battery_entity; - const battery = - batteryEntityPair && batteryEntityPair[0] - ? this.hass.states[batteryEntityPair[0]] - : undefined; - const batteryDomain = battery ? computeStateDomain(battery) : undefined; - const batteryCharging = - batteryEntityPair && batteryEntityPair[1] - ? this.hass.states[batteryEntityPair[1]] + columns.area = { + title: localize("ui.panel.config.devices.data_table.area"), + sortable: true, + hidden: narrow, + filterable: true, + width: "15%", + }; + columns.integration = { + title: localize("ui.panel.config.devices.data_table.integration"), + sortable: true, + hidden: narrow, + filterable: true, + width: "15%", + }; + columns.battery_entity = { + title: localize("ui.panel.config.devices.data_table.battery"), + sortable: true, + filterable: true, + type: "numeric", + width: narrow ? "95px" : "15%", + maxWidth: "95px", + valueColumn: "battery_level", + template: (device) => { + const batteryEntityPair = device.battery_entity; + const battery = + batteryEntityPair && batteryEntityPair[0] + ? this.hass.states[batteryEntityPair[0]] + : undefined; + const batteryDomain = battery + ? computeStateDomain(battery) : undefined; - - return battery && - (batteryDomain === "binary_sensor" || !isNaN(battery.state as any)) - ? html` - ${batteryDomain === "sensor" - ? this.hass.formatEntityState(battery) - : nothing} - - ` - : html`—`; - }, - }; - if (showDisabled) { - columns.disabled_by = { - title: "", - label: this.hass.localize( - "ui.panel.config.devices.data_table.disabled_by" - ), - type: "icon", - template: (device) => - device.disabled_by - ? html`
- - - ${this.hass.localize("ui.panel.config.devices.disabled")} - -
` - : "—", + const batteryCharging = + batteryEntityPair && batteryEntityPair[1] + ? this.hass.states[batteryEntityPair[1]] + : undefined; + + return battery && + (batteryDomain === "binary_sensor" || !isNaN(battery.state as any)) + ? html` + ${batteryDomain === "sensor" + ? this.hass.formatEntityState(battery) + : nothing} + + ` + : html`—`; + }, }; + if (showDisabled) { + columns.disabled_by = { + title: "", + label: localize("ui.panel.config.devices.data_table.disabled_by"), + type: "icon", + template: (device) => + device.disabled_by + ? html`
+ + + ${this.hass.localize("ui.panel.config.devices.disabled")} + +
` + : "—", + }; + } + return columns; } - return columns; - }); + ); public willUpdate(changedProps) { if (changedProps.has("_searchParms")) { @@ -474,7 +472,11 @@ export class HaConfigDeviceDashboard extends LitElement { "number", this._numHiddenDevices )} - .columns=${this._columns(this.narrow, this._showDisabled)} + .columns=${this._columns( + this.hass.localize, + this.narrow, + this._showDisabled + )} .data=${devicesOutput} .filter=${this._filter} @clear-filter=${this._clearFilter} diff --git a/src/panels/config/entities/ha-config-entities.ts b/src/panels/config/entities/ha-config-entities.ts index 5145f3324d55..10e403b7ecc3 100644 --- a/src/panels/config/entities/ha-config-entities.ts +++ b/src/panels/config/entities/ha-config-entities.ts @@ -84,6 +84,7 @@ export interface EntityRow extends StateEntity { restored: boolean; status: string; area?: string; + localized_platform: string; } @customElement("ha-config-entities") @@ -176,12 +177,15 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { ); private _columns = memoize( - (narrow, _language, showDisabled): DataTableColumnContainer => ({ + ( + localize: LocalizeFunc, + narrow, + _language, + showDisabled + ): DataTableColumnContainer => ({ icon: { title: "", - label: this.hass.localize( - "ui.panel.config.entities.picker.headers.state_icon" - ), + label: localize("ui.panel.config.entities.picker.headers.state_icon"), type: "icon", template: (entry) => html` html` ${entry.name}
- ${entry.entity_id} | - ${this.hass.localize(`component.${entry.platform}.title`) || - entry.platform} + ${entry.entity_id} | ${entry.localized_platform}
` : undefined, }, entity_id: { - title: this.hass.localize( - "ui.panel.config.entities.picker.headers.entity_id" - ), + title: localize("ui.panel.config.entities.picker.headers.entity_id"), hidden: narrow, sortable: true, filterable: true, width: "25%", }, - platform: { - title: this.hass.localize( - "ui.panel.config.entities.picker.headers.integration" - ), + localized_platform: { + title: localize("ui.panel.config.entities.picker.headers.integration"), hidden: narrow, sortable: true, filterable: true, width: "20%", - template: (entry) => - this.hass.localize(`component.${entry.platform}.title`) || - entry.platform, }, area: { - title: this.hass.localize( - "ui.panel.config.entities.picker.headers.area" - ), + title: localize("ui.panel.config.entities.picker.headers.area"), sortable: true, hidden: narrow, filterable: true, width: "15%", }, disabled_by: { - title: this.hass.localize( - "ui.panel.config.entities.picker.headers.disabled_by" - ), + title: localize("ui.panel.config.entities.picker.headers.disabled_by"), sortable: true, hidden: narrow || !showDisabled, filterable: true, @@ -257,9 +246,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { ), }, status: { - title: this.hass.localize( - "ui.panel.config.entities.picker.headers.status" - ), + title: localize("ui.panel.config.entities.picker.headers.status"), type: "icon", sortable: true, filterable: true, @@ -318,6 +305,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { private _filteredEntitiesAndDomains = memoize( ( + localize: LocalizeFunc, entities: StateEntity[], devices: DeviceRegistryEntry[] | undefined, areas: AreaRegistryEntry[] | undefined, @@ -432,20 +420,16 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { ), unavailable, restored, + localized_platform: + localize(`component.${entry.platform}.title`) || entry.platform, area: area ? area.name : "—", status: restored - ? this.hass.localize( - "ui.panel.config.entities.picker.status.restored" - ) + ? localize("ui.panel.config.entities.picker.status.restored") : unavailable - ? this.hass.localize( - "ui.panel.config.entities.picker.status.unavailable" - ) + ? localize("ui.panel.config.entities.picker.status.unavailable") : entry.disabled_by - ? this.hass.localize( - "ui.panel.config.entities.picker.status.disabled" - ) - : this.hass.localize("ui.panel.config.entities.picker.status.ok"), + ? localize("ui.panel.config.entities.picker.status.disabled") + : localize("ui.panel.config.entities.picker.status.ok"), }); } @@ -498,6 +482,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { const { filteredEntities, filteredDomains } = this._filteredEntitiesAndDomains( + this.hass.localize, this._entities, this._devices, this._areas, @@ -526,6 +511,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { .route=${this.route} .tabs=${configSections.devices} .columns=${this._columns( + this.hass.localize, this.narrow, this.hass.language, this._showDisabled @@ -969,6 +955,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { private _addDevice() { const { filteredConfigEntry, filteredDomains } = this._filteredEntitiesAndDomains( + this.hass.localize, this._entities!, this._devices, this._areas, diff --git a/src/panels/config/helpers/ha-config-helpers.ts b/src/panels/config/helpers/ha-config-helpers.ts index 8507edad4208..9a72810aa1b9 100644 --- a/src/panels/config/helpers/ha-config-helpers.ts +++ b/src/panels/config/helpers/ha-config-helpers.ts @@ -158,19 +158,11 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { width: "25%", }; } - columns.type = { + columns.localized_type = { title: localize("ui.panel.config.helpers.picker.headers.type"), sortable: true, width: "25%", filterable: true, - template: (helper) => - helper.configEntry - ? domainToName(localize, helper.type) - : html` - ${localize( - `ui.panel.config.helpers.types.${helper.type}` as LocalizeKeys - ) || helper.type} - `, }; columns.editable = { title: "", @@ -202,6 +194,7 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { private _getItems = memoizeOne( ( + localize: LocalizeFunc, stateItems: HassEntity[], entityEntries: Record, configEntries: Record @@ -248,7 +241,14 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { entity: undefined, })); - return [...states, ...entries]; + return [...states, ...entries].map((item) => ({ + ...item, + localized_type: item.configEntry + ? domainToName(localize, item.type) + : localize( + `ui.panel.config.helpers.types.${item.type}` as LocalizeKeys + ) || item.type, + })); } ); @@ -271,6 +271,7 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) { .tabs=${configSections.devices} .columns=${this._columns(this.narrow, this.hass.localize)} .data=${this._getItems( + this.hass.localize, this._stateItems, this._entityEntries, this._configEntries