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 sensor offset to time trigger UI #21957

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

karwosts
Copy link
Contributor

@karwosts karwosts commented Sep 11, 2024

Proposed change

Frontend for home-assistant/core#120858

image

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

Summary by CodeRabbit

  • New Features

    • Enhanced flexibility for time trigger configurations, allowing for more complex settings.
    • Improved contextual output for automation triggers, providing clearer information about entities and offsets.
    • Added localization support for new key-value pairs related to automation triggers, improving user interface clarity.
  • Bug Fixes

    • Refined handling of input modes in automation triggers for better usability and functionality.

Copy link
Contributor

coderabbitai bot commented Sep 11, 2024

Walkthrough

Walkthrough

The pull request introduces modifications to the automation system, primarily focusing on the TimeTrigger interface and related logic. The at property type is expanded to accommodate more complex configurations, allowing for additional parameters like entity_id and offset. Changes are also made to the tryDescribeTrigger function to enhance output clarity. The HaTimeTrigger class is updated to improve input mode handling and schema generation. Additionally, new localization entries are added to the language files to support the updated features.

Changes

Files Change Summary
src/data/automation.ts Modified TimeTrigger interface to change at property type from string to `string
src/data/automation_i18n.ts Updated tryDescribeTrigger function to construct a more informative output string when processing at parameter for time triggers.
src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts Enhanced HaTimeTrigger class with new input modes, updated schema handling, and improved logic for determining inputMode.
src/translations/en.json Added new localization entries: "offset", "entity", and "offset_by" to support the updated automation trigger features.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ce6e484 and fa95ea4.

Files selected for processing (1)
  • src/data/automation_i18n.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/data/automation_i18n.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 generate interesting stats about this repository and render them as a table.
    -- @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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 23e37da and ce6e484.

Files selected for processing (4)
  • src/data/automation.ts (1 hunks)
  • src/data/automation_i18n.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts (4 hunks)
  • src/translations/en.json (1 hunks)
Additional context used
Biome
src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (7)
src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts (3)

23-26: Approved: Enhanced input mode handling.

The modification to _inputMode supports the new functionality effectively by allowing the component to handle different types of inputs, enhancing the flexibility and usability of the time trigger configuration.


33-76: Approved: Dynamic schema generation enhancements.

The updates to the _schema method enhance its functionality by making the schema generation dynamic and responsive to the user's input mode and offset visibility. This is a key improvement for supporting the new sensor offset feature in the time trigger UI.


100-119: Approved: Render method updates for enhanced UI responsiveness.

The changes in the render method ensure that the UI components are updated dynamically based on the trigger configuration. This is crucial for the usability and accuracy of the time trigger settings, especially with the new sensor offset feature.

src/data/automation.ts (1)

156-156: Approve the modification with a suggestion to verify integration.

The change to the TimeTrigger interface, allowing the at property to accept both a string and a complex object, is a significant enhancement for flexibility in automation triggers. This aligns well with the PR's objectives.

However, ensure that this change is thoroughly tested to confirm that it integrates seamlessly with the UI and that all parts of the system that interact with the TimeTrigger are updated to handle the new type structure.

Run the following script to verify the integration of the TimeTrigger changes:

src/translations/en.json (3)

3046-3046: Localization Key Added: "offset"

The key "offset" has been added with the value referencing another localization key for the offset configuration in automation triggers. This is a standard practice for maintaining localization consistency and reusability.


3047-3047: Localization Key Added: "entity"

The key "entity" has been added with a descriptive value "Entity with timestamp". This is clear and provides a good description for the type of entity expected, which is useful for clarity in the user interface.


3048-3048: Localization Key Added: "offset_by"

The key "offset_by" has been added with a formatted string "offset by {offset}". This allows for dynamic insertion of the offset value in the UI, enhancing the flexibility of the localization. The use of placeholders in localization strings is a good practice as it supports dynamic content.

Comment on lines +136 to +157
const newValue = { ...ev.detail.value };
this._inputMode = newValue.mode;
if (newValue.mode === MODE_TIME) {
delete newValue.entity;
delete newValue.offset;
} else {
delete newValue.time;
if (!newValue.entity?.startsWith("sensor.")) {
delete newValue.offset;
}
}
fireEvent(this, "value-changed", {
value: {
platform: "time",
at: newValue.offset
? {
entity_id: newValue.entity,
offset: newValue.offset,
}
: newValue.entity || newValue.time,
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider replacing the delete operator for better performance.

The use of the delete operator in the _valueChanged method could impact performance. Consider setting properties to undefined instead, which can be more performance-friendly in JavaScript environments.

Here's a suggested change:

- delete newValue.entity;
- delete newValue.offset;
- delete newValue.time;
+ newValue.entity = undefined;
+ newValue.offset = undefined;
+ newValue.time = undefined;

Verify that this change does not affect the functionality, as the semantic meaning of undefined and a property not existing can differ in some contexts.

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
const newValue = { ...ev.detail.value };
this._inputMode = newValue.mode;
if (newValue.mode === MODE_TIME) {
delete newValue.entity;
delete newValue.offset;
} else {
delete newValue.time;
if (!newValue.entity?.startsWith("sensor.")) {
delete newValue.offset;
}
}
fireEvent(this, "value-changed", {
value: {
platform: "time",
at: newValue.offset
? {
entity_id: newValue.entity,
offset: newValue.offset,
}
: newValue.entity || newValue.time,
},
});
const newValue = { ...ev.detail.value };
this._inputMode = newValue.mode;
if (newValue.mode === MODE_TIME) {
newValue.entity = undefined;
newValue.offset = undefined;
} else {
newValue.time = undefined;
if (!newValue.entity?.startsWith("sensor.")) {
newValue.offset = undefined;
}
}
fireEvent(this, "value-changed", {
value: {
platform: "time",
at: newValue.offset
? {
entity_id: newValue.entity,
offset: newValue.offset,
}
: newValue.entity || newValue.time,
},
});
Tools
Biome

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

@@ -349,7 +349,7 @@ const tryDescribeTrigger = (
if (trigger.platform === "time" && trigger.at) {
const result = ensureArray(trigger.at).map((at) =>
typeof at !== "string"
? at
? `entity ${hass.states[at.entity_id] ? computeStateName(hass.states[at.entity_id]) : at.entity_id} ${at.offset ? " " + hass.localize(`${triggerTranslationBaseKey}.time.offset_by`, { offset: describeDuration(hass.locale, at.offset) }) : ""}`
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 11, 2024

Choose a reason for hiding this comment

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

Confirm and enhance the handling of at parameter in time triggers.

The updated implementation for handling the at parameter in time triggers is a significant improvement, providing more detailed descriptions based on the entity's state and optional offset. This change aligns with the PR's goal to enhance the user interface by adding sensor offset functionality.

However, consider enhancing the readability of this block by breaking down the complex ternary operation into more manageable parts or using helper functions. This could improve maintainability and make the code easier to understand for future contributors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll follow the rabbit comment here when reviewing. The ternary operator is being used correctly and efficiently. However, combining multiple ternaries in a single line can make the code harder to read and maintain. It might be clearer to refactor this into if-else blocks for better readability.

Copy link
Contributor

Choose a reason for hiding this comment

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

@silamon: Thank you for considering the suggestion! Refactoring the complex ternary operations into if-else blocks will certainly enhance readability and make the code easier to maintain. Please feel free to reach out if you need any assistance.

(^.^)/

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