Skip to content

Wrap IPv6 hosts in brackets in request URLs - #646

Merged
lexpostma merged 1 commit into
mainfrom
fix-ipv6-host-urls
Dec 1, 2026
Merged

lexpostma merged 1 commit into
mainfrom
fix-ipv6-host-urls

Conversation

@lexpostma

Copy link
Copy Markdown
Member

The v1 and v2 clients build their request URLs as http://{host}/{path} and https://{host}{path}. When the host is an IPv6 address, that gives a URL like http://2001:db8::1/api, which isn't valid: aiohttp reads everything after the first colon as a port and raises InvalidUrlClientError. That's a ClientError, so callers only see the generic RequestError "Error occurred while communicating with the HomeWizard Energy device".

Home Assistant runs into this when zeroconf saves a P1 Meter's IPv6 address as the configured host: home-assistant/core#178629.

What changes

  • _url_host: a new property on the base class that wraps IPv6 addresses in brackets (RFC 3986) and returns IPv4 addresses and hostnames unchanged. The public host property still returns the address as given.
  • v1 and v2: both request URLs use _url_host. _get_v2_product_id already put brackets around IPv6 in its Host header, so it's unchanged.

Tests

  • A parametrised test of _url_host for IPv4, hostnames, .local names and IPv6 addresses.
  • For v1 and v2, a test that checks the exact URL passed to the session for an IPv6 host: http://[2001:db8::1]/api/v1/data and https://[2001:db8::1]/api.
  • Checked separately that aiohttp rejects the unbracketed URL with InvalidUrlClientError and accepts the bracketed one. I couldn't run it against a real device over IPv6.

🤖 Generated with Claude Code

The v1 and v2 clients built URLs as http://{host}/... and
https://{host}..., so an IPv6 address like 2001:db8::1 produced an
invalid URL. aiohttp then raised InvalidUrlClientError, which surfaced
as a RequestError. Add a _url_host property that brackets IPv6 literals
and use it for both API versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.33%. Comparing base (90dc57d) to head (913c57e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #646   +/-   ##
=======================================
  Coverage   99.32%   99.33%           
=======================================
  Files           9        9           
  Lines         741      751   +10     
  Branches       78       79    +1     
=======================================
+ Hits          736      746   +10     
  Misses          2        2           
  Partials        3        3           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lexpostma
lexpostma merged commit 25504e4 into main Dec 1, 2026
33 checks passed
@lexpostma
lexpostma deleted the fix-ipv6-host-urls branch December 1, 2026 08:10
@hoefnageltom hoefnageltom mentioned this pull request Dec 2, 2026
24 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants