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

Feature/custom_entity #305

Merged
merged 17 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 4 additions & 4 deletions 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.22.2",
"version": "4.23.0",
"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
263 changes: 108 additions & 155 deletions src/index.ts

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/inverters/dto/custom-entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {HassEntity} from 'home-assistant-js-websocket/dist/types';
import {Utils} from '../../helpers/utils';

/**
* CustomEntity interface represents a custom entity in Home Assistant.
slipx06 marked this conversation as resolved.
Show resolved Hide resolved
* - this entity aids in reducing common boiler plate code. the end goal is that we can just use the state object instead of multiple vars
*/
export interface CustomEntity extends HassEntity {
state: any,

/**
* Extension of Utils.toNum, returns the state in a number
* @param decimals
* @param invert
*/
toNum(decimals?: number, invert?: boolean): number;

/**
* Checks that the state is not null or undefined
*/
isValid(): boolean;

/**
* Checks that the state is not equal to ''
*/
notEmpty(): boolean;

isNaN(): boolean;

/**
* Auto converts the state to watts/kilowatts
* @param invert
*/
toPower(invert?: boolean): number;
}

// Function to convert HassEntity to CustomEntity
export function convertToCustomEntity(entity: any): CustomEntity {
return {
...entity,
toNum: (decimals?: number, invert?: boolean) => Utils.toNum(entity?.state, decimals, invert),
isValid: () => entity?.state !== null && entity.state !== undefined || false,
notEmpty: () => entity?.state !== '' || false,
isNaN: () => Number.isNaN(entity?.state) || true,
toPower: (invert?: boolean) => (entity.attributes?.unit_of_measurement || '').toLowerCase() === 'kw'
? Utils.toNum(((entity?.state || '0') * 1000), 0, invert)
: Utils.toNum((entity?.state || '0'), 0, invert) || 0
}
}
18 changes: 18 additions & 0 deletions src/inverters/dto/inverter-settings.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export class InverterSettingsDto {

constructor() {
}

getBatteryCapacity(battery_power: number, grid_status: string, shutdown: number, inverter_prog, state_battery_soc) {
let battery_capacity = 0;
if (battery_power > 0) {
if (grid_status === 'off' || grid_status === '0' || grid_status.toLowerCase() === 'off-grid' || !inverter_prog.show || parseInt(state_battery_soc.state) <= inverter_prog.capacity) {
battery_capacity = shutdown;
} else {
battery_capacity = inverter_prog.capacity;
}
} else if (battery_power < 0) {
if (grid_status === 'off' || grid_status === '0' || grid_status.toLowerCase() === 'off-grid' || !inverter_prog.show || parseInt(state_battery_soc.state) >= inverter_prog.capacity) {
battery_capacity = 100;
} else if (parseInt(state_battery_soc.state) < inverter_prog.capacity) {
battery_capacity = inverter_prog.capacity;
}
}
return battery_capacity;
}
}

export type InverterStatus = {
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ export interface sunsynkPowerFlowCardConfig extends LovelaceCardConfig {
three_phase: boolean;
}
battery: {
energy: number;
shutdown_soc: number;
shutdown_soc_offgrid: number;
energy: any;
Copy link
Owner

Choose a reason for hiding this comment

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

Please confirm that I am corrct to change this to any where you can provide a number or sensor

shutdown_soc: any;
shutdown_soc_offgrid: any;
hide_soc: boolean;
invert_power: boolean;
colour: string;
charge_colour: string;
show_daily: boolean;
animation_speed: number;
max_power: number;
max_power: any;
full_capacity: number;
empty_capacity: number;
show_absolute: boolean;
Expand Down
Loading