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

Remove empty electricity grid state #20794

Merged
merged 3 commits into from
May 24, 2024

Conversation

silamon
Copy link
Contributor

@silamon silamon commented May 14, 2024

Proposed change

When there's no longer a grid consumption or return to grid defined in the electricity grid section, the whole electricity grid section should be removed.

This is how it used to save after removing all grid sources:

    "energy_sources": [
      {
        "type": "grid",
        "flow_from": [],
        "flow_to": [],
        "cost_adjustment_day": 0.0
      }
    ],
    "device_consumption": []

This is how it will now save:

    "energy_sources": [],
    "device_consumption": []

Given this situation, it now allows to say:

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

  • Improvements
    • Enhanced the energy grid settings by automatically removing empty grid sources before saving preferences, ensuring better data integrity.

@silamon silamon changed the title Remove empty grid state Remove empty electricity grid state May 14, 2024
Copy link
Contributor

coderabbitai bot commented May 23, 2024

Walkthrough

Walkthrough

The changes involve enhancing the EnergyGridSettings class in ha-energy-grid-settings.ts to ensure data integrity by removing empty grid sources before saving preferences. This improvement helps maintain cleaner and more accurate user settings.

Changes

File Path Change Summary
src/.../energy.ts Modified the EnergySource type declaration to be exported for external access.
src/.../energy/components/ha-energy-grid-settings.ts Updated EnergyGridSettings class to remove empty grid sources before saving preferences.
src/.../energy/components/ha-energy-grid-settings.ts Added _removeEmptySources method to handle the removal of empty grid sources from preferences.

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits Files that changed from the base of the PR and between 7f54536 and 1218c71.
Files selected for processing (2)
  • src/data/energy.ts (1 hunks)
  • src/panels/config/energy/components/ha-energy-grid-settings.ts (3 hunks)
Additional Context Used
Biome (29)
src/data/energy.ts (18)

415-415: Forbidden non-null assertion.


420-420: Forbidden non-null assertion.


425-426: This variable implicitly has the any type.


426-427: This variable implicitly has the any type.


427-428: This variable implicitly has the any type.


472-473: Forbidden non-null assertion.


483-484: Forbidden non-null assertion.


500-501: Forbidden non-null assertion.


509-510: Forbidden non-null assertion.


547-550: Prefer for...of instead of forEach.


585-592: Prefer for...of instead of forEach.


628-628: Unexpected any. Specify a different type.


629-629: Unexpected any. Specify a different type.


14-14: Some named imports are only used as types.


22-22: All these imports are only used as types.


23-23: Some named imports are only used as types.


24-30: Some named imports are only used as types.


null-null: Import statements could be sorted:

src/panels/config/energy/components/ha-energy-grid-settings.ts (11)

379-379: Forbidden non-null assertion.


411-411: Forbidden non-null assertion.


443-445: Forbidden non-null assertion.


470-472: Forbidden non-null assertion.


507-507: Unexpected any. Specify a different type.


10-10: Some named imports are only used as types.


15-19: Some named imports are only used as types.


20-31: Some named imports are only used as types.


32-35: Some named imports are only used as types.


42-42: All these imports are only used as types.


null-null: Import statements could be sorted:

Additional comments not posted (2)
src/panels/config/energy/components/ha-energy-grid-settings.ts (1)

453-454: Ensure consistency in method naming and operation.

The method _deleteFromSource and _deleteToSource both end with calling _savePreferences after cleaning preferences with _removeEmptySources. This pattern suggests that the cleaning operation could be integrated into _savePreferences itself to avoid repetition and enhance maintainability.

- const cleanedPreferences = this._removeEmptySources(preferences);
- await this._savePreferences(cleanedPreferences);
+ await this._savePreferences(preferences);

Then, modify _savePreferences to call _removeEmptySources internally.

Also applies to: 479-480

src/data/energy.ts (1)

176-176: Export EnergySource type to ensure it is accessible where needed.

This change is crucial for maintaining proper type safety and accessibility across different modules that might use this type.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (2)
src/panels/config/energy/components/ha-energy-grid-settings.ts (2)

Line range hint 378-378: Avoid using non-null assertions.

Non-null assertions are used in several places in this file. While they may be necessary due to the framework or specific coding patterns, it's generally safer to handle potential null or undefined values explicitly to prevent runtime errors.
Consider checking for null or undefined before usage or handling these cases gracefully.

Also applies to: 410-410, 442-444, 469-471


Line range hint 10-10: Optimize import statements.

Some imports are only used as types. These can be imported using import type to clarify that they are only used for type checking and not included in the JavaScript output, optimizing the bundle size.

- import { ConfigEntry, getConfigEntries } from "../../../../data/config_entries";
+ import type { ConfigEntry } from "../../../../data/config_entries";
+ import { getConfigEntries } from "../../../../data/config_entries";

Apply this change to other similar imports throughout the file.

Also applies to: 15-19, 20-30, 31-34, 41-41

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

@piitaya piitaya merged commit 5d044a0 into home-assistant:dev May 24, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants