Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add value_multiplier and extend value_factor #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/buildConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default (config) => {
state_map: [],
cache: true,
value_factor: 0,
value_multipler: 1,
tap_action: {
action: 'more-info',
},
Expand Down
16 changes: 15 additions & 1 deletion src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ import {
} from './const';

export default class Graph {
constructor(width, height, margin, hours = 24, points = 1, aggregateFuncName = 'avg', groupBy = 'interval', smoothing = true, logarithmic = false) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need to pass valueFactor & valueMultiplier here, they aren't used in the class.

constructor({
width,
height,
margin,
hours = 24,
points = 1,
aggregateFuncName = 'avg',
valueFactor = 0,
valueMultiplier = 1,
groupBy = 'interval',
smoothing = true,
logarithmic = false
}) {
const aggregateFuncMap = {
avg: this._average,
max: this._maximum,
Expand All @@ -26,6 +38,8 @@ export default class Graph {
this.points = points;
this.hours = hours;
this.aggregateFuncName = aggregateFuncName;
this.valueFactor = valueFactor;
this.valueMultiplier = valueMultiplier;
this._calcPoint = aggregateFuncMap[aggregateFuncName] || this._average;
this._smoothing = smoothing;
this._logarithmic = logarithmic;
Expand Down
33 changes: 18 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,26 @@ class MiniGraphCard extends LitElement {
const entitiesChanged = !compareArray(this.config.entities || [], config.entities);

this.config = buildConfig(config, this.config);

if (!this.Graph || entitiesChanged) {
if (this._hass) this.hass = this._hass;
this.Graph = this.config.entities.map(
entity => new Graph(
500,
this.config.height,
[this.config.show.fill ? 0 : this.config.line_width, this.config.line_width],
this.config.hours_to_show,
this.config.points_per_hour,
entity.aggregate_func || this.config.aggregate_func,
this.config.group_by,
getFirstDefinedItem(
entity => new Graph({
width: 500,
height: this.config.height,
margin: [this.config.show.fill ? 0 : this.config.line_width, this.config.line_width],
hours: this.config.hours_to_show,
points: this.config.points_per_hour,
aggregateFuncName: entity.aggregate_func || this.config.aggregate_func,
valueFactor: entity.valueFactor || this.config.valueFactor,
valueMultiplier: entity.valueMultiplier || this.config.valueMultiplier,
groupBy: this.config.group_by,
smoothing: getFirstDefinedItem(
entity.smoothing,
this.config.smoothing,
!entity.entity.startsWith('binary_sensor.'), // turn off for binary sensor by default
),
this.config.logarithmic,
),
logarithmic: this.config.logarithmic,
}),
);
}
}
Expand Down Expand Up @@ -666,12 +667,14 @@ class MiniGraphCard extends LitElement {
}
const dec = this.config.decimals;
const value_factor = 10 ** this.config.value_factor;
const value_multipler = this.config.value_multipler || 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to add this as an option on the entity config level we need to check the entity specific config here as well.


if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state))
return Math.round(state * value_factor * 100) / 100;
if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state)) {
return (Math.round(state * value_factor * 100) / 100) * value_multipler;
}

const x = 10 ** dec;
return (Math.round(state * value_factor * x) / x).toFixed(dec);
return (Math.round(state * value_factor * x) / x).toFixed(dec) * value_multipler;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decimals option will not play nicely together with this change as we first round the value and then multiply with the multiplier value. Should apply the multiplier first and then do the rounding.

}

updateOnInterval() {
Expand Down