Adjust backend translations for entity components (#15820)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Franck Nijhof 2023-03-16 13:51:38 +01:00 committed by GitHub
parent 77dcace95e
commit 0158d7e3e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 37 additions and 16 deletions

View File

@ -12,6 +12,7 @@ export const computeAttributeValueDisplay = (
value?: any value?: any
): string => { ): string => {
const entityId = stateObj.entity_id; const entityId = stateObj.entity_id;
const deviceClass = stateObj.attributes.device_class;
const attributeValue = const attributeValue =
value !== undefined ? value : stateObj.attributes[attribute]; value !== undefined ? value : stateObj.attributes[attribute];
const domain = computeDomain(entityId); const domain = computeDomain(entityId);
@ -23,8 +24,12 @@ export const computeAttributeValueDisplay = (
localize( localize(
`component.${entity.platform}.entity.${domain}.${translationKey}.state_attributes.${attribute}.state.${attributeValue}` `component.${entity.platform}.entity.${domain}.${translationKey}.state_attributes.${attribute}.state.${attributeValue}`
)) || )) ||
(deviceClass &&
localize(
`component.${domain}.entity_component.${deviceClass}.state_attributes.${attribute}.state.${attributeValue}`
)) ||
localize( localize(
`component.${domain}.state_attributes._.${attribute}.state.${attributeValue}` `component.${domain}.entity_component._.state_attributes.${attribute}.state.${attributeValue}`
) || ) ||
attributeValue attributeValue
); );
@ -37,6 +42,7 @@ export const computeAttributeNameDisplay = (
attribute: string attribute: string
): string => { ): string => {
const entityId = stateObj.entity_id; const entityId = stateObj.entity_id;
const deviceClass = stateObj.attributes.device_class;
const domain = computeDomain(entityId); const domain = computeDomain(entityId);
const entity = entities[entityId] as EntityRegistryDisplayEntry | undefined; const entity = entities[entityId] as EntityRegistryDisplayEntry | undefined;
const translationKey = entity?.translation_key; const translationKey = entity?.translation_key;
@ -46,7 +52,13 @@ export const computeAttributeNameDisplay = (
localize( localize(
`component.${entity.platform}.entity.${domain}.${translationKey}.state_attributes.${attribute}.name` `component.${entity.platform}.entity.${domain}.${translationKey}.state_attributes.${attribute}.name`
)) || )) ||
localize(`component.${domain}.state_attributes._.${attribute}.name`) || (deviceClass &&
localize(
`component.${domain}.entity_component.${deviceClass}.state_attributes.${attribute}.name`
)) ||
localize(
`component.${domain}.entity_component._.state_attributes.${attribute}.name`
) ||
attribute attribute
); );
}; };

View File

@ -214,10 +214,10 @@ export const computeStateDisplayFromEntityAttributes = (
// Return device class translation // Return device class translation
(attributes.device_class && (attributes.device_class &&
localize( localize(
`component.${domain}.state.${attributes.device_class}.${state}` `component.${domain}.entity_component.${attributes.device_class}.state.${state}`
)) || )) ||
// Return default translation // Return default translation
localize(`component.${domain}.state._.${state}`) || localize(`component.${domain}.entity_component._.state.${state}`) ||
// We don't know! Return the raw state. // We don't know! Return the raw state.
state state
); );

View File

