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

Add more config option to heading entity element #22063

Merged
merged 1 commit into from
Sep 24, 2024
Merged

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Sep 23, 2024

Proposed change

Add show_state and show_icon option to heading entities

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

    • Introduced default configuration for the HuiHeadingEntity component, enhancing flexibility in display options.
    • Added new properties for controlling visibility of state and icons in heading entities.
  • Documentation

    • Updated translation files to reflect new configuration options and removed outdated keys.

These changes improve user customization and control over how heading entities are displayed in the interface.

@piitaya piitaya force-pushed the heading-entity-config branch 2 times, most recently from b261f3b to 49cf7ca Compare September 24, 2024 12:39
bramkragten
bramkragten previously approved these changes Sep 24, 2024
@piitaya piitaya force-pushed the heading-entity-config branch from 49cf7ca to 7e5df8e Compare September 24, 2024 15:15
@piitaya piitaya marked this pull request as ready for review September 24, 2024 15:15
@piitaya piitaya requested a review from bramkragten September 24, 2024 15:16
Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

Walkthrough

The changes involve updates to the HuiHeadingEntity component and its editor, enhancing configuration options for heading entities in the Lovelace UI. A default configuration is introduced, allowing for conditional rendering of icons and state displays based on new properties. The HeadingEntityConfig interface is modified to include state_content, show_state, and show_icon. Additionally, localization keys are updated to reflect these changes, improving the overall flexibility of the component's appearance and functionality.

Changes

Files Change Summary
src/panels/lovelace/cards/heading/hui-heading-entity.ts Introduced DEFAULT_CONFIG for default settings. Modified rendering logic to conditionally display icons and states based on show_icon and show_state.
src/panels/lovelace/cards/types.ts Updated HeadingEntityConfig interface: replaced content with state_content, added show_state and show_icon properties.
src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts Defined DEFAULT_CONFIG with default values. Updated entityConfigStruct to include new properties. Modified setConfig to merge configurations with defaults. Altered _schema to introduce an "appearance" section. Updated rendering logic in render method to account for displayed_elements. Modified _valueChanged to handle new properties and localization updates.
src/translations/en.json Removed content key, added appearance, state_content, displayed_elements, and displayed_elements_options keys to the entity_config object. Included options for icon and state in displayed_elements_options.

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>, please review it.
    -- 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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: 2

Outside diff range and nitpick comments (4)
src/panels/lovelace/cards/heading/hui-heading-entity.ts (3)

58-58: LGTM: Merging DEFAULT_CONFIG

The inclusion of DEFAULT_CONFIG using the spread operator is a good approach. It ensures default values are applied while allowing user configurations to override them.

For improved clarity, consider destructuring the config object:

-        ...DEFAULT_CONFIG,
-        tap_action: {
-          action: "none",
-        },
-        ...config,
+        ...DEFAULT_CONFIG,
+        tap_action: {
+          action: "none",
+        },
+        ...config,
+        show_icon: config.show_icon ?? DEFAULT_CONFIG.show_icon,
+        show_state: config.show_state ?? DEFAULT_CONFIG.show_state,

This explicitly shows which new properties are being added and allows for easier debugging if needed.


138-146: LGTM: Conditional rendering for show_icon

The implementation of the show_icon option is correct and aligns with the PR objectives.

For consistency with other parts of the code, consider using the nothing literal instead of an empty string for the false case:

-        ${config.show_icon
-          ? html`
-              <ha-state-icon
-                .hass=${this.hass}
-                .icon=${config.icon}
-                .stateObj=${stateObj}
-              ></ha-state-icon>
-            `
-          : nothing}
+        ${config.show_icon
+          ? html`
+              <ha-state-icon
+                .hass=${this.hass}
+                .icon=${config.icon}
+                .stateObj=${stateObj}
+              ></ha-state-icon>
+            `
+          : nothing}

This change makes the code more consistent with the show_state condition below.


Line range hint 1-200: Overall implementation looks good, some additional steps needed

The changes successfully implement the new show_icon and show_state configuration options for the heading entity element. The code is well-integrated with the existing structure and follows consistent patterns.

To complete this PR, please consider the following steps:

  1. Add unit tests to verify the new functionality.
  2. Update the documentation to reflect these new configuration options.
  3. Provide an example configuration snippet in the PR description to help with testing.
  4. Confirm that you've tested these changes locally and update the PR checklist accordingly.

To ensure maintainability and ease of testing, consider extracting the icon and state rendering logic into separate methods. This would make the render method cleaner and allow for easier unit testing of each part independently.

src/panels/lovelace/cards/types.ts (1)

508-511: Update documentation and provide migration guide

With the introduction of new properties and the renaming of content to state_content, it's crucial to update the documentation and provide a migration guide for users.

Please ensure the following:

  1. Update the Home Assistant documentation to reflect these new configuration options.
  2. Provide a migration guide for users to update their existing configurations.
  3. Update any relevant code examples or templates that use the HeadingEntityConfig.

Consider opening a separate PR for documentation updates or linking to a documentation PR in this PR's description.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c30e4a6 and 7e5df8e.

Files selected for processing (4)
  • src/panels/lovelace/cards/heading/hui-heading-entity.ts (3 hunks)
  • src/panels/lovelace/cards/types.ts (1 hunks)
  • src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts (6 hunks)
  • src/translations/en.json (1 hunks)
Additional context used
Biome
src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts

[error] 199-199: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (9)
src/panels/lovelace/cards/heading/hui-heading-entity.ts (2)

27-27: LGTM: Import of DEFAULT_CONFIG

The addition of DEFAULT_CONFIG import is consistent with the PR objectives and follows good practices by providing default configurations.


147-155: LGTM: Conditional rendering for show_state

The implementation of the show_state option is correct and aligns with the PR objectives. The use of config.state_content is consistent with the existing code, and the conditional rendering is properly implemented.

src/panels/lovelace/cards/types.ts (1)

510-511: LGTM: New configuration options added

The addition of show_state and show_icon properties aligns with the PR objectives and provides users with more flexibility in configuring heading entities.

src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts (6)

31-34: Definition of DEFAULT_CONFIG enhances default handling

The addition of DEFAULT_CONFIG ensures that default values for show_state and show_icon are consistently applied, enhancing code maintainability and readability.


63-66: Merging DEFAULT_CONFIG with user config applies defaults correctly

The updated setConfig method correctly merges DEFAULT_CONFIG with the provided configuration, ensuring that default values are applied when they are not specified by the user.


77-115: New appearance schema improves user customization options

The addition of the "appearance" section in the schema allows users to customize the display of the heading entity, including the icon, displayed elements, and state content. This enhances the flexibility and user experience of the configuration editor.


136-141: Efficient computation of displayed_elements

The _displayedElements function correctly constructs the displayed_elements array based on the show_state and show_icon properties. Using memoizeOne improves performance by preventing unnecessary recalculations.


150-153: Data preparation for form binding is accurate

Including displayed_elements in the data object ensures that the form is properly bound to the current configuration, facilitating user interaction with the new options.


228-230: Update label computation for new schema fields

Adding cases for "state_content", "displayed_elements", and "appearance" in _computeLabelCallback ensures that labels for these new fields are correctly localized, improving the user interface.

@@ -505,8 +505,10 @@ export interface TileCardConfig extends LovelaceCardConfig {

export interface HeadingEntityConfig {
entity: string;
content?: string | string[];
state_content?: string | string[];
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Breaking change: Rename content to state_content

The property content has been renamed to state_content. While this change improves clarity, it's a breaking change that will affect existing configurations using the content property.

To address this:

  1. Consider keeping both content and state_content for backward compatibility, deprecating content.
  2. Update the implementation to support both properties, prioritizing state_content if both are present.
  3. Add a deprecation warning when content is used.

Example:

export interface HeadingEntityConfig {
  // ...
  /** @deprecated Use state_content instead */
  content?: string | string[];
  state_content?: string | string[];
  // ...
}

@piitaya piitaya merged commit 76e53e9 into dev Sep 24, 2024
13 checks passed
@piitaya piitaya deleted the heading-entity-config branch September 24, 2024 16:15
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