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

Conversation

Pho3niX90
Copy link
Contributor

The new CustomEntity interface allows for streamlined handling of state-related tasks and boilerplate code reduction. Additionally, the full battery capacity computation logic is now encapsulated within the getBatteryCapacity method. This promotes better organization and clarity throughout the codebase.

I added the co-authored tag. Although I "think" I got all the logic, I might not have understood the intent at all of the calculations, so just need another pair of eyes.

Pho3niX90 and others added 4 commits March 12, 2024 19:51
The new CustomEntity interface allows for streamlined handling of state-related tasks and boilerplate code reduction. Additionally, the full battery capacity computation logic is now encapsulated within the `getBatteryCapacity` method. This promotes better organization and clarity throughout the codebase, particularly in `sunsynk-power-flow-card.js`.
The updated version of sunsynk-power-flow-card.js now incorporates the CustomEntity interface for improved state management and reduced boilerplate code. A part of this change is the enclosed battery capacity computation logic within the `getBatteryCapacity` method, leading to cleaner and more maintainable code.

Co-authored-by: Pho3niX90 <[email protected]>
Co-authored-by: slipx06 <[email protected]>
The CustomEntity methods were refactored and simplified, reducing the overall complexity and remove unnecessary namespace. This update improves state management and decreases boilerplate code, providing for a cleaner, more maintainable code base. The changes include enclosing battery capacity computation inside the `getBatteryCapacity` method and implementing convertToCustomEntity as a direct export function.

Co-authored-by: Pho3niX90 <[email protected]>
Co-authored-by: slipx06 <[email protected]>
Co-authored-by: Pho3niX90 <[email protected]>
Co-authored-by: slipx06 <[email protected]>
Co-authored-by: Pho3niX90 <[email protected]>
Co-authored-by: slipx06 <[email protected]>
src/index.ts Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
Updated the code in src/index.ts by removing the default states for various entities. Default states for 'aux_power_166' and several 'pvX_voltage/current' entities were removed, since they were unnecessary. Additionally, removed the unused 'dynamicConfig' option from sunsynkPowerFlowCardConfig in src/types.ts.

Co-authored-by: Pho3niX90 <[email protected]>
Co-authored-by: slipx06 <[email protected]>
src/index.ts Outdated Show resolved Hide resolved
@slipx06
Copy link
Owner

slipx06 commented Mar 13, 2024

I'm getting this error

image

Edit found the problem

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

@Pho3niX90
Copy link
Contributor Author

@slipx06 should be fixed now, was because toNum() was called on the state, and since state was of type any, it didn't throw type safety errors.

I have fixed the state, by changing it to back to string (HassEntity type).

@slipx06
Copy link
Owner

slipx06 commented Mar 13, 2024

The last update broke battery energy. Its a problem with state_battery_rated_capacity

battery_energy = Utils.toNum(battery_voltage * state_battery_rated_capacity, 0)
const battenergy = this.getEntity('battery.energy', {state: config.battery.energy.toString() ?? ''});
let battery_energy = battenergy.toNum(0);
if (battery_voltage && state_battery_rated_capacity.notEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

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

check if state is empty

@slipx06
Copy link
Owner

slipx06 commented Mar 13, 2024

I think this is good to merge now.

Is there a better way to handle this error. Happens when you set battery_energy: null

image

@Pho3niX90
Copy link
Contributor Author

That should fix it. The optional chaining operator fixes these issues ?, it will then only call toString() if the object is not null.

@slipx06 slipx06 merged commit 5b6820e into slipx06:master Mar 13, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants