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 new badges design with UI editor #21401

Merged
merged 32 commits into from
Jul 19, 2024
Merged

Add new badges design with UI editor #21401

merged 32 commits into from
Jul 19, 2024

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Jul 15, 2024

Breaking change

The current badges will use the new design.

Proposed change

Hui Badge

Badge are now loaded using hui-badge component instead of having the view responsible of loading these element

New badge 🎉

CleanShot 2024-07-17 at 10 25 45

A new type of badge has been added : the entity badge. The design is more compact than the previous one and better match the tile card design.

3 display types are available :

  • minimal : icon only
  • standard : icon and state, this is the default
  • complete : icon, name and state

Customization are similar to tile card options : name, icon, color, state content and entity picture instead of icon.

Visibility options

Like cards and sections, badges support all the available visibility conditions : https://www.home-assistant.io/dashboards/conditional/#conditions-options

Custom badges

Custom badges was already supported but not documented. It's now supported by the card editor. It's loaded the same way as custom cards. A pull request in developer blog and documentation will follow.

Edit mode

The badge can be added using the + on the top of the view. Masonry view and section view are the only ones that supports badges. Badges can also be deleted and moved using drag and drop.

By clicking a badge, you can edit all the configuration directly from the UI the same way as card.

CleanShot 2024-07-17 at 11 24 32

Future Improvement to do (not in this PR) :

  • Add copy/cut badges
  • Better badge error
  • Use badge picker when adding a badge

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

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

    • Added support for customizable and interactive badges in Lovelace views.
    • Introduced new HuiBadge and HuiEntityBadge elements for enhanced badge management.
    • Added HuiViewBadges component for flexible badge display and editing.
    • Implemented hui-badge-edit-mode for easier badge configuration and manipulation.
  • Improvements

    • Enhanced badge visibility and functionality with new properties like visibility and preview.
    • Updated UI elements to better handle state representation and interactions within Lovelace.
    • Introduced new interface CustomBadgeEntry to streamline custom badge management.
  • Bug Fixes

    • Fixed issues related to badge display and interaction, ensuring consistent behavior across different Lovelace views.

@surfingbytes
Copy link

This new design is great, thank you 👍 Much more compact, so I'm sure users will like it. However, it doesn't give the "warning" vibe as the old one, so could we please have them side by side (maybe with the old one changed to regular card, so it can be used anywhere). Here is an example how the current one can be used as "warning" to water plant and show running timer, which the new design really is not the same:
image

@piitaya
Copy link
Member Author

piitaya commented Jul 15, 2024

Thank you for the feedback.

The old badges will always work as before. Badges are just a smaller card. You can mix them if you want but it can not look very good as the height is not the same for these different kind of badge.

Also, more type of badge can be added in the future (alerting ones for example if it make sense). Even custom badges can be added.

@Gloomyeye
Copy link

The new badges look good. I would prefer if you could add an optional name/title to the badges. If you add two light badges, then you can only distingush the badges with the state or the icon. A badge name/title (maybe in a secondary line) would give more insights.

@piitaya piitaya marked this pull request as ready for review July 17, 2024 09:27
Copy link
Contributor

coderabbitai bot commented Jul 17, 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

Walkthrough

The changes encompass modifications to Lovelace components in Home Assistant, focusing on refining badge handling, configuration, and visibility. Key updates include replacing LovelaceBadge with HuiBadge, restructuring badge configurations, adding new interfaces and properties, and introducing new custom elements for badge management and editing. These enhancements streamline the badge handling process, allow for more flexible configurations, and improve the user experience in the Lovelace UI.

Changes

File(s) Change Summary
src/data/lovelace.ts Replaced LovelaceBadge with HuiBadge in imports and type declarations.
src/data/lovelace/config/badge.ts Added visibility property to LovelaceBadgeConfig interface.
src/data/lovelace/config/section.ts Added badges property to LovelaceSectionConfig interface.
src/data/lovelace/config/view.ts Updated LovelaceViewConfig interface to use LovelaceBadgeConfig for badges.
src/data/lovelace_custom_cards.ts Introduced CustomBadgeEntry interface, updated CustomCardsWindow interface, added customBadges array and related functions.
src/panels/lovelace/badges/hui-badge.ts Created HuiBadge custom element with properties and methods for badge handling.
src/panels/lovelace/badges/hui-entity-badge.ts Introduced HuiEntityBadge class for custom entity badges, with methods for rendering and interaction.
src/panels/lovelace/badges/hui-entity-filter-badge.ts Added preview property, updated _elements type, and changed badge element creation logic.
src/panels/lovelace/badges/hui-error-badge.ts Switched icon import, added import for showAlertDialog and HuiEntityBadge, updated rendering logic and styles.
src/panels/lovelace/badges/hui-view-badges.ts Introduced HuiViewBadges custom element for managing badges in Lovelace views, with properties and methods for badge manipulation and rendering.
src/panels/lovelace/badges/types.ts Added EntityBadgeConfig interface extending LovelaceBadgeConfig.
src/panels/lovelace/cards/hui-tile-card.ts Renamed localizedState to stateDisplay for clearer meaning.
src/panels/lovelace/components/hui-badge-edit-mode.ts Introduced HuiBadgeEditMode custom element for editing badges, with properties and methods for user interactions and rendering.
src/panels/lovelace/components/hui-card-edit-mode.ts Renamed findLovelaceCards to findLovelaceItems, updated method _cards accordingly.
src/panels/lovelace/components/hui-card-options.ts Updated findLovelaceCards to findLovelaceItems with an additional argument in _cards getter method.
src/panels/lovelace/create-element/create-badge-element.ts Modified createLovelaceElement function and added getBadgeElementClass function.

