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

Migrate trigger platform key to trigger #22054

Merged
merged 4 commits into from
Sep 25, 2024
Merged

Migrate trigger platform key to trigger #22054

merged 4 commits into from
Sep 25, 2024

Conversation

bramkragten
Copy link
Member

@bramkragten bramkragten commented Sep 22, 2024

Proposed change

For home-assistant/core#124357

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

    • Standardized trigger terminology by replacing "platform" with "trigger" across various components and configurations, enhancing clarity in automation settings.
    • Introduced migration functions to facilitate seamless transitions from old trigger definitions to the new structure.
  • Bug Fixes

    • Improved processing of trigger values in automation components, ensuring accurate event handling.
  • Documentation

    • Updated internal documentation to reflect changes in trigger definitions and usage throughout the system.

Copy link
Contributor

coderabbitai bot commented Sep 22, 2024

Walkthrough

Walkthrough

The changes involve a significant renaming of the platform property to trigger across various files in the codebase related to automation and trigger definitions. This update affects multiple interfaces, classes, and configuration objects, standardizing the terminology used for defining triggers. Additionally, new methods for migrating existing configurations to the new structure have been introduced, ensuring compatibility with the updated naming convention.

Changes

Files Change Summary
gallery/src/pages/automation/describe-action.ts, gallery/src/pages/automation/describe-trigger.ts, src/components/device/ha-device-trigger-picker.ts, src/components/ha-selector/ha-selector-action.ts, src/components/ha-selector/ha-selector-template.ts, src/components/ha-selector/ha-selector-trigger.ts The platform key has been renamed to trigger in various automation and trigger definitions, enhancing consistency in terminology across the codebase.
src/data/automation.ts, src/data/device_automation.ts, src/data/script.ts, src/data/automation_i18n.ts The platform property has been deprecated in favor of a new trigger property across multiple interfaces and types, with migration functions introduced to facilitate this transition.
src/panels/config/automation/action/ha-automation-action-row.ts, src/panels/config/automation/action/ha-automation-action.ts, src/panels/config/automation/structs.ts, src/panels/config/automation/trigger/ha-automation-trigger-row.ts, src/panels/config/automation/trigger/ha-automation-trigger.ts Changes include the renaming of platform to trigger in configuration objects and event handling methods, ensuring uniformity in trigger management.
src/panels/config/automation/trigger/types/*.ts (multiple files) The defaultConfig method in various trigger classes has been updated to replace the platform property with trigger, aligning the configuration structure with the new naming convention.
gallery/src/data/traces/basic_trace.ts, gallery/src/data/traces/motion-light-trace.ts The platform key has been updated to trigger in trace definitions and trigger configurations, reflecting the new naming convention across different contexts.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 206d5fd and ab673a1.

Files selected for processing (1)
  • src/data/device_automation.ts (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/data/device_automation.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.

@github-actions github-actions bot added the Design Related to Home Assistant design gallery label Sep 22, 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: 7

Outside diff range and nitpick comments (1)
src/data/automation.ts (1)

72-74: Consider proper property handling to avoid de-optimizing object shapes

Combining the concerns regarding the use of delete and setting a property to undefined, it might be beneficial to rethink how deprecated properties are managed. Consider whether it's necessary to modify the object in place or if creating a new, sanitized object would be more efficient.

Consider refactoring the migration function as follows:

export const migrateAutomationTrigger = (
  trigger: Trigger | Trigger[]
): Trigger | Trigger[] => {
  if (Array.isArray(trigger)) {
    return trigger.map(migrateAutomationTrigger) as Trigger[];
  }

  if ("platform" in trigger && !("trigger" in trigger)) {
    return {
      ...trigger,
      trigger: trigger.platform,
      platform: undefined, // or omit this line to leave the property as is
    };
  }

  return trigger;
};

Also applies to: 396-399

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 597bf5d and bd30e12.

Files selected for processing (33)
  • gallery/src/pages/automation/describe-action.ts (1 hunks)
  • gallery/src/pages/automation/describe-trigger.ts (1 hunks)
  • src/components/device/ha-device-trigger-picker.ts (1 hunks)
  • src/components/ha-selector/ha-selector-action.ts (2 hunks)
  • src/components/ha-selector/ha-selector-template.ts (1 hunks)
  • src/components/ha-selector/ha-selector-trigger.ts (2 hunks)
  • src/data/automation.ts (4 hunks)
  • src/data/automation_i18n.ts (13 hunks)
  • src/data/device_automation.ts (2 hunks)
  • src/data/script.ts (2 hunks)
  • src/panels/config/automation/action/ha-automation-action-row.ts (3 hunks)
  • src/panels/config/automation/action/ha-automation-action.ts (2 hunks)
  • src/panels/config/automation/structs.ts (1 hunks)
  • src/panels/config/automation/trigger/ha-automation-trigger-row.ts (7 hunks)
  • src/panels/config/automation/trigger/ha-automation-trigger.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-conversation.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-geo_location.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-mqtt.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-state.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-sun.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-tag.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-template.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time_pattern.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-webhook.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-zone.ts (1 hunks)
  • src/panels/config/tags/ha-config-tags.ts (1 hunks)
Additional context used
Biome
src/data/automation.ts

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (42)
src/panels/config/automation/structs.ts (1)

4-4: LGTM! The property name change aligns with the standardization effort.

The renaming of the platform property to trigger in the baseTriggerStruct object is a semantic update that better reflects the purpose of the property. This change is part of a larger effort to standardize the terminology used for defining triggers in the automation configuration.

Please ensure that all references to the platform property in the baseTriggerStruct are updated to use trigger instead throughout the codebase.

src/components/device/ha-device-trigger-picker.ts (1)

29-29: LGTM!

The change from platform: "device" to trigger: "device" in the object returned by the constructor is consistent with the PR objective and the new naming convention. The change is localized and does not affect the rest of the class.

src/components/ha-selector/ha-selector-action.ts (2)

21-26: LGTM!

The new _actions method is a good addition to the HaActionSelector class. It efficiently processes the action using memoizeOne for optimization and handles the case when no action is provided by returning an empty array. Calling migrateAutomationAction ensures that the action is migrated to the latest format before being processed further.


33-33: LGTM!

Updating the render method to use the new _actions method is a good change. It ensures that the actions are processed efficiently and consistently before being passed to the ha-automation-action component.

src/components/ha-selector/ha-selector-trigger.ts (3)

3-4: LGTM!

The added imports are relevant and necessary for the changes introduced in this file.


21-26: LGTM!

The _triggers method is a good addition to the HaTriggerSelector class. It encapsulates the logic of migrating triggers and provides a consistent way to access migrated triggers within the class. The use of memoizeOne for caching the results is a nice optimization.


33-33: LGTM!

The change in the render method to use the _triggers method is a good improvement. It ensures that the triggers are always migrated before being passed to the ha-automation-trigger component, providing a consistent and reliable way to handle trigger migrations.

src/panels/config/automation/trigger/types/ha-automation-trigger-mqtt.ts (1)

24-24: LGTM! The property renaming aligns with the PR objective.

The change from platform to trigger in the default configuration object is consistent with the goal of migrating the trigger platform key to a new trigger structure.

Note that this is a breaking change for any code that relies on the platform property. Existing code will need to be updated to use the trigger property instead.

src/panels/config/automation/trigger/types/ha-automation-trigger-time_pattern.ts (1)

25-25: LGTM!

The change from platform to trigger in the defaultConfig method aligns with the PR objective of migrating the trigger platform key to trigger. It maintains consistency with the updated naming convention across the codebase, as mentioned in the AI-generated high-level summary.

src/panels/config/automation/trigger/types/ha-automation-trigger-tag.ts (1)

23-23: Verify the usage of the defaultConfig method in the codebase.

The change in the return value of the defaultConfig method is consistent with the PR objective of migrating the trigger platform key to a new trigger structure. The change is isolated to the return value and does not affect the rest of the class implementation.

Run the following script to verify the usage of the defaultConfig method:

Verification successful

Usage of defaultConfig method verified.

The HaTagTrigger.defaultConfig method is used appropriately in the codebase, and no residual references to the old configuration structure were found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `defaultConfig` method in the codebase.

# Test: Search for the method usage. Expect: No occurrences of the old structure.
rg --type typescript -A 5 $'HaTagTrigger\.defaultConfig'

Length of output: 92


Script:

#!/bin/bash
# Description: Verify the usage of the `defaultConfig` method in the codebase.

# Test: Search for the method usage using the correct file type or a glob pattern.
rg --type ts -A 5 'HaTagTrigger\.defaultConfig'

Length of output: 462

src/components/ha-selector/ha-selector-template.ts (1)

10-10: LGTM!

The change to the WARNING_STRINGS constant is consistent with the PR objective of migrating the trigger platform key to a new trigger structure. The change is localized and unlikely to introduce any bugs or regressions.

src/panels/config/automation/trigger/types/ha-automation-trigger-sun.ts (1)

48-48: Verify the usage of the trigger property across the codebase.

The renaming of the platform property to trigger in the defaultConfig method looks good and aligns with the PR objective.

To ensure consistency and avoid potential bugs, please verify that all references to the platform property have been updated to trigger across the codebase, especially in the following areas:

  • Configuration schemas and default values
  • Automation and trigger-related components
  • Documentation and examples

You can use the following script to search for occurrences of the platform property in TypeScript files:

Review the search results and ensure that all relevant occurrences have been updated to trigger.

src/panels/config/automation/trigger/types/ha-automation-trigger-geo_location.ts (1)

48-48: LGTM!

The change is consistent with the PR objective and the AI-generated summary. The property renaming from platform to trigger is a straightforward change and is unlikely to cause any issues.

src/panels/config/automation/trigger/types/ha-automation-trigger-template.ts (1)

26-26: LGTM! The property renaming aligns with the PR objectives.

The change from platform to trigger in the defaultConfig return value is consistent with the migration of the trigger platform key to a new trigger structure, as outlined in the PR objectives and AI summary.

This is a breaking change, but it appears to be an intentional part of the larger migration effort.

src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts (1)

23-23: LGTM!

The renaming of the platform property to trigger in the defaultConfig method aligns with the PR objective and contributes to the standardization of terminology for defining triggers. The change is isolated and does not introduce any functional alterations.

src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts (1)

75-75: LGTM!

The renaming of the platform property to trigger is more semantically accurate and consistent with the PR objective.

Please ensure that all references to the platform property have been updated to trigger across the codebase to maintain consistency.

Run the following script to verify the property usage:

Verification successful

Verification Successful!

All instances of the platform property have been successfully renamed to trigger across the codebase.

  • src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts
  • src/data/automation.ts
  • gallery/src/pages/automation/describe-trigger.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to the `platform` property have been updated to `trigger`.

# Test: Search for the old property name. Expect: No occurrences.
rg --type typescript $'platform:(\s+)?"persistent_notification"'

# Test: Search for the new property name. Expect: Only occurrences of the new property name.
rg --type typescript $'trigger:(\s+)?"persistent_notification"'

Length of output: 200


Script:

#!/bin/bash
# Description: Verify all references to the `platform` property have been updated to `trigger`.

# Test: Search for the old property name. Expect: No occurrences.
rg --type ts 'platform\s*:\s*"persistent_notification"'

# Test: Search for the new property name. Expect: Only occurrences of the new property name.
rg --type ts 'trigger\s*:\s*"persistent_notification"'

Length of output: 400

src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts (1)

23-23: LGTM! Ensure all necessary updates are made to consuming code.

The change from platform to trigger is consistent with the overall migration effort and does not introduce any logical errors.

However, this is a breaking change for any code that relies on the platform property being set in the default config. Please ensure that all necessary updates are made to any code that consumes the default config and expects the platform property.

Run the following script to verify all occurrences of defaultConfig usage:

Verification successful

Verification Successful: platform property correctly replaced with trigger.

The platform property in ha-automation-trigger-time.ts has been successfully updated to trigger. No instances of platform property access remain in this file.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all occurrences of `defaultConfig` usage have been updated.

# Test: Search for `defaultConfig` usage. Expect: No occurrences of `platform` property access.
rg --type typescript $'HaTimeTrigger\.defaultConfig' -A 5

Length of output: 93


Script:

#!/bin/bash
# Description: Verify all occurrences of `defaultConfig` usage have been updated.

# Test: Search for `defaultConfig` usage. Expect: No occurrences of `platform` property access.
rg --type ts 'HaTimeTrigger\.defaultConfig' -A 5

Length of output: 472


Script:

#!/bin/bash
# Description: Check for any remaining occurrences of the `platform` property in the codebase.

# Test: Search for the `platform` property in all TypeScript files. Expect: No matches.
rg --type ts 'platform' -A 2

Length of output: 19092

gallery/src/pages/automation/describe-trigger.ts (2)

25-26: LGTM!

The change from platform to trigger is consistent with the goal of standardizing trigger definitions. The modification looks good.


28-28: Looks good!

The renaming of platform to trigger has been applied consistently across all the trigger definitions in the triggers array and the initialTrigger object. This change aligns with the goal of standardizing the terminology used for defining triggers.

The modifications have been made correctly and there are no apparent issues with the logic or syntax.

Also applies to: 33-33, 35-35, 41-45, 47-47, 52-55, 57-57, 60-60, 64-64

src/panels/config/automation/trigger/types/ha-automation-trigger-zone.ts (1)

28-28: LGTM! Ensure dependent code is updated.

Renaming the property from platform to trigger aligns with the trigger definition structure and improves consistency. This change enhances code readability.

Verify that any code depending on the old platform property has been updated to use trigger. Run the following script to find any potential occurrences:

gallery/src/pages/automation/describe-action.ts (1)

51-53: LGTM! The change aligns with the PR objective.

The modification of the key from platform to trigger for defining the trigger within the wait condition is consistent with the goal of migrating the trigger platform key across the codebase.

Please ensure that:

  1. Existing configurations and code that rely on the platform key for defining triggers within wait conditions are updated to use trigger instead.
  2. The migration process is carefully managed to maintain compatibility, as this is a breaking change.
  3. The input_boolean.toggle_1 entity used in the trigger definition exists in the actual codebase (it's not present in the provided ENTITIES array in this file).
src/panels/config/automation/trigger/types/ha-automation-trigger-conversation.ts (1)

29-29: LGTM! The property name change aligns with the new naming convention.

The change from platform to trigger in the defaultConfig method is part of the larger refactor to standardize the terminology used for defining triggers. The functionality remains the same, as the value of "conversation" is preserved.

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

43-43: LGTM!

The changes in the HaDeviceTrigger class are consistent and align with the renaming of the platform property to trigger. The changes are localized to this class and do not introduce any logical errors or bugs.

Also applies to: 158-158

src/panels/config/automation/trigger/types/ha-automation-trigger-webhook.ts (1)

41-41: LGTM!

The change from platform to trigger aligns with the PR objective of migrating the trigger platform key to a new trigger structure.

Verify that all the code referencing this configuration has been updated to use the new trigger key instead of platform. Run the following script to search for any remaining references to the old key:

Verification successful

Verified!

All references to the old platform key have been successfully removed and replaced with trigger. No remaining instances of platform: "webhook" were found in the TypeScript files.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining references to the old `platform` key. Expect: No results.

# Test: Search for the old key
rg --type typescript $'platform: "webhook"'

Length of output: 79


Script:

#!/bin/bash
# Description: Search for any remaining references to the old `platform` key in TypeScript files. Expect: No results.

# Test: Search for the old key in .ts and .tsx files
rg --glob "*.ts" --glob "*.tsx" 'platform: "webhook"'

Length of output: 55

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

52-52: Update to default config looks good, but verify dependent code.

Updating the default configuration to use the trigger property instead of platform is consistent with the previous change. However, please ensure that any code relying on the defaultConfig method is updated to expect the trigger property in the returned object.

Run the following script to verify the usage of the defaultConfig method:

#!/bin/bash
# Description: Verify code relying on the `defaultConfig` method is updated to expect the `trigger` property.

# Test: Search for usage of the `defaultConfig` method. Expect: Only occurrences handling the `trigger` property.
rg --type typescript $'HaStateTrigger\\.defaultConfig\\(\\)'

32-32: Renaming property is fine, but verify all references are updated.

The change from platform to trigger is a good refactor for consistency. However, please ensure that all references to the platform property have been updated to trigger across the codebase to avoid any inconsistencies or errors.

Run the following script to verify the property usage:

Verification successful

All references to platform have been successfully updated to trigger.

No remaining instances of the platform property were found in the TypeScript files.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to the `platform` property have been updated to `trigger`.

# Test: Search for the `platform` property usage. Expect: No occurrences found.
rg --type typescript $'platform: literal\\("state"\\)'

Length of output: 93


Script:

#!/bin/bash
# Description: Verify that all references to the `platform` property have been updated to `trigger` in TypeScript files.

# Use ast-grep to search for any usage of the `platform` property in TypeScript files.
ast-grep --lang typescript --pattern 'platform: $_'

Length of output: 53

src/data/device_automation.ts (1)

94-94: Looks good!

Updating the deviceAutomationIdentifiers array to include "trigger" instead of "platform" keeps it in sync with the DeviceTrigger type change.

This ensures that the "trigger" property is correctly included in comparisons made by the deviceAutomationsEqual function.

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

133-133: LGTM!

The change correctly renames the platform property to trigger when accessing the clipboard item, aligning with the overall renaming across the codebase. The use of the optional chaining operator ?. ensures safe access to the nested properties.


142-144: LGTM!

The changes correctly rename the platform variable to trigger, update the custom element retrieval string, and cast the variable from value as Trigger["trigger"]. These changes align with the overall renaming of platform to trigger across the codebase.

src/panels/config/automation/action/ha-automation-action.ts (1)

246-246: Simplification of the assignment logic looks good, but verify compatibility.

The change simplifies the assignment of the action value by directly assigning ev.detail.value to newValue, eliminating the conditional logic that involved the migrateAutomationAction function.

However, please verify that removing the migration step does not lead to any compatibility issues with existing automation configurations. Run the following script to check for potential issues:

src/panels/config/tags/ha-config-tags.ts (1)

216-216: LGTM!

The change to the trigger property in the _handleAutomationClick method is consistent with the PR objective of migrating the trigger platform key to a new trigger structure. The update from platform: "tag" to trigger: "tag" aligns with the broader changes mentioned in the AI-generated summary.

src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts (1)

242-242: Verify the impact of renaming platform to trigger in the codebase.

The change aligns with the PR objective and improves clarity. However, ensure that all references to the platform key in trigger configurations have been updated accordingly.

Run the following script to verify the usage of the platform key in trigger configurations:

Verification successful

All instances of the platform key in trigger configurations have been successfully renamed to trigger. No remaining references to platform were found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to the `platform` key in trigger configurations have been updated to `trigger`.

# Test: Search for the `platform` key usage in trigger configurations. Expect: No occurrences of `platform` key.
rg --type typescript $'"platform":\\s*"numeric_state"' src/data/automation.ts

Length of output: 112


Script:

#!/bin/bash
# Description: Verify all references to the `platform` key in trigger configurations have been updated to `trigger`.

# Test: Search for the `platform` key usage in trigger configurations across TypeScript files. Expect: No occurrences of `platform` key.

rg --type ts '"platform":\s*"numeric_state"' src/data/automation.ts

Length of output: 69

src/data/script.ts (1)

484-487: LGTM!

The new conditional block correctly handles the migration of the wait_for_trigger action type by calling migrateAutomationTrigger on the wait_for_trigger property. This change is consistent with the existing migration logic for other action types and does not appear to introduce any issues.

src/panels/config/automation/action/ha-automation-action-row.ts (3)

58-58: LGTM!

The import statement for the migrateAutomationAction function is correctly added.


80-80: LGTM!

The import statement for the ha-automation-action-sequence component is correctly added.


568-570: LGTM!

The _onYamlChange method is correctly updated to call migrateAutomationAction on the ev.detail.value before firing the value-changed event. This ensures that the automation action is in the new format when the event is fired.

src/data/automation_i18n.ts (1)

Line range hint 106-701: Standardized trigger terminology. LGTM!

The changes consistently replace trigger.platform with trigger.trigger across all trigger types. This standardizes the terminology used for defining triggers, aligning with the PR objective of migrating the trigger platform key to a new trigger structure.

The changes are unlikely to introduce any functional issues as the core logic remains the same.

src/data/automation.ts (3)

72-74: Deprecation Warning: Ensure consistent use of the new trigger property

The BaseTrigger interface now deprecates the platform property in favor of trigger. Confirm that all implementations and usages across the codebase are updated to use trigger to prevent potential mismatches or errors.


Line range hint 81-180: Consistent Update to trigger Property in Trigger Interfaces

All trigger interfaces correctly replace the platform property with the trigger property, ensuring consistency across the codebase. This aids in standardizing the trigger configuration.


387-402: Confirm recursive handling of triggers in migrateAutomationTrigger function

The migrateAutomationTrigger function recursively migrates triggers from using platform to trigger. Ensure that this function correctly handles all possible trigger configurations, including nested arrays, to prevent any unexpected behavior during runtime.

Run the following script to check the migration function with various trigger configurations:

Verification successful

migrateAutomationTrigger Function Correctly Handles All Trigger Configurations

After reviewing all usages of migrateAutomationTrigger, no instances of improper or deeply nested trigger configurations were found. The function handles both single triggers and arrays of triggers recursively as intended.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that migrateAutomationTrigger handles triggers correctly.

# Test: Search for usages of migrateAutomationTrigger. Expect: Proper handling of different trigger structures.
rg 'migrateAutomationTrigger' -A 3 -B 3

Length of output: 4000


Script:

#!/bin/bash
# Description: Verify that migrateAutomationTrigger handles nested trigger configurations correctly.

# Search for migrateAutomationTrigger calls with array arguments
rg 'migrateAutomationTrigger\(\s*\[' -A 2 -B 2

# Search for migrateAutomationTrigger calls with nested arrays
rg 'migrateAutomationTrigger\(\s*\[\s*\[' -A 2 -B 2

Length of output: 102

Tools
Biome

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

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

44-48: Imports Updated to Include Migration Utilities

The addition of Trigger, migrateAutomationTrigger, and subscribeTrigger from "../../../../data/automation" is appropriate for the migration changes.


78-78: Import preventDefault from Common Utility

Good job importing preventDefault from the common utility instead of defining it locally. This enhances code reuse and maintainability.

src/data/device_automation.ts Show resolved Hide resolved
src/data/automation.ts Show resolved Hide resolved
src/data/automation.ts Show resolved Hide resolved
@@ -144,7 +147,7 @@ export default class HaAutomationTriggerRow extends LitElement {
if (!this.trigger) return nothing;

const supported =
customElements.get(`ha-automation-trigger-${this.trigger.platform}`) !==
customElements.get(`ha-automation-trigger-${this.trigger.trigger}`) !==
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider Renaming trigger.trigger to Improve Readability

Using this.trigger.trigger may be confusing and could reduce code readability. Consider renaming the inner trigger property to type or platform to enhance clarity and avoid redundancy.

Also applies to: 171-171, 339-339, 369-369

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

Outside diff range and nitpick comments (2)
gallery/src/data/traces/motion-light-trace.ts (2)

Line range hint 163-168: Inconsistency in trigger key naming

While the main trigger has been updated to use trigger: "state", there's an inconsistency in the wait_for_trigger array where platform: "state" is still being used. To maintain consistency across all trigger-related configurations, consider updating this as well.

Here's the suggested change:

        {
          wait_for_trigger: [
            {
-              platform: "state",
+              trigger: "state",
              entity_id: "binary_sensor.pauluss_macbook_pro_camera_in_use",
              from: "on",
              to: "off",
            },
          ],
        },

Line range hint 1-191: Consider updating all trigger-related keys in demo files

While the main change has been implemented correctly, there's an inconsistency in the wait_for_trigger section. Given that this is a demo trace file, it might be beneficial to update all occurrences of platform to trigger for consistency, even in demo files. This would ensure that developers referring to these demo traces see the most up-to-date trigger structure throughout.

Consider doing a thorough check of all demo and example files to ensure complete consistency with the new trigger structure.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bd30e12 and 5337a96.

Files selected for processing (3)
  • gallery/src/data/traces/basic_trace.ts (1 hunks)
  • gallery/src/data/traces/motion-light-trace.ts (1 hunks)
  • gallery/src/pages/automation/editor-trigger.ts (1 hunks)
Additional comments not posted (2)
gallery/src/pages/automation/editor-trigger.ts (1)

114-114: LGTM! Verify consistency across the codebase.

The change from platform to trigger aligns with the PR objective of migrating the trigger platform key. However, to ensure a smooth transition:

  1. Check if HaConversationTrigger.defaultConfig (used earlier in this file) needs a similar update.
  2. Verify that this change is consistently applied across the entire codebase, especially in parts that interact with the automation system.

To assist in verifying the consistency of this change, you can run the following script:

This script will help identify any inconsistencies in the migration and highlight areas that might need attention.

Verification successful

Verified! All instances of platform: "conversation" have been successfully migrated to trigger: "conversation".

  • No remaining platform: "conversation" instances found.
  • trigger: "conversation" is consistently used across the codebase.
  • HaConversationTrigger.defaultConfig is appropriately utilized in editor-trigger.ts.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining instances of 'platform: "conversation"' in the codebase
# and instances of the new 'trigger: "conversation"' syntax.

echo "Checking for remaining 'platform: \"conversation\"' instances:"
rg --type typescript --type javascript 'platform:\s*"conversation"'

echo -e "\nChecking for new 'trigger: \"conversation\"' instances:"
rg --type typescript --type javascript 'trigger:\s*"conversation"'

echo -e "\nChecking for HaConversationTrigger.defaultConfig usage:"
rg --type typescript --type javascript 'HaConversationTrigger\.defaultConfig'

Length of output: 716


Script:

#!/bin/bash
# Description: Check for any remaining instances of 'platform: "conversation"' in the codebase
# and instances of the new 'trigger: "conversation"' syntax. Also check for HaConversationTrigger.defaultConfig usage.

echo "Checking for remaining 'platform: \"conversation\"' instances in .ts and .tsx files:"
rg --glob "*.ts" --glob "*.tsx" 'platform:\s*"conversation"'

echo -e "\nChecking for new 'trigger: \"conversation\"' instances in .ts and .tsx files:"
rg --glob "*.ts" --glob "*.tsx" 'trigger:\s*"conversation"'

echo -e "\nChecking for HaConversationTrigger.defaultConfig usage in .ts and .tsx files:"
rg --glob "*.ts" --glob "*.tsx" 'HaConversationTrigger\.defaultConfig'

Length of output: 1272

gallery/src/data/traces/basic_trace.ts (1)

222-224: Confirm consistent application of trigger key replacement

The change from platform to trigger aligns with the PR objective to migrate the trigger platform key. This enhances consistency in the trigger definition structure.

To ensure this change has been applied consistently:

Consider the following to ensure a smooth transition:

  1. Implement a migration strategy for existing configurations using the old platform key.
  2. Update relevant documentation to reflect this change in trigger definition structure.
  3. If not already done, add unit tests to verify correct handling of both old and new trigger definition formats.
Verification successful

Verification Successful: platform key successfully replaced with trigger

All instances of the platform key with the value "state" have been successfully replaced with the trigger key across the codebase. No remaining occurrences of platform were found, ensuring consistency with the PR objectives.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining instances of 'platform' in trigger definitions
rg --type typescript 'platform:\s*"state"' src/
# Search for new instances of 'trigger' key in trigger definitions
rg --type typescript 'trigger:\s*"state"' src/

Length of output: 168


Script:

#!/bin/bash
# Search for any remaining instances of 'platform' in trigger definitions using file extension
rg --glob '*.ts' 'platform:\s*"state"' src/
# Search for new instances of 'trigger' key in trigger definitions using file extension
rg --glob '*.ts' 'trigger:\s*"state"' src/

Length of output: 255

gallery/src/data/traces/motion-light-trace.ts Show resolved Hide resolved
piitaya
piitaya previously approved these changes Sep 24, 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: 1

Outside diff range and nitpick comments (4)
src/data/automation.ts (3)

78-80: LGTM! Consider adding a migration guide.

The changes to the BaseTrigger interface look good. The deprecation of platform and introduction of trigger is a step towards standardizing trigger definitions.

Consider adding a link to a migration guide in the deprecation notice to help users update their code:

-  /** @deprecated Use `trigger` instead */
+  /** @deprecated Use `trigger` instead. See migration guide: [URL] */

411-417: LGTM! Consider adding error handling.

The updates to migrateAutomationConfig look good. They ensure that both triggers and actions are migrated to the new format.

Consider adding error handling to gracefully manage any migration failures:

 if (config.triggers) {
-  config.triggers = migrateAutomationTrigger(config.triggers);
+  try {
+    config.triggers = migrateAutomationTrigger(config.triggers);
+  } catch (error) {
+    console.error("Failed to migrate triggers:", error);
+    // Handle the error appropriately
+  }
 }

 if (config.actions) {
-  config.actions = migrateAutomationAction(config.actions);
+  try {
+    config.actions = migrateAutomationAction(config.actions);
+  } catch (error) {
+    console.error("Failed to migrate actions:", error);
+    // Handle the error appropriately
+  }
 }

Line range hint 1-538: Consider updating documentation and adding migration guides

The changes in this file are part of a significant refactoring to standardize trigger definitions in the automation system. While the code changes look good, it's important to ensure that users can easily migrate their existing automations.

  1. Update the main documentation to reflect the new trigger property usage instead of platform.
  2. Create a comprehensive migration guide that explains:
    • Why this change was made
    • How to update existing automations
    • Any potential breaking changes or edge cases to be aware of
  3. Consider adding a deprecation warning in the runtime that detects old-style automations and points users to the migration guide.

