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

Translation support for device automation extra fields #20567

Merged

Conversation

farmio
Copy link
Contributor

@farmio farmio commented Apr 20, 2024

Proposed change

Add translation support for integration specific device trigger extra fields.

Core PR to allow new keys in strings.json: home-assistant/core#115892

This can be tested with a core-branch adding a new device trigger for "Sun" with some dummy extra_fields
home-assistant/core#115846

Given an integrations strings.json

{
  "device_automation": {
    "trigger_type": {
      "test_sun_trigger": "Sun trigger 🌞"
    },
    "extra_fields": {
      "bool": "Boolean",
      "time": "Time ⏰",
      "number": "Number 🔢",
      "datetime": "Date and time",
      "duration": "Duration ⏳",
      "cv_bool": "Config validation boolean",
      "plain_bool": "Plain boolean"
    },
    "extra_fields_descriptions": {
      "bool": "Boolean helper text",
      "time": "A nice time",
      "number": "Some number between 10 and 100",
      "datetime": "Today may be the day 📅",
      "duration": "Duration ⏳"
    }
  }
}
previous extra field translation support
Bildschirmfoto 2024-04-20 um 16 46 25 Bildschirmfoto 2024-04-20 um 16 45 25

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

  • New Features
    • Enhanced localization for automation actions, conditions, and triggers by adding new callbacks for computing labels and helper texts.
  • Improvements
    • Streamlined UI by removing redundant translation keys, simplifying descriptions for device triggers, conditions, and actions.

@farmio farmio changed the title Translation support for device trigger extra fields Translation support for device automation extra fields Apr 22, 2024
@farmio farmio requested a review from bramkragten June 8, 2024 14:42
@farmio farmio force-pushed the device-trigger-extra-fields-translations branch from 6953a62 to c64fe6f Compare June 8, 2024 14:45
Copy link
Contributor

coderabbitai bot commented Jun 8, 2024

Walkthrough

Walkthrough

The updates introduce two new functions, localizeExtraFieldsComputeLabelCallback and localizeExtraFieldsComputeHelperCallback, across several files to enhance localization of labels and helper texts for device automation schemas. These functions replace existing methods to streamline and improve the handling of extra fields in the Home Assistant UI forms. Additionally, specific key-value pairs related to extra fields are removed from the en.json translations file, simplifying the user interface.

Changes

Files Change Summary
src/data/device_automation.ts Added localizeExtraFieldsComputeLabelCallback and localizeExtraFieldsComputeHelperCallback functions.
src/panels/config/automation/action/types/... Replaced _extraFieldsComputeLabelCallback with new localization functions in HaDeviceAction class.
src/panels/config/automation/condition/types/... Replaced _extraFieldsComputeLabelCallback with new localization functions in HaDeviceCondition class.
src/panels/config/automation/trigger/types/... Replaced _extraFieldsComputeLabelCallback with new localization functions in HaDeviceTrigger class.
src/translations/en.json Removed specific key-value pairs related to extra fields for device triggers, conditions, and actions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI_Form
    participant HaDeviceAction
    participant Localization_Functions
    participant HomeAssistant

    User->>UI_Form: Opens Automation Action Form
    UI_Form->>HaDeviceAction: Initialize Form
    HaDeviceAction->>Localization_Functions: Call localizeExtraFieldsComputeLabelCallback
    Localization_Functions->>HomeAssistant: Fetch localized label
    HomeAssistant-->>Localization_Functions: Return localized label
    Localization_Functions-->>HaDeviceAction: Return localized label
    HaDeviceAction->>UI_Form: Display localized label

    User->>UI_Form: Opens Automation Condition Form
    UI_Form->>HaDeviceCondition: Initialize Form
    HaDeviceCondition->>Localization_Functions: Call localizeExtraFieldsComputeHelperCallback
    Localization_Functions->>HomeAssistant: Fetch localized helper text
    HomeAssistant-->>Localization_Functions: Return localized helper text
    Localization_Functions-->>HaDeviceCondition: Return localized helper text
    HaDeviceCondition->>UI_Form: Display localized helper text

    User->>UI_Form: Opens Automation Trigger Form
    UI_Form->>HaDeviceTrigger: Initialize Form
    HaDeviceTrigger->>Localization_Functions: Call localizeExtraFieldsComputeLabelCallback
    Localization_Functions->>HomeAssistant: Fetch localized label
    HomeAssistant-->>Localization_Functions: Return localized label
    Localization_Functions-->>HaDeviceTrigger: Return localized label
    HaDeviceTrigger->>UI_Form: Display localized label
Loading

Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 9ae3a82 and c64fe6f.