Sequence Diagram(s)

Badge Handling in Lovelace

sequenceDiagram
  participant User
  participant LovelaceUI
  participant HuiBadge
  participant CustomBadgeEntry
  participant HomeAssistant

  User->>LovelaceUI: Interact with badge
  LovelaceUI->>HuiBadge: Create/Update badge
  HuiBadge->>HomeAssistant: Fetch entity state
  HomeAssistant-->>HuiBadge: Return entity state
  HuiBadge->>LovelaceUI: Render badge
  LovelaceUI-->>User: Display updated badge
Loading

Badge Configuration and Visibility

sequenceDiagram
  participant User
  participant LovelaceUI
  participant BadgeConfig
  participant HuiBadge
  participant HomeAssistant

  User->>LovelaceUI: Configure badge visibility
  LovelaceUI->>BadgeConfig: Update visibility conditions
  BadgeConfig-->>LovelaceUI: Return updated config
  LovelaceUI->>HuiBadge: Apply config
  HuiBadge->>HomeAssistant: Validate conditions
  HomeAssistant-->>HuiBadge: Return validation result
  HuiBadge->>LovelaceUI: Update badge visibility
  LovelaceUI-->>User: Display changes
Loading

Custom Badge Management

sequenceDiagram
  participant User
  participant LovelaceUI
  participant CustomBadgeEntry
  participant HuiBadge
  participant HomeAssistant

  User->>LovelaceUI: Add/Remove custom badge
  LovelaceUI->>CustomBadgeEntry: Fetch badge entry
  CustomBadgeEntry-->>LovelaceUI: Return badge entry
  LovelaceUI->>HuiBadge: Configure badge
  HuiBadge->>HomeAssistant: Fetch entity state (if applicable)
  HomeAssistant-->>HuiBadge: Return entity state
  HuiBadge->>LovelaceUI: Render custom badge
  LovelaceUI-->>User: Display custom badge
Loading

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 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.

@frenck frenck added the Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label Jul 17, 2024
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: 12

Outside diff range, codebase verification and nitpick comments (2)
src/panels/lovelace/components/hui-badge-edit-mode.ts (1)

48-53: Consider using local storage for persistent clipboard data.

The clipboard data is stored in session storage, which will clear when the session ends. If persistence beyond the session is desired, consider using local storage.

src/panels/lovelace/editor/config-util.ts (1)

323-416: Unit tests for badge manipulation functions are missing.

The functions addBadge, replaceBadge, deleteBadge, insertBadge, and moveBadge do not appear to be covered by unit tests. To ensure their reliability and correctness, please add unit tests for these functions.

  • addBadge
  • replaceBadge
  • deleteBadge
  • insertBadge
  • moveBadge
Analysis chain

Review newly introduced badge manipulation functions.

The addition of functions like addBadge, replaceBadge, deleteBadge, insertBadge, and moveBadge aligns with the PR's objectives to handle badges similarly to cards. Ensure that these functions are consistent with their card counterparts in terms of logic and error handling.

Additionally, it's important to verify that these new functions are covered by unit tests to ensure their reliability.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unit tests covering the new badge functions.

# Test: Search for unit tests related to the new badge functions.
fd --type f "test_config-util.*" --exec rg "addBadge|replaceBadge|deleteBadge|insertBadge|moveBadge"

Length of output: 102


Script:

#!/bin/bash
# Description: Check for unit tests covering the new badge functions in all test files.

# Test: Search for unit tests related to the new badge functions across all test files.
fd --type f --exec rg "addBadge|replaceBadge|deleteBadge|insertBadge|moveBadge"

Length of output: 860


Script:

#!/bin/bash
# Description: Check for unit tests covering the new badge functions by looking for test cases.

# Test: Search for test cases related to the new badge functions.
fd --type f --exec rg -A 5 "(test|it|describe)" | rg "addBadge|replaceBadge|deleteBadge|insertBadge|moveBadge"

Length of output: 400

@piitaya
Copy link
Member Author

piitaya commented Jul 17, 2024

@coderabbitai pause

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

Outside diff range, codebase verification and nitpick comments (1)
src/panels/lovelace/badges/hui-error-badge.ts (1)

Line range hint 1-92: Review of HuiErrorBadge class:

This class provides an error badge component with detailed error information. The addition of _viewDetail method enhances user interaction by providing detailed error information when clicked.

Suggested Improvement: Error Handling in _viewDetail
The error handling in the _viewDetail method is basic. Consider providing more detailed error messages or logging for better diagnostics.

- dumped = `[Error dumping ${this._config!.origConfig}]`;
+ dumped = `[Error dumping configuration: ${err.message}]`;

This change provides more context in the error message, which can be helpful for debugging.

src/data/lovelace/config/section.ts Outdated Show resolved Hide resolved
@@ -27,7 +27,7 @@ export interface LovelaceBaseViewConfig {

export interface LovelaceViewConfig extends LovelaceBaseViewConfig {
type?: string;
badges?: Array<string | LovelaceBadgeConfig>;
badges?: LovelaceBadgeConfig[];
Copy link
Member

Choose a reason for hiding this comment

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

we should still support just strings, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes for backward compatibility, I reverted that change.

type: "entity";
entity?: string;
name?: string;
icon?: string;
Copy link
Member

Choose a reason for hiding this comment

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

The StateLabelBadgeConfig supported image, should we keep that around in the new one?

Maybe also migrate the option show_name to a display_type?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will make another PR to properly migrate the state label badge

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft July 18, 2024 11:30
@piitaya piitaya marked this pull request as ready for review July 18, 2024 12:44
@home-assistant home-assistant bot requested a review from bramkragten July 18, 2024 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants