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 horizontal swing to climate #22043

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

Conversation

gjohansson-ST
Copy link
Member

@gjohansson-ST gjohansson-ST commented Sep 20, 2024

Proposed change

Add horizontal swing mode to ClimateEntity
Core PR: home-assistant/core#125578

Example printscreens

image
With both swing and horizontal_swing it becomes vertical and horizontal.
image
If only swing it remains as swing
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

Release Notes

  • New Features

    • Enhanced thermostat card with updated swing modes and support for horizontal swing settings.
    • Introduced new configuration options for climate control, including horizontal swing modes.
    • Added a user-friendly interface for selecting and customizing horizontal swing modes in climate settings.
  • Bug Fixes

    • Improved handling of unavailable states for climate controls.
  • Documentation

    • Added translations and labels for new horizontal swing mode features.

Copy link
Contributor

coderabbitai bot commented Sep 20, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce new features and modifications to the climate control functionality within the Home Assistant interface. Key updates include the addition of horizontal swing modes for climate entities, enhancements to the thermostat and tile cards, and the introduction of new configuration options. The changes also include updates to the relevant TypeScript types, translations, and UI components to support these new features, ensuring a cohesive integration across the system.

Changes

Files Change Summary
gallery/src/pages/lovelace/thermostat-card.ts Updated ENTITIES and CONFIGS to reflect new swing modes and supported features for the thermostat card, including the addition of swing_horizontal_modes and changes to swing_modes.
gallery/src/pages/lovelace/tile-card.ts Added switch_horizontal_modes and swing_horizontal_mode to ENTITIES, updated supported_features to include ClimateEntityFeature.SWING_HORIZONTAL_MODE.
src/data/climate.ts Introduced swing_horizontal_mode and swing_horizontal_modes properties to ClimateEntity, and added SWING_HORIZONTAL_MODE to ClimateEntityFeature enum.
src/dialogs/more-info/controls/more-info-climate.ts Added support for swing_horizontal_mode in MoreInfoClimate, including a method to handle changes and a UI component for selecting the mode.
src/panels/lovelace/card-features/hui-climate-swing-horizontal-modes-card-feature.ts Created a new component for managing horizontal swing modes in climate control entities, including methods for configuration and rendering.
src/panels/lovelace/card-features/types.ts Added ClimateSwingHorizontalModesCardFeatureConfig interface and updated LovelaceCardFeatureConfig type to include it.
src/panels/lovelace/create-element/create-card-feature-element.ts Added new type "climate-swing-horizontal-modes" to TYPES set.
src/panels/lovelace/editor/config-elements/hui-card-features-editor.ts Updated to include support for "climate-swing-horizontal-modes" in various feature type arrays.
src/panels/lovelace/editor/config-elements/hui-climate-swing-horizontal-modes-card-feature-editor.ts Introduced a new component for editing climate swing horizontal modes, including methods for configuration and rendering.
src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts Added "climate-swing-horizontal-modes" to COMPATIBLE_FEATURES_TYPES.
src/translations/en.json Added a new section for "climate-swing-horizontal-modes" with relevant labels and customization options.

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 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 20, 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: 5

Outside diff range and nitpick comments (3)
src/panels/lovelace/card-features/hui-climate-swing-horizontal-modes-card-feature.ts (3)

73-75: Optimize state updates by checking for changes

In the willUpdate method, this._currentSwingHorizontalMode is updated whenever stateObj changes. However, if the swing_horizontal_mode attribute hasn't changed, it may cause unnecessary updates.

Consider checking if this._currentSwingHorizontalMode differs from this.stateObj.attributes.swing_horizontal_mode before updating:

   if (changedProp.has("stateObj") && this.stateObj) {
-    this._currentSwingHorizontalMode =
-      this.stateObj.attributes.swing_horizontal_mode;
+    const newMode = this.stateObj.attributes.swing_horizontal_mode;
+    if (this._currentSwingHorizontalMode !== newMode) {
+      this._currentSwingHorizontalMode = newMode;
+    }
   }

175-179: Ensure proper event handling with bound methods