Files selected for processing (5)
  • src/data/device_automation.ts (1 hunks)
  • src/panels/config/automation/action/types/ha-automation-action-device_id.ts (3 hunks)
  • src/panels/config/automation/condition/types/ha-automation-condition-device.ts (3 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts (3 hunks)
  • src/translations/en.json (3 hunks)
Additional context used
Biome
src/panels/config/automation/action/types/ha-automation-action-device_id.ts

[error] 49-49: Unexpected any. Specify a different type. (lint/suspicious/noExplicitAny)

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


[error] 52-52: Forbidden non-null assertion. (lint/style/noNonNullAssertion)

src/panels/config/automation/condition/types/ha-automation-condition-device.ts

[error] 49-49: Unexpected any. Specify a different type. (lint/suspicious/noExplicitAny)

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


[error] 52-52: Forbidden non-null assertion. (lint/style/noNonNullAssertion)

src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts

[error] 56-56: Forbidden non-null assertion. (lint/style/noNonNullAssertion)

src/data/device_automation.ts

[error] 212-212: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 232-232: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 250-250: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 167-167: Reassigning a function parameter is confusing. (lint/style/noParameterAssign)

The parameter is declared here:

Use a local variable instead.


[error] 170-170: Reassigning a function parameter is confusing. (lint/style/noParameterAssign)

The parameter is declared here:

Use a local variable instead.

src/translations/en.json

[error] 380-380: The key image was already declared. (lint/nursery/noDuplicateJsonKeys)

This where a duplicated key was declared again.

If a key is defined multiple times, only the last definition takes effect. Previous definitions are ignored.

Additional comments not posted (10)
src/panels/config/automation/action/types/ha-automation-action-device_id.ts (2)

15-16: The addition of localizeExtraFieldsComputeLabelCallback and localizeExtraFieldsComputeHelperCallback imports from ../../../../../data/device_automation is a good move towards centralizing localization logic. This should make future maintenance and updates more straightforward.


89-95: The integration of localizeExtraFieldsComputeLabelCallback and localizeExtraFieldsComputeHelperCallback into the ha-form component for dynamic label and helper text computation is a significant improvement. This ensures that the UI dynamically adapts to different locales, enhancing user experience for non-English speakers.

src/panels/config/automation/condition/types/ha-automation-condition-device.ts (2)

15-16: Good implementation of the new localization callbacks. Centralizing these functions helps in maintaining consistency across different modules.


89-95: Proper use of the new localization functions in the UI component. This change should make the interface more intuitive and accessible by providing localized labels and helpers based on the user's settings.

src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts (2)

17-18: The addition of localization functions for computing labels and helpers is a positive change, ensuring consistency and maintainability across different parts of the application.


93-99: Effective use of localization callbacks in the form component. This enhancement will improve the user experience by providing context-specific labels and helpers in the user's preferred language.

src/data/device_automation.ts (1)

252-267: The implementation of localizeExtraFieldsComputeLabelCallback and localizeExtraFieldsComputeHelperCallback functions is a crucial enhancement for supporting multiple languages. This change will help in providing a more localized experience to the users.

src/translations/en.json (3)

Line range hint 1-1: The updated description for device triggers enhances clarity and user guidance.


Line range hint 2-2: The updated description for device conditions is well-phrased and supports the goal of improving user experience.


Line range hint 3-3: The addition of the "no_device" description for actions without a device is a thoughtful inclusion, enhancing the robustness of user interactions.


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

Outside diff range comments (7)
src/panels/config/automation/action/types/ha-automation-action-device_id.ts (2)

Line range hint 49-49: Consider specifying a more explicit type instead of any for better type safety and to avoid potential runtime errors.

- const extraFieldsData: Record<string, any> = {};
+ const extraFieldsData: Record<string, unknown> = {};

Line range hint 52-52: Usage of non-null assertions can lead to runtime errors if not handled properly. It's safer to handle potential null or undefined values explicitly.

- extraFieldsData![item.name] = action[item.name];
+ if (extraFieldsData && item.name in action) {
+   extraFieldsData[item.name] = action[item.name];
+ }
src/panels/config/automation/condition/types/ha-automation-condition-device.ts (2)

Line range hint 49-49: To enhance type safety, consider replacing any with a more specific type. This helps in catching potential bugs during compile time.

- const extraFieldsData: Record<string, any> = {};
+ const extraFieldsData: Record<string, unknown> = {};

Line range hint 52-52: Avoid using non-null assertions. Instead, check for null or undefined values explicitly to prevent possible runtime errors.

- extraFieldsData![item.name] = condition[item.name];
+ if (extraFieldsData && item.name in condition) {
+   extraFieldsData[item.name] = condition[item.name];
+ }
src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts (1)

Line range hint 56-56: It's recommended to avoid non-null assertions. Consider handling potential null values explicitly to ensure robustness.

- const extraFieldsData = computeInitialHaFormData(capabilities.extra_fields);
+ const extraFieldsData = capabilities.extra_fields ? computeInitialHaFormData(capabilities.extra_fields) : {};
src/data/device_automation.ts (2)

Line range hint 212-212: Avoid using non-null assertions as they can lead to runtime errors if the assumptions about non-nullability prove incorrect. Consider adding explicit null checks.

- return entityIdA === entityIdB;
+ return entityIdA !== null && entityIdB !== null && entityIdA === entityIdB;

Also applies to: 232-232, 250-250

Tools
Biome

[error] 250-250: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


Line range hint 167-167: Reassigning function parameters can lead to confusing code. Use a local variable instead.

- entityIdA = entityRegistryByEntityId(entityRegistry)[entityIdA].id;
+ const regIdA = entityRegistryByEntityId(entityRegistry)[entityIdA].id;
- entityIdB = entityRegistryByEntityId(entityRegistry)[entityIdB].id;
+ const regIdB = entityRegistryByEntityId(entityRegistry)[entityIdB].id;

Also applies to: 170-170

Tools
Biome

[error] 250-250: Forbidden non-null assertion. (lint/style/noNonNullAssertion)

@farmio farmio added the localization Translations and Localization label Jun 11, 2024
@bramkragten bramkragten merged commit 067e179 into home-assistant:dev Jun 12, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed localization Translations and Localization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants