Skip to content

Commit

Permalink
fix: Rename value multipler to scale factor and fix config priority i…
Browse files Browse the repository at this point in the history
…n compute state
  • Loading branch information
X-Ryl669 committed Dec 7, 2022
1 parent 0d44609 commit d0fde5e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ properties of the Entity object detailed in the following table (as per `sensor.
| y_axis | string | | If 'secondary', displays using the secondary y-axis on the right.
| fixed_value | boolean | | Set to true to graph the entity's current state as a fixed value instead of graphing its state history.
| smoothing | boolean | | Override for a flag indicating whether to make graph line smooth.
| value_multipler | number | 1 | Set a multiplier to use on the graph's value
| scale_factor | number | 1 | Set a scale factor to use on the graph's value
| value_factor | number | 0 | Scale value by order of magnitude (e.g. convert Watts to kilo Watts), use negative value to scale down.

```yaml
Expand All @@ -149,7 +149,7 @@ entities:
- entity: sensor.pressure
name: Pressure
show_state: true
value_multipler: -2.1
scale_factor: -2.1
- sensor.humidity
```

Expand Down
6 changes: 3 additions & 3 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Graph {
hours = 24,
points = 1,
aggregateFuncName = 'avg',
valueMultipler = 1,
scaleFactor = 1,
groupBy = 'interval',
smoothing = true,
logarithmic = false,
Expand All @@ -39,7 +39,7 @@ export default class Graph {
this.points = points;
this.hours = hours;
this.aggregateFuncName = aggregateFuncName;
this._valueMultipler = valueMultipler;
this._scaleFactor = scaleFactor;
this._calcPoint = aggregateFuncMap[aggregateFuncName] || this._average;
this._smoothing = smoothing;
this._logarithmic = logarithmic;
Expand Down Expand Up @@ -111,7 +111,7 @@ export default class Graph {
}

_scale(value) {
return this._valueMultipler * value;
return this._scaleFactor * value;
}

_calcY(coords) {
Expand Down
24 changes: 12 additions & 12 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MiniGraphCard extends LitElement {
!entity.entity.startsWith('binary_sensor.'), // turn off for binary sensor by default
),
logarithmic: this.config.logarithmic,
valueMultipler: (entity.value_multipler ? entity.value_multipler : 1)
scaleFactor: (entity.scale_factor ? entity.scale_factor : 1)
* (entity.value_factor ? 10 ** entity.value_factor : valueFactor),
}),
);
Expand Down Expand Up @@ -295,7 +295,7 @@ class MiniGraphCard extends LitElement {
style=${entityConfig.state_adaptive_color ? `color: ${this.computeColor(state, id)};` : ''}>
${entityConfig.show_indicator ? this.renderIndicator(state, id) : ''}
<span class="state__value ellipsis">
${this.computeState(isPrimary && tooltipValue || state, entityConfig.value_multipler)}
${this.computeState(isPrimary && tooltipValue || state, entityConfig.scale_factor)}
</span>
<span class="state__uom ellipsis">
${this.computeUom(isPrimary && entity || id)}
Expand Down Expand Up @@ -580,7 +580,7 @@ class MiniGraphCard extends LitElement {
<div class="info__item">
<span class="info__item__type">${entry.type}</span>
<span class="info__item__value">
${this.computeState(entry.state, entry.value_multipler)} ${this.computeUom(0)}
${this.computeState(entry.state, entry.scale_factor)} ${this.computeUom(0)}
</span>
<span class="info__item__time">
${entry.type !== 'avg' ? getTime(new Date(entry.last_changed), this.config.format, this._hass.language) : ''}
Expand Down Expand Up @@ -682,7 +682,7 @@ class MiniGraphCard extends LitElement {
);
}

computeState(inState, default_multiplier) {
computeState(inState, default_scale_factor) {
if (this.config.state_map.length > 0) {
const stateMap = Number.isInteger(inState)
? this.config.state_map[inState]
Expand All @@ -703,16 +703,16 @@ class MiniGraphCard extends LitElement {
}
const dec = this.config.decimals;
const value_factor = 10 ** this.config.value_factor;
const value_multipler = this.config.value_multipler || default_multiplier || 1;
const scale_factor = default_scale_factor || this.config.scale_factor || 1;

if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state)) {
return this.numberFormat((Math.round(state * value_factor * value_multipler * 100) / 100),
return this.numberFormat((Math.round(state * value_factor * scale_factor * 100) / 100),
this._hass.language);
}

const x = 10 ** dec;
return this.numberFormat(
(Math.round(state * value_factor * x * value_multipler) / x).toFixed(dec),
(Math.round(state * value_factor * x * scale_factor) / x).toFixed(dec),
this._hass.language, dec,
);
}
Expand Down Expand Up @@ -945,7 +945,7 @@ class MiniGraphCard extends LitElement {
if (stateHistory.length === 0) return;

if (this.entity[0] && entity.entity_id === this.entity[0].entity_id) {
this.updateExtrema(stateHistory, this.config.entities[index].value_multipler || 1);
this.updateExtrema(stateHistory, this.config.entities[index].scale_factor || 1);
}

if (this.config.entities[index].fixed_value === true) {
Expand All @@ -967,22 +967,22 @@ class MiniGraphCard extends LitElement {
return this._hass.callApi('GET', url);
}

updateExtrema(history, value_multipler) {
updateExtrema(history, scale_factor) {
const { extrema, average } = this.config.show;
this.abs = [
...(extrema ? [{
type: 'min',
value_multipler,
scale_factor,
...getMin(history, 'state'),
}] : []),
...(average ? [{
type: 'avg',
value_multipler,
scale_factor,
state: getAvg(history, 'state'),
}] : []),
...(extrema ? [{
type: 'max',
value_multipler,
scale_factor,
...getMax(history, 'state'),
}] : []),
];
Expand Down

0 comments on commit d0fde5e

Please sign in to comment.