In the render method, event listeners use functions that may not be bound to the class instance, potentially causing issues with this context.

Consider using arrow functions or the :: binding syntax to ensure proper context:

   @selected=${this._valueChanged}
   @closed=${stopPropagation}

Alternatively, you can bind the methods in the constructor or use the @eventOptions({ capture: true }) decorator from lit to ensure correct context.


205-205: Simplify static styles definition

The styles property can be defined using the static field instead of a getter method for better readability.

Consider updating the styles definition:

- static get styles() {
-   return cardFeatureStyles;
- }
+ static styles = cardFeatureStyles;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fd9e2b6 and fdd8bbd.

Files selected for processing (11)
  • gallery/src/pages/lovelace/thermostat-card.ts (2 hunks)
  • gallery/src/pages/lovelace/tile-card.ts (1 hunks)
  • src/data/climate.ts (2 hunks)
  • src/dialogs/more-info/controls/more-info-climate.ts (3 hunks)
  • src/panels/lovelace/card-features/hui-climate-swing-horizontal-modes-card-feature.ts (1 hunks)
  • src/panels/lovelace/card-features/types.ts (2 hunks)
  • src/panels/lovelace/create-element/create-card-feature-element.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-card-features-editor.ts (4 hunks)
  • src/panels/lovelace/editor/config-elements/hui-climate-swing-horizontal-modes-card-feature-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts (1 hunks)
  • src/translations/en.json (1 hunks)
Additional context used
Biome
src/panels/lovelace/editor/config-elements/hui-climate-swing-horizontal-modes-card-feature-editor.ts

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (19)
src/data/climate.ts (3)

64-64: LGTM!

The new optional property swing_horizontal_mode is added correctly to the ClimateEntity type. The naming convention, type, and optional nature of the property are consistent with the existing swing_mode property.


65-65: LGTM!

The new optional property swing_horizontal_modes is added correctly to the ClimateEntity type. The naming convention, type, and optional nature of the property are consistent with the existing swing_modes property.


80-80: LGTM!

The new enum value SWING_HORIZONTAL_MODE is added correctly to the ClimateEntityFeature enum. The naming convention and value assignment are consistent with the existing enum values.

src/panels/lovelace/create-element/create-card-feature-element.ts (1)

37-37: LGTM!

The addition of the new type "climate-swing-horizontal-modes" to the TYPES set is a valid change that expands the available types for Lovelace card features. This change enables the creation of card features specifically for controlling horizontal swing modes in climate entities, which aligns with the PR objective of introducing horizontal swing functionality.

The change follows the existing pattern of defining valid types in the TYPES set and appears to be self-contained within this file without introducing any apparent issues.

src/panels/lovelace/card-features/types.ts (2)

64-68: LGTM!

The new interface ClimateSwingHorizontalModesCardFeatureConfig is well-defined and follows the existing conventions in this file. The property names and types are appropriate, and the optional properties provide flexibility in configuration.


148-148: Looks good!

The LovelaceCardFeatureConfig type is correctly updated to include the new ClimateSwingHorizontalModesCardFeatureConfig interface. This makes it a valid configuration option for Lovelace card features.

src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts (1)

38-38: LGTM!

The addition of the "climate-swing-horizontal-modes" feature type to the COMPATIBLE_FEATURES_TYPES array is a straightforward and consistent change that enhances the capabilities of the thermostat card editor. It enables support for horizontal swing modes in climate control settings without introducing any breaking changes or regressions.

gallery/src/pages/lovelace/tile-card.ts (3)

80-80: LGTM!

The addition of the switch_horizontal_modes property to the ENTITIES constant is a valid change. It introduces an array of string values representing different horizontal swing modes for the climate entity.


87-87: Looks good!

The introduction of the swing_horizontal_mode property in the ENTITIES constant is a sensible addition. It allows tracking the currently active horizontal swing mode for the climate entity, with an initial value of "off".


92-92: Approved.

Updating the supported_features property to include ClimateEntityFeature.SWING_HORIZONTAL_MODE is a necessary change. It correctly reflects that the climate entity now supports horizontal swing functionality, aligning with the addition of the switch_horizontal_modes and swing_horizontal_mode properties.

