fix(glances): use dedicated httpx client with longer timeout and surface API error messages#169689
Draft
serl wants to merge 2 commits intohome-assistant:devfrom
Draft
fix(glances): use dedicated httpx client with longer timeout and surface API error messages#169689serl wants to merge 2 commits intohome-assistant:devfrom
serl wants to merge 2 commits intohome-assistant:devfrom
Conversation
…ace API error messages The integration shared the global Home Assistant httpx client which defaults to a 5s timeout. On busy hosts a typical Glances fetch already takes 3.8-4.9s, so any momentary slowness tripped the timeout and the sensors went unavailable. Use create_async_httpx_client to get a dedicated client with a 30s timeout (mutating the shared client's timeout would affect every other integration). Also propagate the underlying GlancesApiError message into UpdateFailed so the coordinator's ERROR log line is no longer empty when a fetch fails. Closes home-assistant#169113 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Contributor
|
Hey there @engrbm87, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Glances integration to use its own httpx client with a longer timeout and to preserve underlying API error text in coordinator failures, so slow Glances instances stop flapping unavailable and users get actionable logs.
Changes:
- Switch Glances setup from the shared Home Assistant httpx client to a dedicated client configured with a 30-second timeout.
- Propagate
GlancesApiErrortext intoUpdateFailedso coordinator error logs are no longer empty. - Add a coordinator test that verifies update failures keep the original exception message.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
homeassistant/components/glances/__init__.py |
Creates the Glances API client with a dedicated httpx client and custom timeout during entry setup. |
homeassistant/components/glances/coordinator.py |
Preserves the original API exception text when converting fetch failures to UpdateFailed. |
homeassistant/components/glances/const.py |
Adds the new default timeout constant used by the integration client. |
tests/components/glances/test_init.py |
Adds coverage for the coordinator update-failure path and imported test helpers/constants. |
Comment on lines
+69
to
+70
| httpx_client = create_async_httpx_client( | ||
| hass, verify_ssl=entry_data[CONF_VERIFY_SSL], timeout=DEFAULT_TIMEOUT |
Comment on lines
+58
to
+67
| async def test_update_error_includes_message( | ||
| hass: HomeAssistant, | ||
| freezer: FrozenDateTimeFactory, | ||
| mock_api: MagicMock, | ||
| ) -> None: | ||
| """Test that the underlying API error message is propagated to UpdateFailed.""" | ||
| entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT) | ||
| entry.add_to_hass(hass) | ||
|
|
||
| await hass.config_entries.async_setup(entry.entry_id) |
… test Address PR review feedback: - Cache the per-verify_ssl httpx client in hass.data so config entry reloads reuse the same client instead of leaking a new one (and a new EVENT_HOMEASSISTANT_CLOSE handler) on every reload. - Add a regression test that asserts setup constructs the client with the configured DEFAULT_TIMEOUT and that a reload hits the cache (no second client created). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
The Glances integration was using the shared httpx client via
get_async_client, which is hardcoded to a 5-second timeout. On hosts where Glances takes more than 5 seconds to respond (reported in #169113 with worst-case fetches around 5 seconds), every refresh would time out and the integration would constantly flap between available and unavailable.This PR switches Glances to its own dedicated
create_async_httpx_client(hass, verify_ssl=..., timeout=DEFAULT_TIMEOUT).DEFAULT_TIMEOUTis set to 30 seconds — well below the 60-secondDEFAULT_SCAN_INTERVAL, and roughly 6x the user's reported worst-case fetch time.While investigating, a secondary bug surfaced:
coordinator.pydidraise UpdateFailed from err(no message), which causedDataUpdateCoordinator's ERROR log line to be empty whenever a fetch failed — making it very hard for users to diagnose the timeouts in the first place. Fixed by passingstr(err)toUpdateFailed.A new test (
test_update_error_includes_message) covers the coordinator update path (only initial setup was previously tested) and asserts the underlying error message is propagated toUpdateFailed.Type of change
Additional information
cc @engrbm87 (codeowner)
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: