@@ -611,22 +624,24 @@ class MiniGraphCard extends LitElement {
renderLabelsSecondary() {
if (!this.config.show.labels_secondary || this.secondaryYaxisSeries.length === 0) return;
+ // index "-1" is passed into computeState() for a secondary axis
return html`
`;
}
renderInfo() {
+ // index "0" is passed into computeState() since "info" is shown for the 1st entity
return this.abs.length > 0 ? html`
${this.abs.map(entry => html`
${entry.type}
- ${this.computeState(entry.state)} ${this.computeUom(0)}
+ ${this.computeState(entry.state, 0)} ${this.computeUom(0)}
${entry.type !== 'avg' ? getTime(new Date(entry.last_changed), this.config.format, this._hass.language) : ''}
@@ -723,7 +738,15 @@ class MiniGraphCard extends LitElement {
);
}
- computeState(inState) {
+ /**
+ * Returns a string value for a state/attrubute:
+ * localized, following locale settings,
+ * accounting possible individual accuracy settings & possible "decimals" options
+ * @returns {string} value of a state/attribute
+ * @param {number|string} inState Value of a state/attribute ("unformatted")
+ * @param {number} index Index of an entity in config.entities
+ */
+ computeState(inState, index) {
if (this.config.state_map.length > 0) {
const stateMap = Number.isInteger(inState)
? this.config.state_map[inState]
@@ -737,22 +760,89 @@ class MiniGraphCard extends LitElement {
}
let state;
- if (typeof inState === 'string') {
+ if (isUnavailableState(inState)) {
+ // as is
+ state = inState;
+ } else if (typeof inState === 'string') {
+ // attempt to fix an unexpected number format
state = parseFloat(inState.replace(/,/g, '.'));
} else {
+ // as is presented as a number
state = Number(inState);
}
- const dec = this.config.decimals;
const value_factor = 10 ** this.config.value_factor;
+ // safely process with a value_factor
+ state = Number.isNaN(Number(state)) ? state : state * value_factor;
+
+ let dec;
+ // attempting to get "decimals" settings
+ if (index === undefined) {
+ // for a primary Y-axis
+ dec = this.config.decimals_primary_labels !== undefined
+ ? this.config.decimals_primary_labels
+ : this.config.decimals;
+ } else if (index === -1) {
+ // for a secondary Y-axis
+ dec = this.config.decimals_secondary_labels !== undefined
+ ? this.config.decimals_secondary_labels
+ : this.config.decimals;
+ } else {
+ // for a state or attribute value
+ dec = this.config.entities[index].decimals !== undefined
+ ? this.config.entities[index].decimals
+ : this.config.decimals;
+ }
- if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state)) {
- return this.numberFormat(Math.round(state * value_factor * 100) / 100, this._hass.language);
+ let value;
+
+ if (dec === undefined || Number.isNaN(Number(dec)) || Number.isNaN(Number(state))) {
+ // no valid "decimals" settings defined, use a default accuracy
+ if (index >= 0) {
+ // formatting a state or attribute
+ const entityId = this.config.entities[index].entity;
+ const { attribute } = this.config.entities[index];
+ const stateObj = this._hass.states[entityId];
+ if (attribute && !this.isObjectAttr(attribute)) {
+ // formatting not-object attribute
+ const attrParts = this._hass.formatEntityAttributeValueToParts(
+ stateObj,
+ attribute,
+ state,
+ );
+ const partValue = attrParts.find(part => part.type === 'value');
+ value = partValue && partValue.value;
+ return value;
+ } else if (attribute && this.isObjectAttr(attribute)) {
+ // formatting object attribute - similar to Y-axis labels
+ return formatNumber(
+ state,
+ this._hass.locale,
+ );
+ } else {
+ // formatting state
+ const stateParts = this._hass.formatEntityStateToParts(
+ stateObj,
+ state,
+ );
+ const partValue = stateParts.find(part => part.type === 'value');
+ value = partValue && partValue.value;
+ return value;
+ }
+ } else {
+ // formatting Y-axis (primary, secondary) labels
+ // use a default hard-coded accuracy
+ return formatNumber(
+ state,
+ this._hass.locale,
+ );
+ }
}
- const x = 10 ** dec;
- return this.numberFormat(
- (Math.round(state * value_factor * x) / x).toFixed(dec),
- this._hass.language, dec,
+ // use an acuracy defined by "dec" variable
+ return formatNumber(
+ state,
+ this._hass.locale,
+ { minimumFractionDigits: dec, maximumFractionDigits: dec },
);
}
diff --git a/src/utils.js b/src/utils.js
index fe64cf7c..dd80e9b6 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,4 +1,3 @@
-/* eslint-disable no-bitwise */
import { compress as lzStringCompress, decompress as lzStringDecompress } from '@kalkih/lz-string';
const getMin = (arr, val) => arr.reduce((min, p) => (