src/panels/lovelace/editor/config-elements/hui-card-features-editor.ts (4)

27-27: LGTM!

The import statement is correctly added to include the new feature.


60-60: Looks good!

The UI_FEATURE_TYPES array is correctly updated to include the new feature type.


91-91: Approved.

The EDITABLES_FEATURE_TYPES set is correctly updated to include the new feature type.


109-110: Looks good to me!

The SUPPORTS_FEATURE_TYPES record is correctly updated to map the new feature type to its corresponding support function.

src/dialogs/more-info/controls/more-info-climate.ts (3)

83-86: LGTM!

The constant is correctly checking for the swing_horizontal_mode feature support and follows the naming convention.


357-409: LGTM!

The conditional block for displaying the swing_horizontal_mode selection menu is implemented correctly. It checks for the necessary conditions, populates the menu with available modes, and disables the menu when appropriate. The code structure and logic is consistent with other similar selection menus in the file.


446-454: LGTM!

The _handleSwingHorizontalmodeChanged function is implemented correctly. It retrieves the new value from the event and calls the service helper with the appropriate arguments to update the swing_horizontal_mode. The function logic is consistent with other similar handler functions in the file.

src/translations/en.json (1)

6215-6224: LGTM!

The new "climate-swing-horizontal-modes" section follows the existing structure and naming conventions of the file. The added keys and values seem appropriate and consistent.

gallery/src/pages/lovelace/thermostat-card.ts (1)

88-92: Ensure 'supported_features' value is correct

The supported_features value has been updated to 553. This value should represent the sum of all supported feature flags for the entity. Please verify that 553 accurately reflects the combined supported features, including the new horizontal swing capabilities.

To verify the calculation of supported_features, run the following script:

If the output matches 553, the supported_features value is correct.

Verification successful

To ensure we have a comprehensive understanding of the supported features, let's search the codebase for any additional climate-related constants or enums that might be relevant to this calculation.


Confirmed: 'supported_features' value is correct

The supported_features value of 553 in the code snippet accurately reflects the sum of the relevant climate entity feature flags. This value corresponds to the following supported features:

  • TARGET_TEMPERATURE (1)
  • FAN_MODE (8)
  • SWING_MODE (32)
  • SWING_HORIZONTAL_MODE (512)

These features are defined in the ClimateEntityFeature enum in src/data/climate.ts, and their sum (1 + 8 + 32 + 512) equals 553, matching the value in the code.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that 'supported_features' value correctly sums the feature flags.

# Climate feature flags:
# SUPPORT_TARGET_TEMPERATURE = 1
# SUPPORT_FAN_MODE = 8
# SUPPORT_SWING_MODE = 32
# SUPPORT_SWING_MODE_HORIZONTAL = 512

# Sum the features supported by the entity:
echo $((1 + 8 + 32 + 512))  # Expected output: 553

Length of output: 14


Script:

#!/bin/bash
# Search for climate-related constants or enums in the codebase
echo "Searching for climate-related constants:"
rg -i "const.*climate" -g "*.ts" -A 5

echo "\nSearching for climate-related enums:"
rg -i "enum.*climate" -g "*.ts" -A 10

echo "\nSearching for SUPPORT_ constants related to climate:"
rg "const SUPPORT_.*=.*;" -g "*.ts" | grep -i "climate"

Length of output: 11311

Comment on lines +88 to +93
swing_modes: ["both", "rangefull", "off"],
swing_mode: "rangefull",
swing_horizontal_modes: ["both", "rangefull", "off"],
swing_horizontal_mode: "both",
supported_features: 553,
Copy link
Contributor

Choose a reason for hiding this comment

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

Verify the validity of 'rangefull' as a swing mode option

It appears that 'rangefull' is used as a swing mode option in both swing_modes and swing_horizontal_modes. Please confirm that 'rangefull' is a valid and recognized swing mode value for the Sensibo device. If it's a typo and should be 'range_full' or another standard value, consider correcting it to ensure compatibility.

Apply this diff if correction is needed:

     swing_modes: ["both", "rangefull", "off"],
     swing_mode: "rangefull",
     swing_horizontal_modes: ["both", "rangefull", "off"],
     swing_horizontal_mode: "both",
     supported_features: 553,
     swing_modes: ["both", "range_full", "off"],
     swing_mode: "range_full",
     swing_horizontal_modes: ["both", "range_full", "off"],
     swing_horizontal_mode: "both",
     supported_features: 553,

Committable suggestion was skipped due to low confidence.

gallery/src/pages/lovelace/thermostat-card.ts Show resolved Hide resolved
stateObj?.attributes.swing_horizontal_modes || [];
}
if (!customize_modes && config.swing_horizontal_modes) {
delete config.swing_horizontal_modes;
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using the delete operator for better performance

Using the delete operator can impact performance. Consider setting the property to undefined instead.

Apply this diff to fix the issue:

-      delete config.swing_horizontal_modes;
+      config.swing_horizontal_modes = undefined;
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
delete config.swing_horizontal_modes;
config.swing_horizontal_modes = undefined;
Tools
Biome

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

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


private async _valueChanged(ev: CustomEvent) {
const swingHorizontalMode =
(ev.detail as any).value ?? ((ev.target as any).value as string);
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance type safety by defining the event detail type

Currently, the code uses type assertions with as any to access ev.detail.value and ev.target.value. This can be improved by defining the event type to enhance type safety and avoid potential runtime errors.

Consider defining a custom event interface and using it to properly type the event parameter. For example:

+ interface ValueChangedEvent extends CustomEvent {
+   detail: { value: string };
+ }

- private async _valueChanged(ev: CustomEvent) {
+ private async _valueChanged(ev: ValueChangedEvent) {
     const swingHorizontalMode =
-      (ev.detail as any).value ?? ((ev.target as any).value as string);
+      ev.detail.value;

Committable suggestion was skipped due to low confidence.

Comment on lines +106 to +107
this._currentSwingHorizontalMode = oldSwingHorizontalMode;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Provide user feedback on failed service call

In the _valueChanged method, if the service call fails, the user may not be aware that their action did not succeed. Providing user feedback in case of an error would enhance user experience.

Consider adding a notification or error message to inform the user:

   try {
     await this._setMode(swingHorizontalMode);
   } catch (err) {
     this._currentSwingHorizontalMode = oldSwingHorizontalMode;
+    showToast(this, {
+      message: this.hass!.localize(
+        "ui.dialogs.more_info_control.climate.swing_mode_set_failed"
+      ),
+    });
   }

Make sure to import the necessary showToast function and define the appropriate localization string.

Committable suggestion was skipped due to low confidence.

@matthiasdebaat
Copy link
Collaborator

When we introduce Horizontal swing mode, does that make the other one Vertical swing mode? To make the label shorter, we could drop the word mode: Horizontal swing and Vertical swing. I wouldn't drop swing, as that gives context about the feature.

@gjohansson-ST
Copy link
Member Author

When we introduce Horizontal swing mode, does that make the other one Vertical swing mode?

Yes

To make the label shorter, we could drop the word mode: Horizontal swing and Vertical swing. I wouldn't drop swing, as that gives context about the feature.

I think that makes sense.
So when both swing modes are available they will be Vertical swing and Horizontal swing. When only one is available (it can be combined both vertical and horizontal or only one of them) it will be called Swing

Would it make sense to update fan, preset etc. in a separate PR then to align the language (or we keep mode here)?

@matthiasdebaat
Copy link
Collaborator

Would it make sense to update fan, preset etc. in a separate PR then to align the language (or we keep mode here)?

I like that, I don't see any concerns if we drop mode. This also gives us one option with Mode:
CleanShot 2024-10-29 at 14 27 35@2x

@gjohansson-ST
Copy link
Member Author

I like that, I don't see any concerns if we drop mode. This also gives us one option with Mode

I have introduced it with this PR but maybe should be done in a preliminary PR before introduced here.
The screenshots in the PR description are updated with the new strings.

@bramkragten bramkragten self-assigned this Nov 20, 2024
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.

4 participants