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 Heading card #22008

Merged
merged 15 commits into from
Sep 19, 2024
Merged

Add Heading card #22008

merged 15 commits into from
Sep 19, 2024

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Sep 17, 2024

Proposed change

CleanShot 2024-09-17 at 14 01 15@2x

The heading card can have 2 styles : title or subtitle.
The icon, the navigation are also configurable.
Optional entities can be added on the right to display some information with the following configuration : entity_id, icon and state content.

From a technical POV, this PR also contain an attempt to bring a generic sub form for entity edition.

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

Release Notes

  • New Features

    • Introduced a customizable heading card for the Lovelace UI, allowing users to display headings, icons, and entity states.
    • Added an interactive editor for configuring heading cards and managing entities.
    • Implemented a new hui-entities-editor for adding, editing, and reordering entities in a drag-and-drop interface.
    • Added support for a dual-mode editing interface with the hui-sub-form-editor.
    • Expanded card creation options by including the new heading card type.
  • Improvements

    • Enhanced user experience in the Lovelace editor with structured forms for configuring heading cards.
    • Improved validation for card properties, ensuring a smoother setup process.
    • Simplified grid layout for better responsiveness and flexibility in card creation.
  • Translations

    • Added new entries for headings and entity management in the user interface translations.

Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Walkthrough

Walkthrough

The changes introduce the HuiHeadingCard component to the Home Assistant Lovelace UI, allowing users to create customizable heading cards that display headings, icons, and a list of entities. The HuiHeadingCardEditor facilitates the configuration of these cards, managing properties and state through a structured form. Additionally, new methods and interfaces are implemented for configuration, rendering, and validation, enhancing the overall functionality of the Lovelace editor. CSS styles for various components are also updated to improve visual presentation.

Changes

Files Change Summary
src/panels/lovelace/cards/hui-heading-card.ts, src/panels/lovelace/cards/types.ts, src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts Introduced HuiHeadingCard class for customizable heading cards, new interfaces for configuration, and HuiHeadingCardEditor for configuring these cards with validation and state management.
src/panels/lovelace/editor/config-elements/hui-card-edit-mode.ts, src/panels/lovelace/editor/config-elements/hui-sub-form-editor.ts Minor adjustments to CSS styles, including padding and icon sizes, to enhance the visual appearance and layout of the card edit mode and sub-form editor.
src/panels/lovelace/editor/process-editor-entities.ts Modified the processEditorEntities function to enhance type safety by explicitly defining its parameter type as an array of EntityConfig objects or strings.
src/translations/en.json Added new entries for "heading" and "entities" sections to improve user interface organization and management options within the dashboard.
src/panels/lovelace/create-element/create-card-element.ts, src/panels/lovelace/editor/lovelace-cards.ts Integrated the new heading card into the card creation system and updated the core card types to include the heading card.
src/panels/lovelace/editor/config-elements/hui-entities-editor.ts Introduced hui-entities-editor component for managing a list of entities, supporting add, edit, remove, and reorder functionalities through a sortable list interface.
src/panels/lovelace/sections/hui-grid-section.ts Updated _addCard method to include "heading" as a suggested card type and modified CSS for grid layout to improve responsiveness.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 01a47c0 and 911b523.

Files selected for processing (1)
  • src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts

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.

@piitaya piitaya changed the title Add header card Add Heading card Sep 17, 2024
@piitaya piitaya mentioned this pull request Sep 17, 2024
9 tasks
@piitaya piitaya force-pushed the header-card branch 2 times, most recently from f40f076 to 827da74 Compare September 18, 2024 11:55
}

