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

Support for Homebridge v2 #920

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Support for Homebridge v2 #920

wants to merge 4 commits into from

Conversation

itavero
Copy link
Owner

@itavero itavero commented Aug 12, 2024

In this MR I'm preparing support for Homebridge v2 (see #904 and the Homebridge wiki).
So far, I've only quickly ran it locally and made the changes needed based on the warnings and errors I saw in the logs.

Of course, this was limited to my own set of Zigbee devices, so more testing is probably needed.

Things to do before putting this in the beta version:

  • Test if control and such still works as expected (sanity check; don't expect any issues).
  • Other uses of Characteristic.setProps where the valid values are being limited but no valid default value has been set first.

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced handling of accessory names for better formatting and sanitization.
    • Updated configuration options for improved clarity in documentation.
  • Bug Fixes

    • Adjusted logging levels for deprecated warnings to improve error visibility.
    • Ensured compatibility with newer versions of dependencies.
  • Documentation

    • Expanded documentation for plugin configuration, detailing various options and features.
  • Tests

    • Updated tests to reflect the removal of the experimental feature flag for light devices.

@itavero itavero self-assigned this Aug 12, 2024
Copy link

Copy link
Contributor

🆗 Published SonarCloud and code coverage data.

@itavero
Copy link
Owner Author

itavero commented Dec 19, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Dec 19, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Dec 19, 2024

Walkthrough

This pull request introduces several enhancements to a Zigbee2MQTT Homebridge plugin, focusing on configuration improvements, code refactoring, and compatibility with Homebridge v2. Key changes include updating dependency versions, removing experimental feature flags, sanitizing accessory names, and improving characteristic handling for devices like lights and thermostats. The modifications aim to streamline the plugin's functionality, enhance error handling, and prepare for future Homebridge versions.

Changes

File Change Summary
.vscode/settings.json Disabled Markdown file formatting on save
CHANGELOG.md Updated with release notes, removed color mode experimental flag
docs/config.md Enhanced documentation for configuration options
package.json Updated Homebridge dependency versions to support v1.6.0 and v2.0.0-beta
src/converters/climate.ts Refactored thermostat characteristic handling
src/converters/light.ts Simplified color mode and adaptive lighting logic
src/experimental.ts Removed EXP_COLOR_MODE experimental flag
src/helpers.ts Added new helper functions for name sanitization and characteristic handling
src/platform.ts Integrated accessory name sanitization
src/platformAccessory.ts Enhanced accessory name handling
test/light.spec.ts Removed experimental feature flag references
test/testHelpers.ts Updated Jest assertion syntax

Sequence Diagram

sequenceDiagram
    participant User
    participant Plugin
    participant Homebridge
    participant Zigbee2MQTT

    User->>Plugin: Configure Plugin
    Plugin->>Homebridge: Register Accessories
    Plugin->>Zigbee2MQTT: Connect and Discover Devices
    Zigbee2MQTT-->>Plugin: Device Information
    Plugin->>Plugin: Sanitize Accessory Names
    Plugin->>Homebridge: Create Services
    Homebridge-->>User: Expose Devices in HomeKit
Loading

Poem

🐰 A Zigbee Rabbit's Delight

Homebridge v2 is here, oh so bright!
Sanitized names, no character's plight
Experimental flags take flight
Our plugin dances with pure delight
Compatibility shining clear and tight! 🏠✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


🪧 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

Copy link

@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

🧹 Nitpick comments (6)
test/testHelpers.ts (1)

218-218: LGTM! Consider updating remaining assertions for consistency.

The update to use toHaveBeenCalledWith aligns with modern Jest practices. However, there are several other assertions in the file still using the older toBeCalledWith syntax.

Consider updating the following assertions to use the newer syntax for consistency:

  • Line 559: toBeCalledWithtoHaveBeenCalledWith
  • Line 571: toBeCalledWithtoHaveBeenCalledWith
  • Line 629: toBeCalledWithtoHaveBeenCalledWith

Example diff:

-expect(this.accessoryMock.queueDataForSetAction).toBeCalledTimes(1).toBeCalledWith(expectedData);
+expect(this.accessoryMock.queueDataForSetAction).toHaveBeenCalledTimes(1).toHaveBeenCalledWith(expectedData);
src/helpers.ts (1)

79-92: Adjust characteristic value if newly provided validValues is empty.
Currently, if validValues is empty, the function silently does nothing. If a device unexpectedly reports no valid values, you may want to fall back to a single default or log a warning.

package.json (1)

93-93: Keep an eye on beta versions.
Using a beta version for development is acceptable for preparing the plugin but ensure there's a plan to upgrade once Homebridge 2.0 is officially released or the beta becomes stable.

src/converters/climate.ts (1)

210-213: Evaluate error-handling coverage.
Ensure that if handleSetTargetState throws or fails for some reason, you handle the callback with an error. Right now, only a success path and a single error message exist.

src/platformAccessory.ts (1)

427-427: Sanitize the user-facing Name.
Replacing invalid or emoji-laden fields is good. Consider logging a warning if you strip unexpected characters from a name that might be user-intended.

CHANGELOG.md (1)

16-18: Consider using "among" instead of "amongst".

The changelog entry clearly documents Homebridge v2 compatibility changes. However, consider using "among" instead of "amongst" as it's more commonly used in modern technical writing.

-- Minor changes to be compatible with the upcoming Homebridge v2 release, amongst others:
++ Minor changes to be compatible with the upcoming Homebridge v2 release, among others:
🧰 Tools
🪛 LanguageTool

[style] ~16-~16: The preposition ‘amongst’ is correct, but some people think that it is old-fashioned or literary. A more frequently used alternative is the preposition “among”.
Context: ...ith the upcoming Homebridge v2 release, amongst others: - In most services where the ...

(AMONGST)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 40136d4 and 2d6473f.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (12)
  • .vscode/settings.json (1 hunks)
  • CHANGELOG.md (1 hunks)
  • docs/config.md (0 hunks)
  • package.json (2 hunks)
  • src/converters/climate.ts (4 hunks)
  • src/converters/light.ts (3 hunks)
  • src/experimental.ts (0 hunks)
  • src/helpers.ts (4 hunks)
  • src/platform.ts (3 hunks)
  • src/platformAccessory.ts (3 hunks)
  • test/light.spec.ts (0 hunks)
  • test/testHelpers.ts (1 hunks)
💤 Files with no reviewable changes (3)
  • src/experimental.ts
  • docs/config.md
  • test/light.spec.ts
✅ Files skipped from review due to trivial changes (1)
  • .vscode/settings.json
🧰 Additional context used
🪛 LanguageTool
CHANGELOG.md

[style] ~16-~16: The preposition ‘amongst’ is correct, but some people think that it is old-fashioned or literary. A more frequently used alternative is the preposition “among”.
Context: ...ith the upcoming Homebridge v2 release, amongst others: - In most services where the ...

(AMONGST)

🔇 Additional comments (16)
src/helpers.ts (3)

14-29: Ensure handling of certain edge cases in sanitizeAccessoryName.
The sanitization logic effectively replaces disallowed characters and trims leading/trailing spaces or apostrophes. However, consider the following:
• What happens if the entire string is made of disallowed characters? This might yield an empty name.
• For user-friendliness, you could optionally default to a placeholder name if the sanitization rules produce an empty string.

Would you like to see a quick test script illustrating how sanitizeAccessoryName behaves with edge-case inputs (like an emoji-only name)?


49-58: Validate type conversions for numeric values.
While clamping the characteristic value within range is correct, ensure that the value is definitely a number. If it's not guaranteed to be numeric, an explicit check could prevent unexpected runtime issues (e.g., passing strings).


69-77: Caution with single-value constraints.
Restricting the characteristic's valid values, minValue, and maxValue all to one number is fine, but be aware that any attempts to set the characteristic to a different value will be rejected. Ensure this is the desired behavior, especially if the device might later support multiple values in a firmware update.

package.json (1)

35-35: Double-check compatibility with Homebridge 2.0.
Specifying "^1.6.0 || ^2.0.0-beta.0" is sensible for bridging versions. Make sure the plugin is tested against both stable 1.x and beta 2.x to catch any potential breaking changes.

src/converters/climate.ts (4)

19-24: Imports for new helpers look correct.
The introduced methods (allowSingleValueForCharacteristic, setValidValuesOnCharacteristic) help maintain consistent handling of characteristic properties.


187-187: Check correct enumeration usage.
The code sets a valid values array for CurrentHeatingCoolingState. Ensure the set of possible states (OFF/HEAT/COOL) is correct for your device’s capabilities.


224-231: Single-value usage for heat-only devices.
Using allowSingleValueForCharacteristic to force Current/Target states to “HEAT” is correct. Confirm that the device does not support OFF or COOL modes to avoid user confusion.


235-238: Locking temperature display to Celsius.
AllowSingleValueForCharacteristic ensures no Fahrenheit option appears in HomeKit. Ensure that’s valid for your region or device.

src/platformAccessory.ts (2)

18-18: Import of sanitizeAccessoryName.
This import integrates well with existing logic. No immediate concerns.


500-500: Sanitize subType-names.
Including the subType as part of the accessory name is excellent for clarity. The sanitizeAccessoryName call ensures we don’t break HomeKit’s rules.

src/converters/light.ts (3)

209-216: LGTM: Color mode filtering logic improves Home.app experience.

The code now properly filters out inactive color information based on the color_mode, preventing potential visual glitches in the Home.app interface.


307-307: LGTM: Using helper function improves code maintainability.

Good refactoring to use the copyExposesRangeToCharacteristic helper function for setting characteristic properties.


Line range hint 314-318: LGTM: Added color temperature to hue/saturation conversion.

The addition of color temperature to hue/saturation monitoring improves the user experience by providing visual feedback when color temperature changes.

CHANGELOG.md (1)

12-13: LGTM: Clear documentation of color mode changes.

The changelog entry properly documents the removal of the experimental flag for color mode and references the related issue.

src/platform.ts (2)

571-574: LGTM: Improved visibility of deprecation warning.

Changing the log level from warning to error for deprecated accessories helps users identify and plan for necessary migrations.


598-600: LGTM: Added name sanitization for Homebridge v2 compatibility.

The code now properly sanitizes accessory names and logs both the original and sanitized versions for better debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant