Skip to content

Commit

Permalink
Energyflow: kW/W switch for non-changing values (evcc-io#9206)
Browse files Browse the repository at this point in the history
* Energyflow: fix kW/W switch for non-changing values

* Energyflow: fix kW/W switch for non-changing values

* fix test
  • Loading branch information
naltatis authored Aug 1, 2023
1 parent eb60a4a commit e6b88f7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
8 changes: 6 additions & 2 deletions assets/js/components/AnimatedNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export default {
unmounted() {
this.instance = null;
},
methods: {
forceUpdate() {
this.instance?.reset();
this.instance?.update(this.to);
},
},
};
</script>
<style lang="less" scoped></style>
2 changes: 1 addition & 1 deletion assets/js/components/Energyflow/Energyflow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export default {
return Math.max(0, this.gridPower * -1);
},
powerInKw: function () {
return this.gridImport + this.selfConsumption + this.pvExport > 1000;
return Math.max(this.gridImport, this.selfConsumption, this.pvExport) >= 1000;
},
inPower: function () {
return this.gridImport + this.pvProduction + this.batteryDischarge;
Expand Down
8 changes: 7 additions & 1 deletion assets/js/components/Energyflow/EnergyflowEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<AnimatedNumber v-if="!isNaN(details)" :to="details" :format="detailsFmt" />
</div>
<div ref="power" class="power" data-bs-toggle="tooltip" @click="powerClicked">
<AnimatedNumber :to="power" :format="kw" />
<AnimatedNumber ref="powerNumber" :to="power" :format="kw" />
</div>
</span>
</div>
Expand Down Expand Up @@ -79,6 +79,12 @@ export default {
this.updateDetailsTooltip();
}
},
powerInKw(newVal, oldVal) {
// force update if unit changes but not the value
if (newVal !== oldVal) {
this.$refs.powerNumber.forceUpdate();
}
},
},
mounted: function () {
this.updatePowerTooltip();
Expand Down
20 changes: 20 additions & 0 deletions assets/js/mixins/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ const fmt = mount({
mixins: [formatter],
}).componentVM;

describe("fmtkW", () => {
test("should format kW and W", () => {
expect(fmt.fmtKw(0, true)).eq("0,0 kW");
expect(fmt.fmtKw(1200, true)).eq("1,2 kW");
expect(fmt.fmtKw(0, false)).eq("0 W");
expect(fmt.fmtKw(1200, false)).eq("1.200 W");
});
test("should format without unit", () => {
expect(fmt.fmtKw(0, true, false)).eq("0,0");
expect(fmt.fmtKw(1200, true, false)).eq("1,2");
expect(fmt.fmtKw(0, false, false)).eq("0");
expect(fmt.fmtKw(1200, false, false)).eq("1.200");
});
test("should format a given number of digits", () => {
expect(fmt.fmtKw(12345, true, true, 0)).eq("12 kW");
expect(fmt.fmtKw(12345, true, true, 1)).eq("12,3 kW");
expect(fmt.fmtKw(12345, true, true, 2)).eq("12,35 kW");
});
});

describe("fmtKWh", () => {
test("should format with units", () => {
expect(fmt.fmtKWh(1200)).eq("1,2 kWh");
Expand Down
2 changes: 1 addition & 1 deletion tests/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.describe("main screen", async () => {
test("visualization", async ({ page }) => {
const locator = page.getByTestId("visualization");
await expect(locator).toBeVisible();
await expect(locator).toContainText("1,000 W");
await expect(locator).toContainText("1.0 kW");
});

test("one loadpoint", async ({ page }) => {
Expand Down

0 comments on commit e6b88f7

Please sign in to comment.