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

Display live remaining time for timer on tile card #21290

Merged
merged 1 commit into from
Jul 11, 2024
Merged

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Jul 4, 2024

Proposed change

Only the state was displayed before.
I also clean up state-summary and state-card to share the same logic.

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

    • Introduced state-display-timer component for displaying timer information in various parts of the UI.
  • Refactor

    • Enhanced HuiTileCard rendering logic for "update" and "timer" domains, improving display and maintainability.
    • Refactored HuiTimerEntityRow and state-card-timer components to utilize the new state-display-timer component, removing redundant timer logic.
  • Bug Fixes

    • Updated display timer logic to show "0" when no time remains, enhancing clarity when the timer has expired.

Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Walkthrough

Walkthrough

The updates center around enhancing the timer display functionality within a TypeScript project. Key changes include refactoring timer calculation and display logic into a modular component, state-display-timer, simplifying the related classes, and improving the rendering logic for domains like "update" and "timer." These updates streamline code for maintainability and clarity, primarily affecting how timers are rendered and managed across various components.

Changes

File Path Summary
src/data/timer.ts Modified computeDisplayTimer function to update display logic for timer states.
src/panels/lovelace/cards/hui-tile-card.ts Updated the HuiTileCard class. Refactored rendering logic for "update" and "timer" domains, including a new HTML template and improved state information display.
src/panels/lovelace/entity-rows/... Refactored HuiTimerEntityRow class to use state-display-timer component, removing interval handling and timer calculation logic.
src/state-display/state-display-timer.ts Added a new state-display-timer custom element for displaying timer information.
src/state-summary/state-card-timer.ts Refactored state-card-timer component to use the state-display-timer component, removing direct timer calculations and display logic.

Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 202bd14 and f30a146.

Files selected for processing (5)
  • src/data/timer.ts (1 hunks)
  • src/panels/lovelace/cards/hui-tile-card.ts (1 hunks)
  • src/panels/lovelace/entity-rows/hui-timer-entity-row.ts (3 hunks)
  • src/state-display/state-display-timer.ts (1 hunks)
  • src/state-summary/state-card-timer.ts (3 hunks)
Additional context used
Biome
src/state-display/state-display-timer.ts

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

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

(lint/suspicious/noExplicitAny)

src/panels/lovelace/cards/hui-tile-card.ts

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

(lint/style/noNonNullAssertion)

Additional comments not posted (16)
src/state-summary/state-card-timer.ts (2)

6-6: Import statement looks good.

The import for state-display-timer is correctly added.


26-29: Component usage looks good.

The state-display-timer component is correctly used and properties are passed as expected.

src/panels/lovelace/entity-rows/hui-timer-entity-row.ts (2)

3-3: Import statement looks good.

The import for state-display-timer is correctly added.


41-44: Component usage looks good.

The state-display-timer component is correctly used and properties are passed as expected.

src/state-display/state-display-timer.ts (9)

1-5: Import statements look good.

The import statements are correct and include necessary dependencies.


8-14: Class definition and properties look good.

The class StateDisplayTimer extends ReactiveElement and includes necessary properties.


21-25: Method update looks good.

The update method is correctly implemented to update the innerHTML with the computed display timer.


27-37: Lifecycle hooks look good.

The connectedCallback and disconnectedCallback methods are correctly implemented to manage the interval.


39-44: Lifecycle hook willUpdate looks good.

The willUpdate method is correctly implemented to start the interval when the stateObj property changes.


46-51: Method _clearInterval looks good.

The _clearInterval method is correctly implemented to clear the interval.


53-63: Method _startInterval looks good.

The _startInterval method is correctly implemented to start the interval and calculate the remaining time.


65-67: Method _calculateRemaining looks good.

The _calculateRemaining method is correctly implemented to calculate the remaining time using the timerTimeRemaining function.


17-25: Avoid using any type.

The _updateRemaining property is typed as any, which disables many type checking rules. Its use should be avoided.

-  private _updateRemaining: any;
+  private _updateRemaining: number | null;

Likely invalid or redundant comment.

src/data/timer.ts (1)

95-95: Function computeDisplayTimer looks good.

The computeDisplayTimer function is correctly implemented to show "0" when no time remains and adds information when paused.

src/panels/lovelace/cards/hui-tile-card.ts (2)

319-326: LGTM!

The addition of the state-display-timer component to handle the "timer" domain is a good approach to modularize and encapsulate the timer display logic.


319-326: LGTM!

The addition of the state-display-timer component to handle the "timer" domain is a good approach to modularize and encapsulate the timer display logic.


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.

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

src/panels/lovelace/cards/hui-tile-card.ts Show resolved Hide resolved
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Jul 4, 2024
Copy link
Contributor

@silamon silamon left a comment

Choose a reason for hiding this comment

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

This does fix it for the tile card, but you need to keep the state content empty. If you're adding "remaining" and "duration" both to the state content, it will just show the initial duration of the timer for both "remaining" and "duration".

Comment on lines +17 to +19
protected createRenderRoot() {
return this;
}
Copy link
Member

Choose a reason for hiding this comment

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

Why no shadowroot?

@piitaya
Copy link
Member Author

piitaya commented Jul 11, 2024

This does fix it for the tile card, but you need to keep the state content empty. If you're adding "remaining" and "duration" both to the state content, it will just show the initial duration of the timer for both "remaining" and "duration".

Yep I will look to create another PR to let the user select these special states.

@piitaya piitaya merged commit e59c04c into dev Jul 11, 2024
15 checks passed
@piitaya piitaya deleted the timer_tile_card branch July 11, 2024 09:09
@piitaya piitaya mentioned this pull request Jul 11, 2024
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timers remaning time not working in tile card
4 participants