8000 Handle connection issues after websocket reconnected in homematicip_cloud by hahn-th · Pull Request #147731 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Handle connection issues after websocket reconnected in homematicip_cloud #147731

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

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

Conversation

hahn-th
Copy link
Contributor
@hahn-th hahn-th commented Jun 28, 2025

Proposed change

Fix an issue where the current state of entities is not updated when the WebSocket connection reconnects.

Bump homematicip to 2.0.7
Changes:

  • Raise HmipConnectionError, if REST-Request in get_state has an error
  • Raise HomeNotInitializedError, if get_state is called before the Home is initialized

Changelog: https://github.com/hahn-th/homematicip-rest-api/blob/master/CHANGELOG.md
Full diff: https://github.com/hahn-th/homematicip-rest-api/compare/2.0.6..2.0.7

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors how the HomematicIP HAP handles state updates after websocket reconnection by replacing the old _accesspoint_connected logic with a new retry loop and explicit task handling.

  • Remove _retry_task, _tries, and _accesspoint_connected in favor of a single _get_state_task
  • Introduce try_get_state to retry get_state with exponential backoff on HmipConnectionError
  • Update websocket handlers (ws_connected_handler, ws_disconnected_handler, ws_reconnected_handler) to use the new event and retry task
  • Adjust tests to use an AsyncMock home and patch AsyncHome.websocket_is_connected

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/components/homematicip_cloud/test_hap.py Added simple_mock_home, patched websocket_is_connected, and verified get_state is called
tests/components/homematicip_cloud/test_device.py Patched websocket_is_connected and invoked ws_connected_handler to simulate reconnection
homeassistant/components/homematicip_cloud/hap.py Refactored reconnection logic: added try_get_state, replaced flags with asyncio.Event
Comments suppressed due to low confidence (2)

homeassistant/components/homematicip_cloud/hap.py:267

  • [nitpick] The docstring could be clearer: specify that this handler sets the ws_connection_closed event when a reconnect attempt occurs.
        """Handle websocket reconnection. Is called when Websocket tries to reconnect."""

homeassistant/components/homematicip_cloud/hap.py:177

  • Add tests to verify the retry logic in try_get_state, including exponential backoff behavior on HmipConnectionError.
    async def try_get_state(self) -> None:

Comment on lines 204 to 207
future.result()
_LOGGER.info(
"Updating state after HMIP access point reconnect finished successfully",
)
Copy link
Preview
Copilot AI Jun 28, 2025

Choose a reason for hiding this comment

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

Wrap future.result() in a try/except to catch exceptions and prevent unhandled exceptions in the callback, handling errors gracefully.

Copilot uses AI. Check for mistakes.

@@ -196,8 +196,14 @@ async def test_hap_reconnected(
assert ha_state.state == STATE_UNAVAILABLE

mock_hap._accesspoint_connected = False
Copy link
Preview
Copilot AI Jun 28, 2025

Choose a reason for hiding this comment

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

The _accesspoint_connected attribute was removed from HomematicipHAP; update the test to manipulate hap._ws_connection_closed or hap.home.connected instead.

Suggested change
mock_hap._accesspoint_connected = False
mock_hap._ws_connection_closed = True

Copilot uses AI. Check for mistakes.

@@ -185,20 +174,37 @@ async def async_create_entity_lazy(self, is_device=True) -> None:
await asyncio.sleep(30)
await self.hass.config_entries.async_reload(self.config_entry.entry_id)

async def try_get_state(self) -> None:
Copy link
Preview
Copilot AI Jun 28, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider renaming try_get_state to _try_get_state to reflect its internal use and align with private method conventions.

Suggested change
async def try_get_state(self) -> None:
async def _try_get_state(self) -> None:

Copilot uses AI. Check for mistakes.

@hahn-th hahn-th marked this pull request as ready for review June 30, 2025 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HomematicIP Websocket connection to Cloud closed
1 participant
0