Skip to content

Commit

Permalink
Merge pull request #45 from molikk/fix-power-entity-precision-display…
Browse files Browse the repository at this point in the history
…-in-loads-when-entity-is-not-power

fix power entity precision display in loads when entity is not power
  • Loading branch information
molikk authored Jul 24, 2024
2 parents 636b33e + 78d0e5a commit 796ec48
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/mlk-power-flow-card.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mlk-power-flow-card",
"version": "0.10.0",
"version": "0.10.1",
"description": "A customizable Home Assistant card to emulate the Sunsynk System flow that's displayed on the Inverter screen.",
"main": "mlk-power-flow-card.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ export class Utils {
}
}

static convertValueNew(value: string | number, unit: UnitOfEnergyOrPower | string = '', decimal: number = 2) {
static convertValueNew(value: string | number, unit: UnitOfEnergyOrPower | string = '', decimal: number = 2):string {
decimal = isNaN(decimal) ? 2 : decimal;
const numberValue = Number(value);
if (isNaN(numberValue)) return Number(0).toFixed(decimal);

const rules = unitOfEnergyConversionRules[unit];
if (!rules) return `${Math.round(numberValue).toFixed(decimal)} ${unit}`;
if (!rules) {
return `${this.toNum(numberValue, decimal)} ${unit}`;
}

if (unit === UnitOfPower.WATT && Math.abs(numberValue) < 1000) {
return `${Math.round(numberValue)} ${unit}`;
Expand Down
4 changes: 2 additions & 2 deletions src/inverters/dto/custom-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export function convertToCustomEntity(entity: any): CustomEntity {
},
toPowerString: (scale?: boolean, decimals?: number, invert?: boolean) =>
scale ?
Utils.convertValueNew(entity?.state, entity?.attributes?.unit_of_measurement, decimals || 0) :
`${Utils.toNum(entity?.state, 0, invert).toFixed(decimals)} ${entity?.attributes?.unit_of_measurement || ''}`,
Utils.convertValueNew(entity?.state, entity?.attributes?.unit_of_measurement, decimals) :
`${Utils.toNum(entity?.state, decimals, invert).toFixed(decimals)} ${entity?.attributes?.unit_of_measurement || ''}`,
toString: () => entity?.state?.toString() || '',
toOnOff: () => ['on', 'On', 'ON', 1, true].includes(entity?.state) ? 'on' : 'off' || 'off',
getUOM: () => entity?.attributes?.unit_of_measurement || '',
Expand Down

0 comments on commit 796ec48

Please sign in to comment.