These steps will help ensure a smooth transition for users and minimize potential issues during the migration process.

src/panels/config/automation/ha-automation-editor.ts (1)

Line range hint 1-924: Overall changes look good, but consider documentation updates.

The changes in this file are focused on updating the handling of automation configurations, specifically:

  1. Removing the migrateAutomationConfig import and usage.
  2. Adding an explicit import for substituteBlueprint.
  3. Simplifying the config normalization in the _yamlChanged method.

These changes align well with the PR objectives of migrating the trigger platform key to a new trigger structure. The code looks cleaner and more straightforward.

However, given the significance of these changes in how automation configurations are handled, consider the following:

  1. Update any relevant documentation to reflect the new way of handling automation configurations.
  2. Add comments in the code to explain the rationale behind using normalizeAutomationConfig directly, especially if it differs significantly from the previous migrateAutomationConfig approach.
  3. Consider adding a brief comment near the substituteBlueprint import to explain its purpose, as it seems to play a crucial role in the new configuration handling.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5337a96 and 206d5fd.

Files selected for processing (37)
  • gallery/src/data/traces/basic_trace.ts (1 hunks)
  • gallery/src/data/traces/motion-light-trace.ts (1 hunks)
  • gallery/src/pages/automation/describe-action.ts (1 hunks)
  • gallery/src/pages/automation/describe-trigger.ts (1 hunks)
  • gallery/src/pages/automation/editor-trigger.ts (1 hunks)
  • src/components/device/ha-device-trigger-picker.ts (1 hunks)
  • src/components/ha-selector/ha-selector-action.ts (2 hunks)
  • src/components/ha-selector/ha-selector-template.ts (1 hunks)
  • src/components/ha-selector/ha-selector-trigger.ts (2 hunks)
  • src/data/automation.ts (4 hunks)
  • src/data/automation_i18n.ts (13 hunks)
  • src/data/device_automation.ts (2 hunks)
  • src/data/script.ts (2 hunks)
  • src/panels/config/automation/action/ha-automation-action-row.ts (3 hunks)
  • src/panels/config/automation/action/ha-automation-action.ts (2 hunks)
  • src/panels/config/automation/ha-automation-editor.ts (2 hunks)
  • src/panels/config/automation/structs.ts (1 hunks)
  • src/panels/config/automation/trigger/ha-automation-trigger-row.ts (7 hunks)
  • src/panels/config/automation/trigger/ha-automation-trigger.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-conversation.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-geo_location.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-mqtt.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-state.ts (2 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-sun.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-tag.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-template.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time_pattern.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-webhook.ts (1 hunks)
  • src/panels/config/automation/trigger/types/ha-automation-trigger-zone.ts (1 hunks)
  • src/panels/config/tags/ha-config-tags.ts (1 hunks)
Files skipped from review as they are similar to previous changes (35)
  • gallery/src/data/traces/basic_trace.ts
  • gallery/src/data/traces/motion-light-trace.ts
  • gallery/src/pages/automation/describe-action.ts
  • gallery/src/pages/automation/describe-trigger.ts
  • gallery/src/pages/automation/editor-trigger.ts
  • src/components/device/ha-device-trigger-picker.ts
  • src/components/ha-selector/ha-selector-action.ts
  • src/components/ha-selector/ha-selector-template.ts
  • src/components/ha-selector/ha-selector-trigger.ts
  • src/data/automation_i18n.ts
  • src/data/device_automation.ts
  • src/data/script.ts
  • src/panels/config/automation/action/ha-automation-action-row.ts
  • src/panels/config/automation/action/ha-automation-action.ts
  • src/panels/config/automation/structs.ts
  • src/panels/config/automation/trigger/ha-automation-trigger-row.ts
  • src/panels/config/automation/trigger/ha-automation-trigger.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-calendar.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-conversation.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-geo_location.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-homeassistant.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-mqtt.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-persistent_notification.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-state.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-sun.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-tag.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-template.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-time_pattern.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-webhook.ts
  • src/panels/config/automation/trigger/types/ha-automation-trigger-zone.ts
  • src/panels/config/tags/ha-config-tags.ts
Additional context used
Biome
src/data/automation.ts

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (4)
src/data/automation.ts (2)

87-87: LGTM! Consistent updates across all trigger interfaces.

The changes to replace platform with trigger in all trigger interfaces are consistent and improve the overall structure of the automation system.

Also applies to: 96-96, 102-102, 109-109, 114-114, 124-124, 129-129, 135-135, 142-142, 149-149, 155-155, 162-162, 168-168, 173-173, 179-179, 186-186


422-437: ⚠️ Potential issue

Improve type safety and performance in migrateAutomationTrigger

The new migrateAutomationTrigger function looks good overall, but there are a couple of areas for improvement:

  1. The use of @ts-ignore suppresses TypeScript errors but can mask underlying type issues.
  2. The delete operator can negatively impact performance.

Apply this diff to address these issues:

 export const migrateAutomationTrigger = (
   trigger: Trigger | Trigger[]
 ): Trigger | Trigger[] => {
   if (Array.isArray(trigger)) {
     return trigger.map(migrateAutomationTrigger) as Trigger[];
   }

   if ("platform" in trigger) {
     if (!("trigger" in trigger)) {
-      // @ts-ignore
-      trigger.trigger = trigger.platform;
+      (trigger as any).trigger = trigger.platform;
     }
-    delete trigger.platform;
+    trigger.platform = undefined;
   }
   return trigger;
 };

These changes improve type safety by using a type assertion instead of @ts-ignore, and enhance performance by setting platform to undefined instead of using delete.

Tools
Biome

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

src/panels/config/automation/ha-automation-editor.ts (2)

642-642: LGTM: Simplified config normalization.

The direct use of normalizeAutomationConfig on ev.detail.value simplifies the code and aligns with the removal of migrateAutomationConfig. This change is consistent with the PR objectives of updating the trigger structure.


Line range hint 1-924: Final thoughts: Changes look good, consider testing and documentation.

The changes in this file successfully update the handling of automation configurations, aligning with the PR objectives. The code is now more streamlined, with the removal of migrateAutomationConfig and the direct use of normalizeAutomationConfig.

To ensure a smooth transition and maintain code quality:

  1. Thoroughly test the new configuration handling process, especially with various types of automation configs.
  2. Update any related documentation or comments in other files that might reference the old migrateAutomationConfig approach.
  3. Consider adding a brief comment in the _yamlChanged method explaining the rationale behind using normalizeAutomationConfig directly.

Overall, good job on simplifying the code while maintaining functionality!

silamon
silamon previously approved these changes Sep 24, 2024
@bramkragten bramkragten merged commit dc940f2 into dev Sep 25, 2024
13 checks passed
@bramkragten bramkragten deleted the trigger-platform branch September 25, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Design Related to Home Assistant design gallery wait for backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants