Repository navigation
Send requests one at a time to keep one connection open per device - #642
Merged
Merged
Conversation
combined() gathers device, measurement, system and state concurrently. With a shared ClientSession without a per-host limit (as in Home Assistant) this opens several parallel keep-alive connections per poll. Energy Sockets only have about three client socket slots in total, so these idle connections block every new connection to the device, including the HTTPS token request needed to migrate to the v2 API. Serialize HomeWizardEnergyV1._request with the instance lock: - the lock covers reading the full response body, so the connection is back in the pool before the next request starts and is reused; - the lock is taken before the request timeout starts, so time spent queued behind other requests does not cause spurious timeouts; - the lock is taken per backoff attempt, so it is released while a failed request waits for its retry. Error handling and return values are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 client already took a lock around session.request, but: - it released the lock as soon as the response headers arrived, so the next request could start while the body was still being read and open a second connection to the device; - it took the lock inside the request timeout, so time spent queued behind other requests counted towards the timeout and could cause spurious RequestErrors and retries. Apply the same structure as v1: the lock is taken per backoff attempt before the timeout starts, and covers session/SSL context creation, the request and reading the full response body. Error handling and return values are unchanged. The tests run against a local plain-HTTP fake device by rewriting the request URL; TLS does not affect how requests are serialized. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #642 +/- ##
=======================================
Coverage 99.28% 99.28%
=======================================
Files 9 9
Lines 699 704 +5
Branches 70 70
=======================================
+ Hits 694 699 +5
Misses 2 2
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
Author
|
Also contributes to EMB-1474 |
Member
Author
Tested on hardwareI tested this on a real Energy Socket (firmware PR-601) with Home Assistant
"Migrate to v2" repair, end to end:
HA's own 🤖 Generated with Claude Code |
nvanofwegen
approved these changes
Nov 23, 2026
This was referenced Nov 25, 2026
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.
combined()gathers its requests at the same time. With a sharedClientSessionthat has no per-host limit, as in Home Assistant, each poll opens several parallel keep-alive connections to the device. An Energy Socket only has about three client socket slots in total (HTTP and HTTPS together), so these idle connections block every new connection. That includes the HTTPSPOST /api/usertoken request that Home Assistant's "migrate to v2" repair needs. Measured on firmware PR-601: with Home Assistant's v1 entry loaded, there were 3 open connections to port 80, and 20 out of 20 new connections were refused. The firmware side is tracked in EMB-1474.This PR makes both clients send one request at a time, so they reuse a single keep-alive connection:
_requestnow holds the instance lock (self._lock, which already existed but was unused in v1).asyncio.timeout, so time spent waiting in line counted toward a request's timeout.In both clients, the lock is now:
backoffattempt, so it's free while a failed request waits to retry;await resp.read()), so the connection is back in the pool before the next request starts. It also covers session creation, which removes a race, and in v2 the SSL context.Error handling and return values are unchanged, and all existing tests and snapshots pass.
Effect on Home Assistant
Tests
tests/v1/test_v1_request_serialization.pyandtests/v2/test_v2_request_serialization.pyrun against a localaiohttpserver. The server sends each response body in two halves with a pause between them, and records whether requests overlap and which client connections it saw. The tests check that:combined()and other calls never overlap and all use one connection;Each test fails against a broken version of
_request: the original code, a v2-style lock released after the headers, a lock taken inside the timeout, and a lock held through retries. The v2 tests use plain HTTP by rewriting thehttps://address in the test. TLS doesn't change how requests are queued.Not yet tested against real socket hardware.
🤖 Generated with Claude Code