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

fix: handle missing QR code #65

Merged
merged 4 commits into from
Feb 3, 2025
Merged

fix: handle missing QR code #65

merged 4 commits into from
Feb 3, 2025

Conversation

mikejgray
Copy link
Collaborator

@mikejgray mikejgray commented Jan 25, 2025

chore: allow newer versions of websockets
fix: increase websocket max size to 5MB from 1MB

Closes numerous issues ahead of merging the plugin and skill.

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration property for maximum WebSocket message size (5 MB).
    • Improved error handling for Home Assistant QR code login.
  • Dependency Updates

    • Updated websockets package version constraint.
    • Added websocket-client package.
  • Bug Fixes

    • Enhanced OAuth flow error reporting.
    • Resolved potential WebSocket connection limitations.

If the QR code is not successfully generated, send the user a notification. A notification is chosen for persistence, otherwise text on the screen may be lost on boot.

Closes #49
Copy link

coderabbitai bot commented Jan 25, 2025

Walkthrough

The pull request introduces changes to the Home Assistant plugin for OVOS, focusing on enhancing error handling and WebSocket connection capabilities. Key modifications include the addition of a max_ws_message_size property set to 5 MB, which is utilized in connection setups, and improved error handling for QR code retrieval during the OAuth process. Additionally, the requirements.txt file has been updated to reflect new dependency constraints and the inclusion of a new package.

Changes

File Change Summary
ovos_PHAL_plugin_homeassistant/__init__.py Added max_ws_message_size property and improved error handling for QR code retrieval in the OAuth flow.
ovos_PHAL_plugin_homeassistant/logic/socketclient.py Updated constructor to include max_ws_message_size parameter and modified _connect method to utilize this parameter. Enhanced error handling in the authenticate method.
requirements.txt Updated websockets version constraint to <14.0 and added websocket-client dependency with version >=0.54.0,<2.0.0.

Sequence Diagram

sequenceDiagram
    participant User
    participant Plugin
    participant HomeAssistant
    participant WebSocket

    User->>Plugin: Initiate OAuth
    Plugin->>HomeAssistant: Request QR Code
    alt QR Code Retrieved Successfully
        HomeAssistant-->>Plugin: QR Code URL
        Plugin->>User: Display QR Code
    else QR Code Retrieval Fails
        HomeAssistant-->>Plugin: No QR Code
        Plugin->>User: Notify Failure
    end
    Plugin->>WebSocket: Connect (Max Size: 5MB)
    WebSocket-->>Plugin: Connection Established
Loading

Poem

🐰 A Rabbit's Websocket Tale 🌐

With QR codes and connection might,
Our plugin dances, error-free and bright.
Five megabytes of data flow,
Through websockets, smooth and slow.
Home Assistant, we embrace with glee! 🏡


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 91f4585 and 9a3bb17.