protected render() {
if (!this.entities || !this.hass) {
Copy link
Member

Choose a reason for hiding this comment

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

This means nothing is rendered (not even an add) if there are not entities in the config?

Suggested change
if (!this.entities || !this.hass) {
if (!this.hass) {

}
}

@customElement("hui-sub-form-editor")
Copy link
Member

Choose a reason for hiding this comment

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

Are planning on migrating the rest of the sub forms to this one too?

Copy link
Member

Choose a reason for hiding this comment

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

Or should we make a base version that can be used by hui-sub-element-editor and hui-sub-form-editor, seems like there is too much duplicate code now.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 71947ae and 90ef9ab.

Files selected for processing (3)
  • src/panels/lovelace/cards/hui-heading-card.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-entities-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/panels/lovelace/cards/hui-heading-card.ts
  • src/panels/lovelace/editor/config-elements/hui-entities-editor.ts
Additional context used
GitHub Check: Lint and check format
src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts

[failure] 156-156:
Object is possibly 'undefined'.


[failure] 160-160:
Object is possibly 'undefined'.


[failure] 180-180:
Object is possibly 'undefined'.


[failure] 192-192:
Object is possibly 'undefined'.

Additional comments not posted (6)
src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts (6)

1-36: LGTM!

The import statements look good and are importing the necessary dependencies for the component.


47-47: The following past review comment is still applicable:

bramkragten: here entities is set to any, but below is a struct for entities? Why not use it?

Do we support both a string and object for entities? Because the editor seems to not work when using a string.

Please address this comment if it hasn't been resolved yet.


51-55: LGTM!

The entityConfigStruct definition looks good and properly defines the schema for individual entities using superstruct.


57-320: LGTM!

The HuiHeadingCardEditor class is well-implemented and follows best practices for a LitElement component. The rendering logic, event handling, and styling are all properly structured and enhance the user experience for configuring heading cards.

Some key highlights:

  • The use of memoizeOne for efficient schema generation.
  • Clean and understandable rendering logic.
  • Proper event handling for configuration changes and entity editing.
  • Localized label computation for form fields.
  • Encapsulated styling using CSS literals.

Overall, this component improves the functionality and flexibility of the Lovelace UI.

Tools
GitHub Check: Lint and check format

[failure] 156-156:
Object is possibly 'undefined'.


[failure] 160-160:
Object is possibly 'undefined'.


[failure] 180-180:
Object is possibly 'undefined'.


[failure] 192-192:
Object is possibly 'undefined'.


156-156: Address the static analysis hints related to possible 'undefined' objects.

The static analysis tool has flagged the following lines as potentially accessing 'undefined' objects:

  • Line 156: this.hass
  • Line 160: this._entityFormEditorData.data
  • Line 180: this.hass.localize
  • Line 192: this._config.entities

Please double-check these lines and ensure that the objects are properly defined before accessing them. If necessary, add appropriate null checks or default values to prevent potential runtime errors.

Also applies to: 160-160, 180-180, 192-192

Tools
GitHub Check: Lint and check format

[failure] 156-156:
Object is possibly 'undefined'.


192-192: The following past review comment is still applicable:

bramkragten: use processEditorEntities(this._config.entities)

Please address this comment if it hasn't been resolved yet.

Tools
GitHub Check: Lint and check format

[failure] 192-192:
Object is possibly 'undefined'.

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 (1)
src/panels/lovelace/editor/hui-sub-form-editor.ts (1)

164-167: Refine typing in _valueChanged method

In the _valueChanged method, ev.target is cast to any to access the value property. This usage of any can be avoided by specifying the event type more precisely, enhancing type safety and readability.

Consider defining a custom event type or using a generic type to avoid using any:

-  const value = (ev.detail.value ?? (ev.target as any).value ?? {}) as T;
+  const value = (ev.detail.value ?? (ev.target as HTMLInputElement).value ?? {}) as T;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 69a24f8 and 01a47c0.

Files selected for processing (13)
  • src/panels/lovelace/cards/hui-heading-card.ts (1 hunks)
  • src/panels/lovelace/cards/types.ts (1 hunks)
  • src/panels/lovelace/components/hui-card-edit-mode.ts (1 hunks)
  • src/panels/lovelace/create-element/create-card-element.ts (2 hunks)
  • src/panels/lovelace/editor/config-elements/hui-entities-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts (1 hunks)
  • src/panels/lovelace/editor/hui-sub-form-editor.ts (1 hunks)
  • src/panels/lovelace/editor/lovelace-cards.ts (1 hunks)
  • src/panels/lovelace/editor/process-editor-entities.ts (1 hunks)
  • src/panels/lovelace/editor/types.ts (1 hunks)
  • src/panels/lovelace/sections/hui-grid-section.ts (2 hunks)
  • src/translations/en.json (4 hunks)
Files skipped from review as they are similar to previous changes (11)
  • src/panels/lovelace/cards/hui-heading-card.ts
  • src/panels/lovelace/cards/types.ts
  • src/panels/lovelace/components/hui-card-edit-mode.ts
  • src/panels/lovelace/create-element/create-card-element.ts
  • src/panels/lovelace/editor/config-elements/hui-entities-editor.ts
  • src/panels/lovelace/editor/config-elements/hui-heading-card-editor.ts
  • src/panels/lovelace/editor/lovelace-cards.ts
  • src/panels/lovelace/editor/process-editor-entities.ts
  • src/panels/lovelace/editor/types.ts
  • src/panels/lovelace/sections/hui-grid-section.ts
  • src/translations/en.json
Additional comments not posted (2)
src/panels/lovelace/editor/config-elements/hui-map-card-editor.ts (1)

184-184: LGTM!

The change adds a safeguard to prevent potential runtime errors when this._config.entities is undefined. It reflects a defensive programming approach to handle missing data gracefully.

src/panels/lovelace/editor/hui-sub-form-editor.ts (1)

1-190: Overall, the component is well-implemented

The HuiSubFormEditor component is thoughtfully designed, providing flexibility between YAML and visual editing modes. It adheres to best practices in the codebase, and the logic is clear and maintainable.

Comment on lines +131 to +145
protected willUpdate(changedProperties: PropertyValues<this>) {
if (changedProperties.has("data")) {
if (this.assertConfig) {
try {
this.assertConfig(this.data);
this._warnings = undefined;
this._errors = undefined;
} catch (err: any) {
const msgs = handleStructError(this.hass, err);
this._warnings = msgs.warnings ?? [err.message];
this._errors = msgs.errors || undefined;
this._yamlMode = true;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add unit tests for configuration validation logic

The willUpdate lifecycle method contains critical logic for validating the configuration and handling errors and warnings. Implementing unit tests for this method will help ensure that the validation behaves correctly and prevent future regressions.

Comment on lines +39 to +45
public schema!: LovelaceConfigForm["schema"];

public assertConfig?: (config: T) => void;

public computeLabel?: LovelaceConfigForm["computeLabel"];

public computeHelper?: LovelaceConfigForm["computeHelper"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Annotate reactive properties with @property decorator

The properties schema, assertConfig, computeLabel, and computeHelper are not annotated with the @property decorator. Without this decorator, changes to these properties won't trigger re-rendering, which may lead to the component not updating as expected.

Apply this diff to add the necessary decorators:

+  @property({ attribute: false }) public schema!: LovelaceConfigForm["schema"];

+  @property({ attribute: false }) public assertConfig?: (config: T) => void;

+  @property({ attribute: false }) public computeLabel?: LovelaceConfigForm["computeLabel"];

+  @property({ attribute: false }) public computeHelper?: LovelaceConfigForm["computeHelper"];
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public schema!: LovelaceConfigForm["schema"];
public assertConfig?: (config: T) => void;
public computeLabel?: LovelaceConfigForm["computeLabel"];
public computeHelper?: LovelaceConfigForm["computeHelper"];
@property({ attribute: false }) public schema!: LovelaceConfigForm["schema"];
@property({ attribute: false }) public assertConfig?: (config: T) => void;
@property({ attribute: false }) public computeLabel?: LovelaceConfigForm["computeLabel"];
@property({ attribute: false }) public computeHelper?: LovelaceConfigForm["computeHelper"];

@bramkragten bramkragten merged commit b776388 into dev Sep 19, 2024
13 checks passed
@bramkragten bramkragten deleted the header-card branch September 19, 2024 08:46
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