Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 27 additions & 50 deletions src/common/entity/compute_state_display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,34 @@ import { formatDateTime } from "../datetime/format_date_time";
import { formatTime } from "../datetime/format_time";
import { LocalizeFunc } from "../translations/localize";
import { computeStateDomain } from "./compute_state_domain";
import { UNKNOWN, UNAVAILABLE } from "../../data/entity";

export const computeStateDisplay = (
localize: LocalizeFunc,
stateObj: HassEntity,
language: string
): string => {
let display: string | undefined;
const domain = computeStateDomain(stateObj);
if (stateObj.state === UNKNOWN || stateObj.state === UNAVAILABLE) {
return localize(`state.default.${stateObj.state}`);
}

if (domain === "binary_sensor") {
// Try device class translation, then default binary sensor translation
if (stateObj.attributes.device_class) {
display = localize(
`state.${domain}.${stateObj.attributes.device_class}.${stateObj.state}`
);
}
if (stateObj.attributes.unit_of_measurement) {
return `${stateObj.state} ${stateObj.attributes.unit_of_measurement}`;
}

if (!display) {
display = localize(`state.${domain}.default.${stateObj.state}`);
}
} else if (
stateObj.attributes.unit_of_measurement &&
!["unknown", "unavailable"].includes(stateObj.state)
) {
display = stateObj.state + " " + stateObj.attributes.unit_of_measurement;
} else if (domain === "input_datetime") {
const domain = computeStateDomain(stateObj);

if (domain === "input_datetime") {
let date: Date;
if (!stateObj.attributes.has_time) {
date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day
);
display = formatDate(date, language);
} else if (!stateObj.attributes.has_date) {
return formatDate(date, language);
}
if (!stateObj.attributes.has_date) {
const now = new Date();
date = new Date(
// Due to bugs.chromium.org/p/chromium/issues/detail?id=797548
Expand All @@ -49,38 +42,22 @@ export const computeStateDisplay = (
stateObj.attributes.hour,
stateObj.attributes.minute
);
display = formatTime(date, language);
} else {
date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day,
stateObj.attributes.hour,
stateObj.attributes.minute
);
display = formatDateTime(date, language);
}
} else if (domain === "zwave") {
if (["initializing", "dead"].includes(stateObj.state)) {
display = localize(
`state.zwave.query_stage.${stateObj.state}`,
"query_stage",
stateObj.attributes.query_stage
);
} else {
display = localize(`state.zwave.default.${stateObj.state}`);
return formatTime(date, language);
}
} else {
display = localize(`state.${domain}.${stateObj.state}`);
}

// Fall back to default, component backend translation, or raw state if nothing else matches.
if (!display) {
display =
localize(`state.default.${stateObj.state}`) ||
localize(`component.${domain}.state.${stateObj.state}`) ||
stateObj.state;
date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day,
stateObj.attributes.hour,
stateObj.attributes.minute
);
return formatDateTime(date, language);
}

return display;
const deviceClass = stateObj.attributes.device_class || "_";
return (
localize(`component.${domain}.state.${deviceClass}.${stateObj.state}`) ||
stateObj.state
);
};
10 changes: 8 additions & 2 deletions src/components/entity/ha-state-label-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { stateIcon } from "../../common/entity/state_icon";
import { timerTimeRemaining } from "../../common/entity/timer_time_remaining";
import { HomeAssistant } from "../../types";
import "../ha-label-badge";
import { computeStateDisplay } from "../../common/entity/compute_state_display";

@customElement("ha-state-label-badge")
export class HaStateLabelBadge extends LitElement {
Expand Down Expand Up @@ -108,8 +109,13 @@ export class HaStateLabelBadge extends LitElement {
default:
return state.state === "unknown"
? "-"
: this.hass!.localize(`component.${domain}.state.${state.state}`) ||
state.state;
: state.attributes.unit_of_measurement
? state.state
: computeStateDisplay(
this.hass!.localize,
state,
this.hass!.language
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/panels/config/zwave/ha-config-zwave.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class HaConfigZwave extends LocalizeMixin(EventsMixin(PolymerElement)) {
on-click="_backTapped"
></ha-paper-icon-button-arrow-prev>
<div main-title="">
[[localize('ui.panel.config.zwave.caption')]]
[[localize('component.zwave.title')]]
</div>
</app-toolbar>
</app-header>
Expand Down
14 changes: 10 additions & 4 deletions src/state/translations-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
});
this.hass!.connection.subscribeEvents(
debounce(() => {
this._refetchCachedHassTranslations(false);
this._refetchCachedHassTranslations(false, false);
}, 500),
"component_loaded"
);
Expand All @@ -68,7 +68,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>

protected hassReconnected() {
super.hassReconnected();
this._refetchCachedHassTranslations(true);
this._refetchCachedHassTranslations(true, false);
this._applyTranslations(this.hass!);
}

Expand All @@ -94,7 +94,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
saveTranslationPreferences(this.hass, { language });
}
this._applyTranslations(this.hass);
this._refetchCachedHassTranslations(true);
this._refetchCachedHassTranslations(true, true);
}

private _applyTranslations(hass: HomeAssistant) {
Expand Down Expand Up @@ -227,10 +227,16 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
this._updateHass(changes);
}

private _refetchCachedHassTranslations(includeConfigFlow: boolean) {
private _refetchCachedHassTranslations(
includeConfigFlow: boolean,
clearIntegrations: boolean
) {
for (const [category, cache] of Object.entries(
this.__loadedTranslations
)) {
if (clearIntegrations) {
cache.integrations = [];
}
if (cache.setup) {
this._loadHassTranslations(
this.hass!.language,
Expand Down
1 change: 0 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,6 @@
}
},
"zwave": {
"caption": "Z-Wave",
"description": "Manage your Z-Wave network",
"learn_more": "Learn more about Z-Wave",
"common": {
Expand Down
50 changes: 5 additions & 45 deletions test-mocha/common/entity/compute_state_display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("computeStateDisplay", () => {
};
assert.strictEqual(
computeStateDisplay(localize, stateObj, "en"),
"state.binary_sensor.default.off"
"component.binary_sensor.state._.off"
);
});

Expand All @@ -28,7 +28,7 @@ describe("computeStateDisplay", () => {
};
assert.strictEqual(
computeStateDisplay(localize, stateObj, "en"),
"state.binary_sensor.moisture.off"
"component.binary_sensor.state.moisture.off"
);
});

Expand All @@ -48,7 +48,7 @@ describe("computeStateDisplay", () => {
};
assert.strictEqual(
computeStateDisplay(altLocalize, stateObj, "en"),
"state.binary_sensor.default.off"
"component.binary_sensor.state.invalid_device_class.off"
);
});

Expand Down Expand Up @@ -105,7 +105,7 @@ describe("computeStateDisplay", () => {

it("Localizes sensor value with component translation", () => {
const altLocalize = (message, ...args) => {
if (message !== "component.sensor.state.custom_state") {
if (message !== "component.sensor.state._.custom_state") {
return "";
}
return localize(message, ...args);
Expand All @@ -117,7 +117,7 @@ describe("computeStateDisplay", () => {
};
assert.strictEqual(
computeStateDisplay(altLocalize, stateObj, "en"),
"component.sensor.state.custom_state"
"component.sensor.state._.custom_state"
);
});

Expand Down Expand Up @@ -184,46 +184,6 @@ describe("computeStateDisplay", () => {
);
});

it("Localizes zwave ready", () => {
const stateObj: any = {
entity_id: "zwave.test",
state: "ready",
attributes: {
query_stage: "Complete",
},
};
assert.strictEqual(
computeStateDisplay(localize, stateObj, "en"),
"state.zwave.default.ready"
);
});

it("Localizes zwave initializing", () => {
const stateObj: any = {
entity_id: "zwave.test",
state: "initializing",
attributes: {
query_stage: "Probe",
},
};
assert.strictEqual(
computeStateDisplay(localize, stateObj, "en"),
"state.zwave.query_stage.initializing: query_stage,Probe"
);
});

it("Localizes cover open", () => {
const stateObj: any = {
entity_id: "cover.test",
state: "open",
attributes: {},
};
assert.strictEqual(
computeStateDisplay(localize, stateObj, "en"),
"state.cover.open"
);
});

it("Localizes unavailable", () => {
const altLocalize = (message, ...args) => {
if (message === "state.sensor.unavailable") {
Expand Down