Skip to content

Commit

Permalink
Merge pull request #324 from Pho3niX90/issue/322
Browse files Browse the repository at this point in the history
Refine custom entity validations and improve unit conversions
  • Loading branch information
slipx06 authored Mar 24, 2024
2 parents 00b6e16 + 7cda98c commit fac12f0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/sunsynk-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": "sunsynk-power-flow-card",
"version": "4.24.5",
"version": "4.24.6",
"description": "A customizable Home Assistant card to emulate the Sunsynk System flow that's displayed on the Inverter screen.",
"main": "sunsynk-power-flow-card.js",
"scripts": {
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ export class Utils {
return `${Math.round(numberValue)} ${unit}`;
};

if (unit === UnitOfPower.KILO_WATT && Math.abs(numberValue) < 1) {
return `${Math.round(numberValue * 1000)} W`;
};

if (unit === UnitOfPower.MEGA_WATT && Math.abs(numberValue) < 1) {
return `${(numberValue * 1000).toFixed(decimal)} kW`;
};

for (const rule of rules) {
if (Math.abs(numberValue) >= rule.threshold) {
const convertedValue = (numberValue / rule.divisor).toFixed(rule.decimal || decimal);
return `${convertedValue} ${rule.targetUnit}`;
}
}
};

return `${numberValue.toFixed(decimal)} ${unit}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ export class SunsynkPowerFlowCard extends LitElement {
* Status can be returned as decimals "3.0", so this is just to change it to an int
*/
if (inverterModel == InverterModel.Solis) {
inverterState = !stateInverterStatus.isNaN() ? stateInverterStatus.toNum(0) : stateInverterStatus.toString();
inverterState = !stateInverterStatus.isNaN() ? stateInverterStatus.toNum(0).toString() : stateInverterStatus.toString();
}

let typeStatusGroups = inverterSettings.statusGroups;
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 @@ -53,8 +53,8 @@ export function convertToCustomEntity(entity: any): CustomEntity {
...entity,
toNum: (decimals?: number, invert?: boolean) => Utils.toNum(entity?.state, decimals, invert),
isValid: () => entity?.state !== null && entity.state !== undefined && entity.state !== 'unknown' || false,
notEmpty: () => entity?.state !== '' || false,
isNaN: () => Number.isNaN(entity?.state) || true,
notEmpty: () => (entity?.state !== '' && entity?.state !== null && entity?.state !== 'unknown' && entity.state !== undefined) || false,
isNaN: () => entity?.state === null || Number.isNaN(entity?.state),
toPower: (invert?: boolean) => {
const unit = (entity.attributes?.unit_of_measurement || '').toLowerCase();
if (unit === 'kw') {
Expand Down

0 comments on commit fac12f0

Please sign in to comment.