Skip to content

Commit

Permalink
Use different computeStateDisplay implementation for 0.109 or later
Browse files Browse the repository at this point in the history
  • Loading branch information
gadgetchnnel committed May 1, 2020
1 parent 86ad085 commit 1df2a55
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 80 deletions.
16 changes: 8 additions & 8 deletions lovelace-home-feed-card.js

Large diffs are not rendered by default.

96 changes: 35 additions & 61 deletions src/helpers/compute-state-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,28 @@ import { computeStateDomain } from "./compute-state-domain.js";
export function computeStateDisplay(localize, stateObj, language) {
let display;

if (stateObj.state === "unknown" || stateObj.state === "unavailable") {
return localize(`state.default.${stateObj.state}`);
}

if (stateObj.attributes.unit_of_measurement) {
return `${stateObj.state} ${stateObj.attributes.unit_of_measurement}`;
}

const domain = computeStateDomain(stateObj);

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(!display){
display = localize(
`component.${domain}.state.${stateObj.attributes.device_class}.${stateObj.state}`
);
}
}

if (!display) {
display = localize(`state.${domain}.default.${stateObj.state}`);
if(!display){
display = localize(
`component.${domain}.state._.${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") {

if (domain === "input_datetime") {
let 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 @@ -54,38 +38,28 @@ export function computeStateDisplay(localize, stateObj, language) {
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);
return formatTime(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}`);
}
} 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;

return (
// Return device class translation
(stateObj.attributes.device_class &&
localize(
`component.${domain}.state.${stateObj.attributes.device_class}.${stateObj.state}`
)) ||
// Return default translation
localize(`component.${domain}.state._.${stateObj.state}`) ||
// We don't know! Return the raw state.
stateObj.state
);
}
5 changes: 5 additions & 0 deletions src/helpers/hass-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function versionGreaterOrEqual(version1, version2){
var version1Cleaned = version1.split('.').map(d => String(parseInt(d)).padStart(3,'0')).join('.');
var version2Cleaned = version2.split('.').map(d => String(parseInt(d)).padStart(3,'0')).join('.');
return version1Cleaned >= version2Cleaned;
}
22 changes: 11 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LitElement, html, css } from "./lit-element.js";
import { HomeFeedNotificationPopup } from "./popup.js";
import { versionGreaterOrEqual } from "./helpers/hass-version.js";
import { computeStateDisplay as computeStateDisplayHelper } from "./helpers/compute-state-display.js";
import { handleClick } from "custom-card-helpers";
import { handleClick, computeStateDisplay as computeStateDisplayLegacy } from "custom-card-helpers";
import { createCard } from "card-tools/src/lovelace-element";

import { getCalendarString } from "./locale.js";
Expand Down Expand Up @@ -243,7 +244,9 @@ class HomeFeedCard extends LitElement {
var state = entityConfig.state_map && entityConfig.state_map[stateObj.state] ? entityConfig.state_map[stateObj.state] : null;

if(!state){
state = computeStateDisplayHelper(this._hass.localize, stateObj);
state = versionGreaterOrEqual(this.hass_version, "0.109.0")
? computeStateDisplayHelper(this._hass.localize, stateObj)
: computeStateDisplayLegacy(this._hass.localize, stateObj);
}

return state;
Expand Down Expand Up @@ -792,7 +795,7 @@ class HomeFeedCard extends LitElement {
}

const d = new Date(notification.created_at);
return d.toLocaleDateString(this.browser_language, {
return d.toLocaleDateString(this._hass.language, {
year: "numeric",
month: "short",
day: "numeric",
Expand Down Expand Up @@ -917,7 +920,7 @@ class HomeFeedCard extends LitElement {
if(endDate > date) {
let endTimeString = getCalendarString(endDate);
if(endTimeString == endDate.format("L")){
endTimeString = endDate.toDate().toLocaleDateString(this.browser_language, {
endTimeString = endDate.toDate().toLocaleDateString(this._hass.language, {
year: "numeric",
month: "long",
day: "numeric",
Expand All @@ -929,14 +932,14 @@ class HomeFeedCard extends LitElement {
timeItem = html`<div style="display:block; ${compact_mode ? "float:right" : "clear:both;"}" title="${date.toDate()} - ${endDate.toDate()}">${timeString}</div>`;
}
else{
var timeString = date.toDate().toLocaleDateString(this.browser_language, {
var timeString = date.toDate().toLocaleDateString(this._language, {
year: "numeric",
month: "long",
day: "numeric",
});

if(endDate > date) {
timeString = timeString + " - " + endDate.toDate().toLocaleDateString(this.browser_language, {
timeString = timeString + " - " + endDate.toDate().toLocaleDateString(this._hass.language, {
year: "numeric",
month: "long",
day: "numeric",
Expand Down Expand Up @@ -971,13 +974,9 @@ class HomeFeedCard extends LitElement {
}
else {
// Time difference creater than or equal to 1 minute, so use hui-timestamp-display in relative mode
let tsHass = {};
Object.assign(tsHass, this._hass);
tsHass.language = this.browser_language;

timeItem = html`<hui-timestamp-display
style="display:block; ${compact_mode ? "float:right" : "clear:both;"}"
.hass="${tsHass}"
.hass="${this._hass}"
.ts="${new Date(n.timestamp)}"
.format="${n.format}"
title="${new Date(n.timestamp)}"
Expand Down Expand Up @@ -1094,6 +1093,7 @@ class HomeFeedCard extends LitElement {

set hass(hass) {
this._hass = hass;
this.hass_version = hass.config.version;
this._language = Object.keys(hass.resources)[0];
if(this.moment){
this.refreshEntityHistory().then(() => {});
Expand Down

0 comments on commit 1df2a55

Please sign in to comment.