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

Don't set hass to undefined in lovelace cards. #21189

Merged
merged 5 commits into from
Jun 27, 2024

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Jun 27, 2024

Proposed change

Multiple fix and improvements :

  • Don't set hass if undefined (because it was the case before hui-card)
  • Remove the usage of setter/getter for config

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

  • New Features

    • Enhanced HuiCard class with improved configuration handling and error management.
    • Introduced load() method across various card components for better initialization.
  • Improvements

    • Updated HuiStackCard class to ensure controlled property assignments and added load() method calls.
    • Enhanced HuiCardLayoutEditor, HuiSection, and HUIView to ensure elements are properly loaded before accessing properties.

@piitaya piitaya added this to the 2024.7 milestone Jun 27, 2024
@piitaya piitaya marked this pull request as ready for review June 27, 2024 14:22
Copy link
Contributor

coderabbitai bot commented Jun 27, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The recent changes improve the configuration handling, loading, and property management across various Lovelace card components within the codebase. Key adjustments include added load() methods for better initialization, refactoring of property declarations, and enhanced error handling. These changes aim to boost performance, clarity, and maintainability of the Lovelace UI components.

Changes

File / Group Change Summary
src/panels/lovelace/cards/hui-card.ts Refactored HuiCard class for better configuration handling, property updates, and error management.
src/panels/lovelace/cards/hui-conditional-card.ts Added load() method to HuiConditionalCard class to enhance element initialization.
src/panels/lovelace/cards/hui-entity-filter-card.ts Introduced load() method in HuiEntityFilterCard class for improved element setup.
src/panels/lovelace/cards/hui-stack-card.ts Updated preview property handling and added element load calls within HuiStackCard class.
.../card-editor/hui-card-layout-editor.ts Modified HuiCardLayoutEditor to call load() on card elements before accessing layout options.
src/panels/lovelace/sections/hui-section.ts Enhanced HuiSection with load() method call to improve element loading mechanics.
src/panels/lovelace/views/hui-view.ts Added load() call to renderCard() function in HUIView for better element rendering.

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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @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.

@piitaya
Copy link
Member Author

piitaya commented Jun 27, 2024

@coderabbitai pause

@piitaya
Copy link
Member Author

piitaya commented Jun 27, 2024

@coderabbitai configuration

Copy link
Contributor

coderabbitai bot commented Jun 27, 2024

CodeRabbit configuration
language: en-US
tone_instructions: ''
early_access: false
enable_free_tier: true
reviews:
  profile: assertive
  request_changes_workflow: false
  high_level_summary: true
  high_level_summary_placeholder: '@coderabbitai summary'
  auto_title_placeholder: '@coderabbitai title'
  poem: false
  review_status: false
  collapse_walkthrough: true
  path_filters: []
  path_instructions:
    - path: '*/**(html|markdown|md)'
      instructions: >-
        - For instructional content in documentation, use a direct and
        authoritative tone. Avoid expressions of politeness such as 'may' or
        'please', and ensure the goal of the instruction is fronted.

        - Apply the Microsoft Style Guide to ensure documentation maintains
        clarity and conciseness.

        - In step-by-step instructions, front the location phrase in the
        instructional sentence.

        - In step-by-step instructions, front the 'goal' in the instructional
        sentence.

        - In step-by-step instructions, if in doubt what to front, front the
        'goal' before the location phrase in the instructional sentence.

        - do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'
    - path: '*/**(html|markdown|md)'
      instructions: |-
        - Use bold to mark UI strings.
        - If "" are used to mark UI strings, replace them by bold.
    - path: '*/**(html|markdown|md)'
      instructions: >-
        - Be brief in your replies and don't add fluff like "thank you for..."
        and "Please let me know if"
    - path: '*/**(html|markdown|md)'
      instructions: '- Use sentence-style capitalization also in headings.'
  abort_on_close: true
  auto_review:
    enabled: true
    auto_incremental_review: true
    ignore_title_keywords: []
    labels: []
    drafts: false
    base_branches:
      - master
      - main
      - rc
      - next
      - current
      - dev
  tools:
    shellcheck:
      enabled: true
    ruff:
      enabled: true
    markdownlint:
      enabled: true
    github-checks:
      enabled: true
      timeout_ms: 90000
    languagetool:
      enabled: true
      disabled_rules:
        - EN_UNPAIRED_BRACKETS
        - ENGLISH_WORD_REPEAT_BEGINNING_RULE
        - REP_WANT_TO_VB
      disabled_categories:
        - TYPOGRAPHY
      enabled_only: false
      level: default
      enabled_rules: []
      enabled_categories: []
    biome:
      enabled: true
    hadolint:
      enabled: true
    swiftlint:
      enabled: true
    phpstan:
      enabled: true
      level: '1'
    golangci-lint:
      enabled: true
    yamllint:
      enabled: true
    gitleaks:
      enabled: true
    checkov:
      enabled: true
    ast-grep:
      packages: []
      rule_dirs: []
      util_dirs: []
      essential_rules: true
chat:
  auto_reply: true
knowledge_base:
  learnings:
    scope: global
  issues:
    scope: global
  jira:
    project_keys: []
  linear:
    team_keys: []

@piitaya piitaya force-pushed the fix_hass_and_config_loading_hui_card branch from 5097df0 to f0fb94b Compare June 27, 2024 14:26
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: 2

Outside diff range comments (5)
src/panels/lovelace/cards/hui-card.ts (1)

Line range hint 131-162: Complexity and error handling in update method.

The update method has a high complexity and multiple points where errors can occur. Consider refactoring to simplify the logic and improve error handling.

- // Current implementation
+ // Suggested refactoring here
Tools
Biome

[error] 91-91: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 102-102: Forbidden non-null assertion.

(lint/style/noNonNullAssertion)


[error] 152-152: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)


[error] 131-131: Excessive complexity detected.

Please refactor this function to reduce its complexity score from 24 to the max allowed complexity 15.

(lint/complexity/noExcessiveCognitiveComplexity)

src/panels/lovelace/editor/card-editor/hui-card-layout-editor.ts (1)

Line range hint 185-210: Address performance concerns with the delete operator.

Using the delete operator can lead to performance issues. Consider using an undefined assignment instead to maintain performance while achieving the same functionality.

- delete newConfig.layout_options;
+ newConfig.layout_options = undefined;
src/panels/lovelace/sections/hui-section.ts (1)

Line range hint 172-172: Consider replacing non-null assertions with safer checks.

Using non-null assertions (!) can lead to runtime errors if the object is actually null. Consider using optional chaining (?.) or adding necessary checks to ensure the object exists before accessing its properties.

- this.hass!
+ this.hass?. // Repeat for all instances

Also applies to: 193-193, 194-194, 195-195, 196-196, 197-197, 198-198, 261-261, 263-263

src/panels/lovelace/views/hui-view.ts (2)

Line range hint 93-93: Avoid using any type for better type safety.

Using any disables TypeScript's static type checking, which can lead to errors that are hard to detect. Consider specifying more explicit types.

- processConfigEntities(config.badges as any);
+ processConfigEntities(config.badges as LovelaceBadgeConfig[]); // Adjust according to actual expected type

Also applies to: 174-174, 186-186, 215-215, 340-340, 345-345


Line range hint 118-118: Replace non-null assertions with optional chaining for safer code.

Non-null assertions might lead to unexpected null reference errors. Using optional chaining (?.) is a safer alternative that handles potential null or undefined values gracefully.

- this.hass!
+ this.hass?. // Repeat for all instances

Also applies to: 262-262, 284-284, 285-285, 286-286, 287-287, 288-288, 289-289, 290-290, 291-291, 300-300, 326-326, 328-328

@piitaya piitaya merged commit 7603fa3 into dev Jun 27, 2024
13 checks passed
@piitaya piitaya deleted the fix_hass_and_config_loading_hui_card branch June 27, 2024 14:49
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