From dfc461ce0534ef668b2418bfefa20b59f45127ba Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 1 Dec 2022 12:53:44 +0100 Subject: [PATCH] Add support for entity translation key (#14482) --- gallery/src/pages/misc/entity-state.ts | 3 +- src/common/entity/compute_state_display.ts | 11 ++++ .../entity/ha-entity-state-picker.ts | 1 + src/components/entity/ha-state-label-badge.ts | 3 +- src/components/ha-water_heater-state.js | 7 ++- src/data/entity_registry.ts | 1 + src/data/history.ts | 3 + src/data/logbook.ts | 8 ++- src/data/timer.ts | 10 +++- src/data/translation.ts | 1 + .../configurator-notification-item.ts | 3 +- src/fake_data/provide_hass.ts | 4 +- src/layouts/home-assistant.ts | 2 + src/panels/lovelace/cards/hui-button-card.ts | 3 +- src/panels/lovelace/cards/hui-entity-card.ts | 3 +- src/panels/lovelace/cards/hui-glance-card.ts | 3 +- src/panels/lovelace/cards/hui-light-card.ts | 3 +- .../lovelace/cards/hui-picture-entity-card.ts | 3 +- .../lovelace/cards/hui-picture-glance-card.ts | 6 +- src/panels/lovelace/cards/hui-tile-card.ts | 3 +- .../cards/hui-weather-forecast-card.ts | 3 +- .../elements/hui-state-label-element.ts | 7 ++- .../entity-rows/hui-group-entity-row.ts | 3 +- .../hui-input-number-entity-row.ts | 1 + .../hui-media-player-entity-row.ts | 7 ++- .../entity-rows/hui-number-entity-row.ts | 1 + .../entity-rows/hui-sensor-entity-row.ts | 3 +- .../entity-rows/hui-simple-entity-row.ts | 7 ++- .../entity-rows/hui-toggle-entity-row.ts | 3 +- .../entity-rows/hui-weather-entity-row.ts | 3 +- src/state-summary/state-card-configurator.js | 7 ++- src/state-summary/state-card-display.ts | 3 +- src/state-summary/state-card-input_number.js | 1 + src/state-summary/state-card-media_player.js | 7 ++- src/state/translations-mixin.ts | 2 +- test/common/entity/compute_state_display.ts | 58 +++++++++++++------ 36 files changed, 150 insertions(+), 47 deletions(-) diff --git a/gallery/src/pages/misc/entity-state.ts b/gallery/src/pages/misc/entity-state.ts index 16e2d7c73f..280ba22976 100644 --- a/gallery/src/pages/misc/entity-state.ts +++ b/gallery/src/pages/misc/entity-state.ts @@ -307,7 +307,8 @@ export class DemoEntityState extends LitElement { html`${computeStateDisplay( hass.localize, entry.stateObj, - hass.locale + hass.locale, + hass.entities )}`, }, device_class: { diff --git a/src/common/entity/compute_state_display.ts b/src/common/entity/compute_state_display.ts index 5a65756246..18a05de5ac 100644 --- a/src/common/entity/compute_state_display.ts +++ b/src/common/entity/compute_state_display.ts @@ -1,10 +1,12 @@ import { HassEntity } from "home-assistant-js-websocket"; import { UNAVAILABLE, UNKNOWN } from "../../data/entity"; +import { EntityRegistryEntry } from "../../data/entity_registry"; import { FrontendLocaleData } from "../../data/translation"; import { updateIsInstallingFromAttributes, UPDATE_SUPPORT_PROGRESS, } from "../../data/update"; +import { HomeAssistant } from "../../types"; import { formatDuration, UNIT_TO_SECOND_CONVERT } from "../datetime/duration"; import { formatDate } from "../datetime/format_date"; import { formatDateTime } from "../datetime/format_date_time"; @@ -23,11 +25,13 @@ export const computeStateDisplay = ( localize: LocalizeFunc, stateObj: HassEntity, locale: FrontendLocaleData, + entities: HomeAssistant["entities"], state?: string ): string => computeStateDisplayFromEntityAttributes( localize, locale, + entities, stateObj.entity_id, stateObj.attributes, state !== undefined ? state : stateObj.state @@ -36,6 +40,7 @@ export const computeStateDisplay = ( export const computeStateDisplayFromEntityAttributes = ( localize: LocalizeFunc, locale: FrontendLocaleData, + entities: HomeAssistant["entities"], entityId: string, attributes: any, state: string @@ -194,7 +199,13 @@ export const computeStateDisplayFromEntityAttributes = ( : localize("ui.card.update.up_to_date"); } + const entity = entities[entityId] as EntityRegistryEntry | undefined; + return ( + (entity?.translation_key && + localize( + `component.${entity.platform}.entity.${domain}.${entity.translation_key}.state.${state}` + )) || // Return device class translation (attributes.device_class && localize( diff --git a/src/components/entity/ha-entity-state-picker.ts b/src/components/entity/ha-entity-state-picker.ts index 98b5640b67..3536baa665 100644 --- a/src/components/entity/ha-entity-state-picker.ts +++ b/src/components/entity/ha-entity-state-picker.ts @@ -55,6 +55,7 @@ class HaEntityStatePicker extends LitElement { this.hass.localize, state, this.hass.locale, + this.hass.entities, key ) : formatAttributeValue(this.hass, key), diff --git a/src/components/entity/ha-state-label-badge.ts b/src/components/entity/ha-state-label-badge.ts index 5b05e0d7d7..3c7cc3dfa7 100644 --- a/src/components/entity/ha-state-label-badge.ts +++ b/src/components/entity/ha-state-label-badge.ts @@ -158,7 +158,8 @@ export class HaStateLabelBadge extends LitElement { : computeStateDisplay( this.hass!.localize, entityState, - this.hass!.locale + this.hass!.locale, + this.hass!.entities ); } } diff --git a/src/components/ha-water_heater-state.js b/src/components/ha-water_heater-state.js index 4fb1330fcb..d4dc750640 100644 --- a/src/components/ha-water_heater-state.js +++ b/src/components/ha-water_heater-state.js @@ -85,7 +85,12 @@ class HaWaterHeaterState extends LocalizeMixin(PolymerElement) { } _localizeState(stateObj) { - return computeStateDisplay(this.hass.localize, stateObj, this.hass.locale); + return computeStateDisplay( + this.hass.localize, + stateObj, + this.hass.locale, + this.hass.entities + ); } } customElements.define("ha-water_heater-state", HaWaterHeaterState); diff --git a/src/data/entity_registry.ts b/src/data/entity_registry.ts index 3802b26dca..a80562cb41 100644 --- a/src/data/entity_registry.ts +++ b/src/data/entity_registry.ts @@ -21,6 +21,7 @@ export interface EntityRegistryEntry { has_entity_name: boolean; original_name?: string; unique_id: string; + translation_key?: string; } export interface ExtEntityRegistryEntry extends EntityRegistryEntry { diff --git a/src/data/history.ts b/src/data/history.ts index 3f8777588a..9940292b72 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -184,6 +184,7 @@ const equalState = (obj1: LineChartState, obj2: LineChartState) => const processTimelineEntity = ( localize: LocalizeFunc, language: FrontendLocaleData, + entities: HomeAssistant["entities"], entityId: string, states: EntityHistoryState[], current_state: HassEntity | undefined @@ -198,6 +199,7 @@ const processTimelineEntity = ( state_localize: computeStateDisplayFromEntityAttributes( localize, language, + entities, entityId, state.a || first.a, state.s @@ -344,6 +346,7 @@ export const computeHistory = ( processTimelineEntity( localize, hass.locale, + hass.entities, entityId, stateInfo, currentState diff --git a/src/data/logbook.ts b/src/data/logbook.ts index 682eb111a5..c191ed8535 100644 --- a/src/data/logbook.ts +++ b/src/data/logbook.ts @@ -435,7 +435,13 @@ export const localizeStateMessage = ( `${LOGBOOK_LOCALIZE_PATH}.changed_to_state`, "state", stateObj - ? computeStateDisplay(localize, stateObj, hass.locale, state) + ? computeStateDisplay( + localize, + stateObj, + hass.locale, + hass.entities, + state + ) : state ); }; diff --git a/src/data/timer.ts b/src/data/timer.ts index 4631454e12..a06a865bf3 100644 --- a/src/data/timer.ts +++ b/src/data/timer.ts @@ -90,7 +90,12 @@ export const computeDisplayTimer = ( } if (stateObj.state === "idle" || timeRemaining === 0) { - return computeStateDisplay(hass.localize, stateObj, hass.locale); + return computeStateDisplay( + hass.localize, + stateObj, + hass.locale, + hass.entities + ); } let display = secondsToDuration(timeRemaining || 0); @@ -99,7 +104,8 @@ export const computeDisplayTimer = ( display = `${display} (${computeStateDisplay( hass.localize, stateObj, - hass.locale + hass.locale, + hass.entities )})`; } diff --git a/src/data/translation.ts b/src/data/translation.ts index e47d9d031b..56764f922a 100644 --- a/src/data/translation.ts +++ b/src/data/translation.ts @@ -44,6 +44,7 @@ declare global { export type TranslationCategory = | "title" | "state" + | "entity" | "config" | "config_panel" | "options" diff --git a/src/dialogs/notifications/configurator-notification-item.ts b/src/dialogs/notifications/configurator-notification-item.ts index f57932ca5e..2ec0c037c7 100644 --- a/src/dialogs/notifications/configurator-notification-item.ts +++ b/src/dialogs/notifications/configurator-notification-item.ts @@ -37,7 +37,8 @@ export class HuiConfiguratorNotificationItem extends LitElement { >${computeStateDisplay( this.hass.localize, this.notification, - this.hass.locale + this.hass.locale, + this.hass.entities )} diff --git a/src/fake_data/provide_hass.ts b/src/fake_data/provide_hass.ts index 8a6d1c36f1..2c717528ee 100644 --- a/src/fake_data/provide_hass.ts +++ b/src/fake_data/provide_hass.ts @@ -307,7 +307,9 @@ export const provideHass = ( true ); }, - + areas: {}, + devices: {}, + entities: {}, ...overrideData, }; diff --git a/src/layouts/home-assistant.ts b/src/layouts/home-assistant.ts index f75efdb624..3b0d618dd9 100644 --- a/src/layouts/home-assistant.ts +++ b/src/layouts/home-assistant.ts @@ -137,6 +137,8 @@ export class HomeAssistantAppEl extends QuickBarMixin(HassElement) { super.hassConnected(); // @ts-ignore this._loadHassTranslations(this.hass!.language, "state"); + // @ts-ignore + this._loadHassTranslations(this.hass!.language, "entity"); document.addEventListener( "visibilitychange", diff --git a/src/panels/lovelace/cards/hui-button-card.ts b/src/panels/lovelace/cards/hui-button-card.ts index e75e08f150..3e274f957a 100644 --- a/src/panels/lovelace/cards/hui-button-card.ts +++ b/src/panels/lovelace/cards/hui-button-card.ts @@ -205,7 +205,8 @@ export class HuiButtonCard extends LitElement implements LovelaceCard { ${computeStateDisplay( this.hass.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )} ` : ""} diff --git a/src/panels/lovelace/cards/hui-entity-card.ts b/src/panels/lovelace/cards/hui-entity-card.ts index 95ce2d82e6..692d67b5ba 100644 --- a/src/panels/lovelace/cards/hui-entity-card.ts +++ b/src/panels/lovelace/cards/hui-entity-card.ts @@ -167,7 +167,8 @@ export class HuiEntityCard extends LitElement implements LovelaceCard { : computeStateDisplay( this.hass.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )}${showUnit ? html` diff --git a/src/panels/lovelace/cards/hui-glance-card.ts b/src/panels/lovelace/cards/hui-glance-card.ts index b640284bfd..7c1668d7b8 100644 --- a/src/panels/lovelace/cards/hui-glance-card.ts +++ b/src/panels/lovelace/cards/hui-glance-card.ts @@ -335,7 +335,8 @@ export class HuiGlanceCard extends LitElement implements LovelaceCard { : computeStateDisplay( this.hass!.localize, stateObj, - this.hass!.locale + this.hass!.locale, + this.hass!.entities )} ` diff --git a/src/panels/lovelace/cards/hui-light-card.ts b/src/panels/lovelace/cards/hui-light-card.ts index 6717feef65..37dcd61603 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -160,7 +160,8 @@ export class HuiLightCard extends LitElement implements LovelaceCard { ${computeStateDisplay( this.hass.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )} ` diff --git a/src/panels/lovelace/cards/hui-picture-entity-card.ts b/src/panels/lovelace/cards/hui-picture-entity-card.ts index aaab63cb0f..c1e3efbd80 100644 --- a/src/panels/lovelace/cards/hui-picture-entity-card.ts +++ b/src/panels/lovelace/cards/hui-picture-entity-card.ts @@ -121,7 +121,8 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard { const entityState = computeStateDisplay( this.hass!.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities ); let footer: TemplateResult | string = ""; diff --git a/src/panels/lovelace/cards/hui-picture-glance-card.ts b/src/panels/lovelace/cards/hui-picture-glance-card.ts index c46d669a9f..eda0d51fec 100644 --- a/src/panels/lovelace/cards/hui-picture-glance-card.ts +++ b/src/panels/lovelace/cards/hui-picture-glance-card.ts @@ -255,7 +255,8 @@ class HuiPictureGlanceCard extends LitElement implements LovelaceCard { title=${`${computeStateName(stateObj)} : ${computeStateDisplay( this.hass!.localize, stateObj, - this.hass!.locale + this.hass!.locale, + this.hass!.entities )}`} > `} diff --git a/src/panels/lovelace/cards/hui-tile-card.ts b/src/panels/lovelace/cards/hui-tile-card.ts index dbfc55d6de..75c21f56b9 100644 --- a/src/panels/lovelace/cards/hui-tile-card.ts +++ b/src/panels/lovelace/cards/hui-tile-card.ts @@ -201,7 +201,8 @@ export class HuiTileCard extends LitElement implements LovelaceCard { return computeStateDisplay( this.hass!.localize, stateObj, - this.hass!.locale + this.hass!.locale, + this.hass!.entities ); } diff --git a/src/panels/lovelace/cards/hui-weather-forecast-card.ts b/src/panels/lovelace/cards/hui-weather-forecast-card.ts index 91e485d284..7c0507d0a8 100644 --- a/src/panels/lovelace/cards/hui-weather-forecast-card.ts +++ b/src/panels/lovelace/cards/hui-weather-forecast-card.ts @@ -221,7 +221,8 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { ${computeStateDisplay( this.hass.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )}
${name}
diff --git a/src/panels/lovelace/elements/hui-state-label-element.ts b/src/panels/lovelace/elements/hui-state-label-element.ts index 23efa72506..a45d32ac60 100644 --- a/src/panels/lovelace/elements/hui-state-label-element.ts +++ b/src/panels/lovelace/elements/hui-state-label-element.ts @@ -83,7 +83,12 @@ class HuiStateLabelElement extends LitElement implements LovelaceElement { )} > ${this._config.prefix}${!this._config.attribute - ? computeStateDisplay(this.hass.localize, stateObj, this.hass.locale) + ? computeStateDisplay( + this.hass.localize, + stateObj, + this.hass.locale, + this.hass.entities + ) : stateObj.attributes[this._config.attribute]}${this._config.suffix} `; diff --git a/src/panels/lovelace/entity-rows/hui-group-entity-row.ts b/src/panels/lovelace/entity-rows/hui-group-entity-row.ts index 9a0e24aa7c..e837cf2f67 100644 --- a/src/panels/lovelace/entity-rows/hui-group-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-group-entity-row.ts @@ -69,7 +69,8 @@ class HuiGroupEntityRow extends LitElement implements LovelaceRow { ${computeStateDisplay( this.hass!.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )} `} diff --git a/src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts b/src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts index 28230bb774..1cdd25958c 100644 --- a/src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts @@ -100,6 +100,7 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow { this.hass.localize, stateObj, this.hass.locale, + this.hass.entities, stateObj.state )} diff --git a/src/panels/lovelace/entity-rows/hui-media-player-entity-row.ts b/src/panels/lovelace/entity-rows/hui-media-player-entity-row.ts index f34a0557dc..68e5526c50 100644 --- a/src/panels/lovelace/entity-rows/hui-media-player-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-media-player-entity-row.ts @@ -193,7 +193,12 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow { .hass=${this.hass} .config=${this._config} .secondaryText=${mediaDescription || - computeStateDisplay(this.hass.localize, stateObj, this.hass.locale)} + computeStateDisplay( + this.hass.localize, + stateObj, + this.hass.locale, + this.hass.entities + )} >
${supportsFeature(stateObj, SUPPORT_TURN_ON) && diff --git a/src/panels/lovelace/entity-rows/hui-number-entity-row.ts b/src/panels/lovelace/entity-rows/hui-number-entity-row.ts index 22f5c34eb6..c9fe9659ee 100644 --- a/src/panels/lovelace/entity-rows/hui-number-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-number-entity-row.ts @@ -104,6 +104,7 @@ class HuiNumberEntityRow extends LitElement implements LovelaceRow { this.hass.localize, stateObj, this.hass.locale, + this.hass.entities, stateObj.state )} diff --git a/src/panels/lovelace/entity-rows/hui-sensor-entity-row.ts b/src/panels/lovelace/entity-rows/hui-sensor-entity-row.ts index ba95580d28..297bf97aeb 100644 --- a/src/panels/lovelace/entity-rows/hui-sensor-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-sensor-entity-row.ts @@ -83,7 +83,8 @@ class HuiSensorEntityRow extends LitElement implements LovelaceRow { : computeStateDisplay( this.hass!.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )}
diff --git a/src/panels/lovelace/entity-rows/hui-simple-entity-row.ts b/src/panels/lovelace/entity-rows/hui-simple-entity-row.ts index e0dde82dd5..735d4ba7ed 100644 --- a/src/panels/lovelace/entity-rows/hui-simple-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-simple-entity-row.ts @@ -49,7 +49,12 @@ class HuiSimpleEntityRow extends LitElement implements LovelaceRow { return html` - ${computeStateDisplay(this.hass!.localize, stateObj, this.hass.locale)} + ${computeStateDisplay( + this.hass!.localize, + stateObj, + this.hass.locale, + this.hass.entities + )} `; } diff --git a/src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts b/src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts index 79eab48a0d..3f6e638a78 100644 --- a/src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts @@ -64,7 +64,8 @@ class HuiToggleEntityRow extends LitElement implements LovelaceRow { ${computeStateDisplay( this.hass!.localize, stateObj, - this.hass!.locale + this.hass!.locale, + this.hass!.entities )} `} diff --git a/src/panels/lovelace/entity-rows/hui-weather-entity-row.ts b/src/panels/lovelace/entity-rows/hui-weather-entity-row.ts index 3b0d17ff37..d8c53fa802 100644 --- a/src/panels/lovelace/entity-rows/hui-weather-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-weather-entity-row.ts @@ -120,7 +120,8 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow { ? computeStateDisplay( this.hass.localize, stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities ) : html` ${formatNumber( diff --git a/src/state-summary/state-card-configurator.js b/src/state-summary/state-card-configurator.js index 6240406ba4..98ab9a1bd1 100644 --- a/src/state-summary/state-card-configurator.js +++ b/src/state-summary/state-card-configurator.js @@ -58,7 +58,12 @@ class StateCardConfigurator extends LocalizeMixin(PolymerElement) { } _localizeState(stateObj) { - return computeStateDisplay(this.hass.localize, stateObj, this.hass.locale); + return computeStateDisplay( + this.hass.localize, + stateObj, + this.hass.locale, + this.hass.entities + ); } } customElements.define("state-card-configurator", StateCardConfigurator); diff --git a/src/state-summary/state-card-display.ts b/src/state-summary/state-card-display.ts index bda437929d..0d42269577 100755 --- a/src/state-summary/state-card-display.ts +++ b/src/state-summary/state-card-display.ts @@ -52,7 +52,8 @@ export class StateCardDisplay extends LitElement { : computeStateDisplay( this.hass!.localize, this.stateObj, - this.hass.locale + this.hass.locale, + this.hass.entities )} diff --git a/src/state-summary/state-card-input_number.js b/src/state-summary/state-card-input_number.js index 49efced50a..ced62e2984 100644 --- a/src/state-summary/state-card-input_number.js +++ b/src/state-summary/state-card-input_number.js @@ -165,6 +165,7 @@ class StateCardInputNumber extends mixinBehaviors( this.hass.localize, newVal, this.hass.locale, + this.hass.entities, newVal.state ), mode: String(newVal.attributes.mode), diff --git a/src/state-summary/state-card-media_player.js b/src/state-summary/state-card-media_player.js index 93a957b0d9..6f5e243639 100644 --- a/src/state-summary/state-card-media_player.js +++ b/src/state-summary/state-card-media_player.js @@ -85,7 +85,12 @@ class StateCardMediaPlayer extends LocalizeMixin(PolymerElement) { computePrimaryText(localize, playerObj) { return ( playerObj.primaryTitle || - computeStateDisplay(localize, playerObj.stateObj, this.hass.locale) + computeStateDisplay( + localize, + playerObj.stateObj, + this.hass.locale, + this.hass.entities + ) ); } } diff --git a/src/state/translations-mixin.ts b/src/state/translations-mixin.ts index b0848fcb4e..5531fbacde 100644 --- a/src/state/translations-mixin.ts +++ b/src/state/translations-mixin.ts @@ -234,7 +234,7 @@ export default >(superClass: T) => category: Parameters[2], integration?: Parameters[3], configFlow?: Parameters[4], - force = false + force = true ): Promise { if ( __BACKWARDS_COMPAT__ && diff --git a/test/common/entity/compute_state_display.ts b/test/common/entity/compute_state_display.ts index e4c7e0b198..26284d5af9 100644 --- a/test/common/entity/compute_state_display.ts +++ b/test/common/entity/compute_state_display.ts @@ -31,7 +31,7 @@ describe("computeStateDisplay", () => { attributes: {}, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "component.binary_sensor.state._.off" ); }); @@ -45,7 +45,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "component.binary_sensor.state.moisture.off" ); }); @@ -65,7 +65,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "component.binary_sensor.state.invalid_device_class.off" ); }); @@ -79,7 +79,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "123 m" ); }); @@ -93,7 +93,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "1,234.5 m" ); }); @@ -107,7 +107,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "1,234.5" ); }); @@ -127,7 +127,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "state.default.unknown" ); }); @@ -147,7 +147,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "state.default.unavailable" ); }); @@ -165,7 +165,7 @@ describe("computeStateDisplay", () => { attributes: {}, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "component.sensor.state._.custom_state" ); }); @@ -187,14 +187,14 @@ describe("computeStateDisplay", () => { }; it("Uses am/pm time format", () => { assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "November 18, 2017 at 11:12 PM" ); }); it("Uses 24h time format", () => { localeData.time_format = TimeFormat.twenty_four; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "November 18, 2017 at 23:12" ); }); @@ -216,7 +216,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "November 18, 2017" ); }); @@ -239,14 +239,14 @@ describe("computeStateDisplay", () => { it("Uses am/pm time format", () => { localeData.time_format = TimeFormat.am_pm; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "11:12 PM" ); }); it("Uses 24h time format", () => { localeData.time_format = TimeFormat.twenty_four; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData), + computeStateDisplay(localize, stateObj, localeData, {}), "23:12" ); }); @@ -273,6 +273,7 @@ describe("computeStateDisplay", () => { localize, stateObj, localeData, + {}, "2021-07-04 15:40:03" ), "July 4, 2021 at 3:40 PM" @@ -285,6 +286,7 @@ describe("computeStateDisplay", () => { localize, stateObj, localeData, + {}, "2021-07-04 15:40:03" ), "July 4, 2021 at 15:40" @@ -308,7 +310,7 @@ describe("computeStateDisplay", () => { }, }; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData, "2021-07-04"), + computeStateDisplay(localize, stateObj, localeData, {}, "2021-07-04"), "July 4, 2021" ); }); @@ -331,14 +333,14 @@ describe("computeStateDisplay", () => { it("Uses am/pm time format", () => { localeData.time_format = TimeFormat.am_pm; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData, "17:05:07"), + computeStateDisplay(localize, stateObj, localeData, {}, "17:05:07"), "5:05 PM" ); }); it("Uses 24h time format", () => { localeData.time_format = TimeFormat.twenty_four; assert.strictEqual( - computeStateDisplay(localize, stateObj, localeData, "17:05:07"), + computeStateDisplay(localize, stateObj, localeData, {}, "17:05:07"), "17:05" ); }); @@ -357,7 +359,7 @@ describe("computeStateDisplay", () => { attributes: {}, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "state.default.unavailable" ); }); @@ -372,8 +374,26 @@ describe("computeStateDisplay", () => { attributes: {}, }; assert.strictEqual( - computeStateDisplay(altLocalize, stateObj, localeData), + computeStateDisplay(altLocalize, stateObj, localeData, {}), "My Custom State" ); }); + + it("Localizes using translation key", () => { + const stateObj: any = { + entity_id: "sensor.test", + state: "custom_state", + attributes: {}, + }; + const entities: any = { + "sensor.test": { + translation_key: "custom_translation", + platform: "custom_integration", + }, + }; + assert.strictEqual( + computeStateDisplay(localize, stateObj, localeData, entities), + "component.custom_integration.entity.sensor.custom_translation.state.custom_state" + ); + }); });