Skip to content

Conversation

@zweckj
Copy link
Member

@zweckj zweckj commented Nov 18, 2025

Breaking change

Proposed change

Use a property instead of an attribute to check the websocket status and also check that the task is terminated to fix an issue where during an internet outage the task would not restart as expected. Also remove a duplicate test fixture.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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:

Copilot AI and others added 4 commits November 18, 2025 06:31
Co-authored-by: zweckj <24647999+zweckj@users.noreply.github.com>
Co-authored-by: zweckj <24647999+zweckj@users.noreply.github.com>
@zweckj zweckj added this to the 2025.11.3 milestone Nov 18, 2025
Copilot AI review requested due to automatic review settings November 18, 2025 08:21
@zweckj zweckj changed the title Copilot/fix websocket reconnect issue Lamarzocco fix websocket reconnect issue Nov 18, 2025
Copilot finished reviewing on behalf of zweckj November 18, 2025 08:23
Copy link
Contributor

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 fixes a websocket reconnection issue during internet outages by converting the websocket_terminated attribute to a property that checks if the background task is actually done. The fix ensures the coordinator can properly detect when the websocket task has terminated and needs to be restarted.

Key changes:

  • Converted websocket_terminated from a boolean attribute to a property that checks the task's done() status
  • Added _websocket_task tracking to monitor the background task state
  • Enhanced websocket reconnection condition to check both connection status and task state
  • Added test coverage for the reconnection scenario after task termination

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
homeassistant/components/lamarzocco/coordinator.py Changed websocket_terminated from attribute to property checking task status, added _websocket_task tracking, updated reconnection logic
tests/components/lamarzocco/conftest.py Updated mock_websocket_terminated fixture to use PropertyMock, added websocket mocks to mock_lamarzocco fixture
tests/components/lamarzocco/test_binary_sensor.py Removed duplicate fixture definition, updated test to use shared fixture from conftest
tests/components/lamarzocco/test_init.py Added new test for websocket reconnection after task termination, imported necessary utilities
Comments suppressed due to low confidence (2)

homeassistant/components/lamarzocco/coordinator.py:117

  • Multiple calls to _internal_async_update_data could create multiple background tasks without canceling the previous one. If the websocket reconnection logic is triggered multiple times rapidly (e.g., during network instability), this could lead to multiple concurrent websocket tasks. Consider canceling the existing _websocket_task before creating a new one if it exists and is not done.
        self._websocket_task = self.config_entry.async_create_background_task(
            hass=self.hass,
            target=self.connect_websocket(),
            name="lm_websocket_task",
        )

homeassistant/components/lamarzocco/coordinator.py:125

  • The cleanup handlers are registered every time a websocket connection is attempted. This means if the websocket disconnects and reconnects multiple times, multiple cleanup handlers accumulate. Consider registering these handlers only once during coordinator initialization, or tracking whether they've already been registered to avoid duplication.
        self.config_entry.async_on_unload(
            self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, websocket_close)
        )
        self.config_entry.async_on_unload(websocket_close)

Copy link
Member

@edenhaus edenhaus left a comment

Choose a reason for hiding this comment

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

Thanks @zweckj 👍

@edenhaus edenhaus merged commit b8b101d into home-assistant:dev Nov 19, 2025
41 of 42 checks passed
@zweckj zweckj deleted the copilot/fix-websocket-reconnect-issue branch November 19, 2025 12:12
@github-actions github-actions bot locked and limited conversation to collaborators Nov 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants