-
-
Notifications
You must be signed in to change notification settings - Fork 36k
Lamarzocco fix websocket reconnect issue #156786
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
Lamarzocco fix websocket reconnect issue #156786
Conversation
Co-authored-by: zweckj <24647999+zweckj@users.noreply.github.com>
Co-authored-by: zweckj <24647999+zweckj@users.noreply.github.com>
There was a problem hiding this 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_terminatedfrom a boolean attribute to a property that checks the task'sdone()status - Added
_websocket_tasktracking 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_datacould 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_taskbefore 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)
edenhaus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @zweckj 👍
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
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: