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 color option to heading entities #22068

Merged
merged 6 commits into from
Sep 24, 2024
Merged

Add color option to heading entities #22068

merged 6 commits into from
Sep 24, 2024

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Sep 24, 2024

Proposed change

Color editor

CleanShot 2024-09-24 at 17 41 16

CleanShot 2024-09-24 at 18 04 20

Colored icons

CleanShot 2024-09-24 at 14 25 24

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

Release Notes

  • New Features

    • Enhanced HaColorPicker component with new properties for better customization: includeState and includeNone.
    • Introduced dynamic color handling in entity headings based on state and attributes.
    • Added color configuration options in entity editors, including an "appearance" section for better user experience.
  • Bug Fixes

    • Improved rendering logic for color selections, ensuring accurate display of state and uncolored options.
  • Documentation

    • Updated translations for clarity in color selection and entity configuration terminology.

Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Warning

Rate limit exceeded

@piitaya has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 25 minutes and 46 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 0066ff1 and 2c68b5e.

Walkthrough

Walkthrough

The pull request introduces several modifications to the HaColorPicker component and related entities, enhancing color selection capabilities. Key changes include the transition of the defaultColor property from a Boolean to a String type, the addition of includeState and includeNone properties, and updates to the UiColorSelector interface. Furthermore, new methods and properties have been added to various classes to improve color handling and configuration options, including updates to the entity badge editor and tile card editor. Additionally, changes to translation keys reflect the updated structure and terminology.

Changes

File Path Change Summary
src/components/ha-color-picker.ts Updated defaultColor from Boolean to String, added includeState and includeNone properties, modified _valueSelected and render methods to incorporate new properties.
src/components/ha-selector/ha-selector-ui-color.ts Added bindings for .includeNone and .includeState to ha-color-picker from selector.ui_color.
src/data/selector.ts Modified UiColorSelector interface to allow ui_color to include default_color as a string, include_none, and include_state as booleans.
src/panels/lovelace/cards/heading/hui-heading-entity.ts Introduced _computeStateColor method for dynamic color computation based on entity state, updated render method to apply computed colors.
src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts Updated selector configuration for color to include include_state: true.
src/panels/lovelace/editor/config-elements/hui-tile-card-editor.ts Changed default_color from Boolean to String, added include_state attribute set to true.
src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts Added color property to entityConfigStruct, restructured schema to include an "appearance" section with color options.
src/translations/en.json Updated translation keys in "color-picker" and added "appearance" key in "entity_config" section.

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: 1

Outside diff range and nitpick comments (5)
src/components/ha-selector/ha-selector-ui-color.ts (1)

27-28: LGTM! Consider a minor improvement for consistency.

The addition of includeUncolored and includeState properties enhances the customization options for the color picker, aligning well with the PR objectives. The implementation is correct and uses proper optional chaining.

For consistency with other properties, consider using the spread operator to conditionally include these new properties:

-        .includeUncolored=${this.selector.ui_color?.include_uncolored}
-        .includeState=${this.selector.ui_color?.include_state}
+        ...(this.selector.ui_color?.include_uncolored && { includeUncolored: true })
+        ...(this.selector.ui_color?.include_state && { includeState: true })

This approach would make the new properties consistent with how defaultColor is handled, only including them when they are explicitly set.

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

48-79: LGTM: New "appearance" section added to schema.

The addition of the "appearance" section with icon and color selectors improves the organization of the UI. The color selector options align well with the PR objectives.

Consider adding a comment explaining the purpose and structure of the new "appearance" section for better code maintainability.

src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts (1)

95-97: LGTM! Consider adding a comment for clarity.

The addition of include_state: true to the ui_color selector is a good enhancement. It allows the color picker to consider the entity's state when selecting colors, which aligns with the PR objectives and improves customization options.

Consider adding a brief comment explaining the purpose of include_state: true for future maintainers. For example:

selector: {
  ui_color: {
    include_state: true, // Allow color selection based on entity state
  },
},
src/panels/lovelace/editor/config-elements/hui-tile-card-editor.ts (1)

98-101: LGTM! Consider minor formatting adjustment.

The changes to the color selector align well with the PR objective of adding a color option to heading entities. The new ui_color selector with default_color: "state" and include_state: true options enhance the customization capabilities, allowing users to select colors based on entity states.

Consider adjusting the indentation of the new properties to match the parent object:

 selector: {
   ui_color: {
-    default_color: "state",
-    include_state: true,
+     default_color: "state",
+     include_state: true,
   },
 },

This minor change would improve code readability and maintain consistent formatting.

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

510-510: LGTM! Consider adding a JSDoc comment for clarity.

The addition of the optional color property to the HeadingEntityConfig interface is well-implemented and aligns with the PR objective. It allows for customization of heading entity colors without breaking existing configurations.

Consider adding a JSDoc comment to provide more context about the color property. For example:

/** Custom color for the heading entity. Can be any valid CSS color value. */
color?: string;

This addition would improve code documentation and help developers understand the expected values for this property.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c4a700a and bf4464e.

Files selected for processing (9)
  • src/components/ha-color-picker.ts (3 hunks)
  • src/components/ha-selector/ha-selector-ui-color.ts (1 hunks)
  • src/data/selector.ts (1 hunks)
  • src/panels/lovelace/cards/heading/hui-heading-entity.ts (4 hunks)
  • src/panels/lovelace/cards/types.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-tile-card-editor.ts (1 hunks)
  • src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts (4 hunks)
  • src/translations/en.json (2 hunks)
Additional comments not posted (16)
src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts (5)

1-1: LGTM: New import for color-related icon.

The addition of mdiPalette import is appropriate for the new color feature being implemented.


22-22: LGTM: New color property added to entityConfigStruct.

The addition of the optional color property of type string to entityConfigStruct is consistent with the PR objective of adding a color option to heading entities.


134-134: LGTM: New case for "appearance" added to _computeLabelCallback.

The addition of the "appearance" case in the _computeLabelCallback method is consistent with the new schema structure and follows the existing localization pattern.


Line range hint 1-165: Overall assessment: Well-implemented color option for heading entities.

The changes in this file successfully implement the color option for heading entities as described in the PR objectives. The new "appearance" section in the schema improves the organization of the UI, and the color selector includes appropriate options. The code is consistent with the existing style and patterns.

A few minor suggestions:

  1. Consider adding a comment explaining the purpose and structure of the new "appearance" section for better code maintainability.
  2. Ensure that the new localization key for the "appearance" section is defined in the appropriate localization files.

Great job on implementing this new feature!


Line range hint 134-138: Verify new localization keys.

Please ensure that the new localization key ui.panel.lovelace.editor.card.heading.entity_config.appearance is defined in the appropriate localization files.

Run the following script to check for the new localization key:

src/data/selector.ts (2)

Line range hint 1-1000: Verify usage of UiColorSelector in components

While the changes to the UiColorSelector interface are well-implemented and backwards-compatible, it's important to ensure that any components or functions using this interface are updated to handle the new properties effectively.

Please run the following script to identify components that might need updating:

#!/bin/bash
# Search for components using UiColorSelector
rg --type typescript "UiColorSelector" -C 10 src/components

Review the results and update the relevant components to take advantage of the new color selection options if necessary.


457-461: LGTM! Consider updating documentation.

The changes to the UiColorSelector interface enhance the color selection capabilities, aligning well with the PR objectives. The new optional properties provide more flexibility and customization options.

To ensure consistency across the codebase, please run the following script to check for any other occurrences of UiColorSelector that might need updating:

Additionally, consider updating the relevant documentation to reflect these new options for color selection.

Verification successful

Verification Successful!

All occurrences of UiColorSelector are confined to src/data/selector.ts and src/components/ha-selector/ha-selector-ui-color.ts. The changes are properly contained, and no additional updates are necessary.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of UiColorSelector
rg --type typescript "UiColorSelector" -C 5

Length of output: 94


Script:

#!/bin/bash
# Search for other occurrences of UiColorSelector without specifying file type
rg "UiColorSelector" -C 5

Length of output: 2427

src/translations/en.json (2)

702-704: New color picker options added: Approved

The addition of "default", "state", and "uncolored" options to the color picker enhances user flexibility in color selection. These new options provide more granular control and should improve the overall user experience when working with colors in the interface.


6012-6013: Entity configuration options improved: Approved

The addition of "content" and "appearance" keys under "entity_config" provides a clear separation of configuration options for entities. This organization improves the user experience by making it easier to distinguish between content-related and appearance-related settings when configuring entities.

src/components/ha-color-picker.ts (6)

36-39: Confirm value assignment logic in _valueSelected method

The line this.value = value === this.defaultColor ? undefined : value; sets this.value to undefined when the selected value matches this.defaultColor. Ensure this logic correctly handles the cases where defaultColor is a string and that the component behaves as expected when the default color is selected.

Consider testing the color selection to ensure that the default color functionality works correctly, and selecting the default color resets this.value appropriately.


61-67: Validate conditional icon rendering logic

In the render method, the conditional logic for displaying icons based on the selected value seems correct. However, double-check that all possible value cases ("uncolored", "state", and color strings) are handled appropriately and that the correct icons are displayed.


128-132: Ensure CSS styles are consistent

The CSS changes for .circle-color appear to be correct. Verify that the styling is consistent across different themes and that the --circle-color variable is appropriately defined in all cases where the component is used.


71-94: Review inclusion of "uncolored" and "state" options

The inclusion of "uncolored" and "state" options in the color picker is controlled by includeUncolored and includeState. Ensure that these options are needed and that their localization keys (ui.components.color-picker.uncolored and ui.components.color-picker.state) are correctly defined in the translation files.

You might want to verify that the localization keys are present:

#!/bin/bash
# Description: Check for localization keys for uncolored and state
rg --type json '"ui\.components\.color-picker\.(uncolored|state)"' src/translations/

Also, confirm that the inclusion of these options aligns with the intended user experience.


26-30: Ensure new properties are utilized appropriately

You've added includeState and includeUncolored properties to ha-color-picker. Verify that any components that should expose these options are updated to set these properties accordingly.

Run the following script to locate all instances of ha-color-picker and check if they need to include the new properties:

#!/bin/bash
# Description: Find all usages of ha-color-picker in the codebase
rg --type js --type ts '<ha-color-picker' --multiline

23-24: Update dependent components for defaultColor type change

Changing defaultColor from a Boolean to a String type may affect other components that use ha-color-picker. Ensure that all instances where ha-color-picker is used are updated to pass the correct default_color attribute value.

Run the following script to find usages of ha-color-picker with the default_color attribute:

src/panels/lovelace/cards/heading/hui-heading-entity.ts (1)

60-95: Efficient implementation of color computation with memoization

The newly added _computeStateColor method effectively computes the color based on the entity's state and configuration. Utilizing memoizeOne enhances performance by avoiding redundant computations for the same inputs.

bramkragten
bramkragten previously approved these changes Sep 24, 2024
@bramkragten
Copy link
Member

I think we should align the off state color with the no color option personally.

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: 0

Outside diff range and nitpick comments (2)
src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts (2)

95-97: LGTM! Consider adding a comment for clarity.

The update to include the entity's state color as an option in the color selector is a good improvement. It aligns well with the PR objectives and enhances the user interface as described in the summary.

Consider adding a brief comment explaining the purpose of include_state: true for future maintainers:

 selector: {
   ui_color: {
+    // Include the entity's state color as an option in the color picker
     include_state: true,
   },
 },

256-267: LGTM! Consider future extensibility.

The addition of the _computeHelperCallback method is a good improvement. It enhances the user interface by providing contextual help for the color property, which aligns well with the PR objectives.

For future extensibility, consider using a switch statement instead of an if-else chain. This will make it easier to add help text for other properties in the future:

private _computeHelperCallback = (
  schema: SchemaUnion<ReturnType<typeof this._schema>>
) => {
  switch (schema.name) {
    case "color":
      return this.hass!.localize(
        `ui.panel.lovelace.editor.badge.entity.${schema.name}_helper`
      );
    default:
      return undefined;
  }
};
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5ce0cd1 and 0066ff1.

Files selected for processing (8)
  • src/components/ha-color-picker.ts (3 hunks)
  • src/components/ha-selector/ha-selector-ui-color.ts (1 hunks)
  • src/data/selector.ts (1 hunks)
  • src/panels/lovelace/cards/heading/hui-heading-entity.ts (5 hunks)
  • src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts (3 hunks)
  • src/panels/lovelace/editor/config-elements/hui-tile-card-editor.ts (3 hunks)
  • src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts (6 hunks)
  • src/translations/en.json (4 hunks)
Files skipped from review as they are similar to previous changes (7)
  • src/components/ha-color-picker.ts
  • src/components/ha-selector/ha-selector-ui-color.ts
  • src/data/selector.ts
  • src/panels/lovelace/cards/heading/hui-heading-entity.ts
  • src/panels/lovelace/editor/config-elements/hui-tile-card-editor.ts
  • src/panels/lovelace/editor/heading-entity/hui-heading-entity-editor.ts
  • src/translations/en.json
Additional comments not posted (1)
src/panels/lovelace/editor/config-elements/hui-entity-badge-editor.ts (1)

208-208: LGTM! Good integration of the helper callback.

The addition of the computeHelpText property to the ha-form component is a good integration of the newly added _computeHelperCallback method. This change enables the display of contextual help text for form fields, which enhances the user experience and supports the PR objectives.

bramkragten
bramkragten previously approved these changes Sep 24, 2024
@piitaya piitaya force-pushed the heading-entity-color branch from 1ec1083 to 2c68b5e Compare September 24, 2024 16:19
@piitaya piitaya requested a review from bramkragten September 24, 2024 16:29
@bramkragten bramkragten merged commit 813feff into dev Sep 24, 2024
13 checks passed
@bramkragten bramkragten deleted the heading-entity-color branch September 24, 2024 18:14
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