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 4 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
182 changes: 66 additions & 116 deletions src/index.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/inverters/brands/huawei.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {InverterSettingsDto, InverterStatus} from '../dto/inverter-settings.dto';
import {InverterModel} from '../../types';
import {localize} from '../../localize/localize';
import {Utils} from '../../helpers/utils';
Pho3niX90 marked this conversation as resolved.
Show resolved Hide resolved

export class Huawei extends InverterSettingsDto {
brand = InverterModel.Huawei;
Expand Down
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
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum AutarkyType {

export interface sunsynkPowerFlowCardConfig extends LovelaceCardConfig {
type: string;
dynamicConfig: boolean;
slipx06 marked this conversation as resolved.
Show resolved Hide resolved
cardstyle: CardStyle;
panel_mode?: boolean;
large_font?: boolean;
Expand All @@ -58,8 +59,8 @@ export interface sunsynkPowerFlowCardConfig extends LovelaceCardConfig {
}
battery: {
energy: number;
shutdown_soc: number;
shutdown_soc_offgrid: number;
shutdown_soc: any;
shutdown_soc_offgrid: any;
hide_soc: boolean;
invert_power: boolean;
colour: string;
Expand Down
Loading