@ -76,7 +76,7 @@ class HaVacuumState extends LocalizeMixin(PolymerElement) {
? this.localize( ? this.localize(
`ui.card.vacuum.actions.${STATES_INTERCEPTABLE[state].action}` `ui.card.vacuum.actions.${STATES_INTERCEPTABLE[state].action}`
) )
: this.localize(`component.vacuum._.${state}`); : this.localize(`component.vacuum.entity_component._.state.${state}`);
} }
_callService(ev) { _callService(ev) {

View File

@ -44,8 +44,8 @@ declare global {
export type TranslationCategory = export type TranslationCategory =
| "title" | "title"
| "state" | "state"
| "state_attributes"
| "entity" | "entity"
| "entity_component"
| "config" | "config"
| "config_panel" | "config_panel"
| "options" | "options"

View File

@ -115,7 +115,7 @@ class MoreInfoVacuum extends LitElement {
<strong> <strong>
${stateObj.attributes.status || ${stateObj.attributes.status ||
this.hass.localize( this.hass.localize(
`component.vacuum.state._.${stateObj.state}` `component.vacuum.entity_component._.state.${stateObj.state}`
) || ) ||
stateObj.state} stateObj.state}
</strong> </strong>

View File

@ -225,7 +225,10 @@ class MoreInfoWaterHeater extends LocalizeMixin(EventsMixin(PolymerElement)) {
} }
_localizeOperationMode(localize, mode) { _localizeOperationMode(localize, mode) {
return localize(`component.water_heater.state._.${mode}`) || mode; return (
localize(`component.water_heater.entity_component._.state.${mode}`) ||
mode
);
} }
} }

View File

@ -136,12 +136,14 @@ export class HomeAssistantAppEl extends QuickBarMixin(HassElement) {
protected hassConnected() { protected hassConnected() {
super.hassConnected(); super.hassConnected();
// @ts-ignore // @ts-ignore
this._loadHassTranslations(this.hass!.language, "state"); this._loadHassTranslations(this.hass!.language, "entity_component");
// @ts-ignore
this._loadHassTranslations(this.hass!.language, "state_attributes");
// @ts-ignore // @ts-ignore
this._loadHassTranslations(this.hass!.language, "entity"); this._loadHassTranslations(this.hass!.language, "entity");
// Backwards compatibility for custom integrations
// @ts-ignore
this._loadHassTranslations(this.hass!.language, "state");
document.addEventListener( document.addEventListener(
"visibilitychange", "visibilitychange",
() => this._checkVisibility(), () => this._checkVisibility(),

View File

@ -236,7 +236,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard {
return entityState === UNAVAILABLE return entityState === UNAVAILABLE
? this.hass!.localize("state.default.unavailable") ? this.hass!.localize("state.default.unavailable")
: this.hass!.localize( : this.hass!.localize(
`component.alarm_control_panel.state._.${entityState}` `component.alarm_control_panel.entity_component._.state.${entityState}`
) || entityState; ) || entityState;
} }

View File

@ -478,7 +478,9 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
@click=${this._handleAction} @click=${this._handleAction}
tabindex="0" tabindex="0"
.path=${modeIcons[mode]} .path=${modeIcons[mode]}
.label=${this.hass!.localize(`component.climate.state._.${mode}`)} .label=${this.hass!.localize(
`component.climate.entity_component._.state.${mode}`
) || mode}
> >
</ha-icon-button> </ha-icon-button>
`; `;

View File

@ -32,7 +32,7 @@ describe("computeStateDisplay", () => {
}; };
assert.strictEqual( assert.strictEqual(
computeStateDisplay(localize, stateObj, localeData, {}), computeStateDisplay(localize, stateObj, localeData, {}),
"component.binary_sensor.state._.off" "component.binary_sensor.entity_component._.state.off"
); );
}); });
@ -154,7 +154,9 @@ describe("computeStateDisplay", () => {
it("Localizes sensor value with component translation", () => { it("Localizes sensor value with component translation", () => {
const altLocalize = (message, ...args) => { const altLocalize = (message, ...args) => {
if (message !== "component.sensor.state._.custom_state") { if (
message !== "component.sensor.entity_component._.state.custom_state"
) {
return ""; return "";
} }
return localize(message, ...args); return localize(message, ...args);
@ -166,7 +168,7 @@ describe("computeStateDisplay", () => {
}; };
assert.strictEqual( assert.strictEqual(
computeStateDisplay(altLocalize, stateObj, localeData, {}), computeStateDisplay(altLocalize, stateObj, localeData, {}),
"component.sensor.state._.custom_state" "component.sensor.entity_component._.state.custom_state"
); );
}); });