📒 Files selected for processing (3)
  • README.md (2 hunks)
  • ovos_PHAL_plugin_homeassistant/__init__.py (3 hunks)
  • ovos_PHAL_plugin_homeassistant/logic/socketclient.py (3 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
ovos_PHAL_plugin_homeassistant/__init__.py

257-257: SyntaxError: Expected ',', found name


264-264: SyntaxError: Expected ',', found name

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: license_tests / license_tests
🔇 Additional comments (6)
ovos_PHAL_plugin_homeassistant/logic/socketclient.py (3)

12-16: LGTM! Constructor changes look good.

The addition of the max_ws_message_size parameter with a default of 5MB aligns with the PR objective to increase the websocket message size limit.


33-35: Good addition of connection validation!

The check for websocket connection before attempting authentication helps prevent potential errors and provides better error logging.


53-53: LGTM! WebSocket connection setup looks good.

The websocket connection is correctly configured to use the max_ws_message_size parameter.

ovos_PHAL_plugin_homeassistant/__init__.py (2)

169-176: LGTM! Property implementation looks good.

The max_ws_message_size property is well-documented and correctly retrieves the value from config with the appropriate default.


897-899: Good addition of QR code error handling!

The check for missing QR code and user notification improves error handling and user experience.

README.md (1)

65-65: LGTM! Documentation is clear and accurate.

The max_ws_message_size configuration option is well-documented with the correct default value and usage example.

Also applies to: 77-77

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 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.

@mikejgray
Copy link
Collaborator Author

@coderabbitai review

Copy link

coderabbitai bot commented Jan 26, 2025

✅ 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 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 (1)
ovos_PHAL_plugin_homeassistant/__init__.py (1)

886-888: LGTM! Consider adding error details to the notification.

Good addition of error handling for missing QR codes. The notification helps users understand when the login process fails.

Consider including more specific error details in the notification message:

-            self.gui.show_notification("Failed to get QR code for Home Assistant login!")
+            self.gui.show_notification(f"Failed to get QR code for Home Assistant login: {message.data.get('error', 'Unknown error')}")
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c63a179 and 91f4585.

📒 Files selected for processing (3)
  • ovos_PHAL_plugin_homeassistant/__init__.py (1 hunks)
  • ovos_PHAL_plugin_homeassistant/logic/socketclient.py (1 hunks)
  • requirements.txt (1 hunks)
🔇 Additional comments (2)
ovos_PHAL_plugin_homeassistant/logic/socketclient.py (1)

49-49: LGTM! Consider monitoring message sizes.

The increase in WebSocket message size to 5MB is reasonable and aligns with the requirements updates. This should help prevent potential message truncation issues.

Consider monitoring actual message sizes in production to ensure this limit is appropriate:

requirements.txt (1)

8-8: LGTM! Well-documented version constraints.

The version constraints are appropriate and the comments clearly explain the reasoning behind them:

  • Avoiding websockets 14.0 due to breaking changes
  • Adding websocket-client with Python 3 compatible versions

Also applies to: 11-11

Copy link
Member

@NeonDaniel NeonDaniel left a comment

Choose a reason for hiding this comment

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

Tested and confirmed this resolves an error in the oauth flow

@NeonDaniel NeonDaniel self-requested a review January 29, 2025 01:32
@NeonDaniel
Copy link
Member

Approved too soon. Found these errors in logs after authentication finished:

2025-01-28 17:26:46.863 - enclosure - ovos_PHAL_plugin_homeassistant.logic.socketclient:_connect:73 - ERROR - sent 1000 (OK); then received 1000 (OK)
Traceback (most recent call last):
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 59, in _connect
    await self.authenticate()
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 40, in authenticate
    await self.listen()
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 84, in listen
    message = await self.websocket.recv()
  File "/home/neon/venv/lib/python3.10/site-packages/websockets/legacy/protocol.py", line 568, in recv
    await self.ensure_open()
  File "/home/neon/venv/lib/python3.10/site-packages/websockets/legacy/protocol.py", line 948, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: sent 1000 (OK); then received 1000 (OK)
2025-01-28 17:26:46.866 - enclosure - ovos_PHAL_plugin_homeassistant.logic.socketclient:run:258 - INFO - self.loop.is_running=False
2025-01-28 17:26:49.290 - enclosure - ovos_PHAL_plugin_homeassistant.logic.socketclient:_connect:73 - ERROR - sent 1000 (OK); then received 1000 (OK)
Traceback (most recent call last):
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 59, in _connect
    await self.authenticate()
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 40, in authenticate
    await self.listen()
  File "/home/neon/venv/lib/python3.10/site-packages/ovos_PHAL_plugin_homeassistant/logic/socketclient.py", line 84, in listen
    message = await self.websocket.recv()
  File "/home/neon/venv/lib/python3.10/site-packages/websockets/legacy/protocol.py", line 568, in recv
    await self.ensure_open()
  File "/home/neon/venv/lib/python3.10/site-packages/websockets/legacy/protocol.py", line 948, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: sent 1000 (OK); then received 1000 (OK)
2025-01-28 17:26:49.292 - enclosure - ovos_PHAL_plugin_homeassistant.logic.socketclient:run:258 - INFO - self.loop.is_running=False

@mikejgray
Copy link
Collaborator Author

@NeonDaniel I made the max websocket message size configurable with max_ws_message_size. We spoke privately about exposing fewer entities to Assist, so hopefully a combination of this change and reducing the exposed entities will work.

Copy link
Member

@NeonDaniel NeonDaniel left a comment

Choose a reason for hiding this comment

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

Set that max size to 10x as a quick test and can confirm that exception is no longer raised 🎉

@mikejgray mikejgray merged commit d3c58b3 into dev Feb 3, 2025
4 checks passed
@NeonDaniel NeonDaniel mentioned this pull request Feb 6, 2025
@coderabbitai coderabbitai bot mentioned this pull request Feb 7, 2025
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.

2 participants