Repository navigation
Only report v2 API support for officially supported models - #643
Conversation
has_v2_api returned True for any device that answers 401 on the unauthenticated https /api endpoint. Devices that enable the v2 API before it is officially supported (for example with beta firmware) were therefore treated as v2 devices, and Home Assistant offered to migrate them to the v2 API. The unauthenticated v2 endpoint does not tell which product it is, so read the product type from the device certificate instead. Its common name is `appliance/<product type>/<serial>` and it is validated against the HomeWizard CA, without authorization. Only models in the new SUPPORTED_V2_MODELS allowlist are reported as v2 capable: P1 Meter, Energy Socket, kWh Meters and Plug-In Battery. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the separate unauthenticated `GET /api` request: only v2 devices serve HTTPS, so a certificate that validates against the HomeWizard CA is enough to know the device runs the v2 API. This halves the number of connections, which matters because devices have very few connection slots. The websession argument is no longer used and only kept for backwards compatibility. Add an optional product_type argument for callers that already know it (discovery or an existing configuration). Unsupported models return False without connecting to the device, and a device of another product type at this host returns False as well. 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 #643 +/- ##
==========================================
+ Coverage 99.28% 99.32% +0.03%
==========================================
Files 9 9
Lines 704 741 +37
Branches 70 78 +8
==========================================
+ Hits 699 736 +37
Misses 2 2
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Tested against a real Energy SocketDevice: Energy Socket ( Certificate (
Effect on the device:
Not tested on other device types. The battery, P1 and kWh Meter certificate names come from the API docs and from 🤖 Generated with Claude Code |
Setup tested in Home Assistant, including the button pressSetup: a Home Assistant dev instance (
🤖 Generated with Claude Code |
hoefnageltom
left a comment
There was a problem hiding this comment.
Some things to take in consideration
…ates - Send GET /api over the same TLS connection and require 401 Unauthorized, as some firmware serves a valid certificate without the v2 API. - Accept certificates with `appliance/generic/<serial>`, as used by early batteries that do serve the v2 API, instead of reporting them as not v2. - Wait for the connection to close, ignoring errors while closing. - Only catch OSError (includes SSL errors and timeouts) instead of Exception. - Share the SSL context with HomeWizardEnergyV2 via create_ssl_context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review changes, tested against a P1 Meter, the Energy Socket and 4 batteriesCommit 4a659bd addresses the review:
Results with this commit (no mocks):
Finding: the battery with a generic certificate. One battery has Not tested: the old Socket firmware that has a certificate without the v2 API. It's covered by unit tests for 404, 200 and empty responses. Side note, not changed in this PR: for that battery, |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Follow-up: firmware 4.1101 and development unitsGeneric certificates. These are development units. 34a771a rewords the code comments, and the behaviour stays the same. I kept the support so development units keep working on v2 in Home Assistant: a battery has no v1 API to fall back to. Users don't have these units, so for them the check isn't any less strict. This corrects my earlier comment: the previous commit was only a regression for development units. Firmware 4.1101 (current external beta), on the same Energy Socket:
Still untested: firmware where HTTPS is running and the certificate is sent, but without the v2 handlers. For that case the check relies on |
Keep this branch's has_v2_api. It no longer sends the aiohttp request that #640 fixed the timeout of; the TLS connection and the GET /api response use asyncio.wait_for with a 5 second timeout instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rejected token Found by testing on a P1 Meter (firmware 6.0503): - The device closes the connection when the token is not accepted, instead of sending an error. This now raises UnauthorizedError - A failed connect left the session of the WebSocket open The Energy Socket gets the v2 API before it gets a WebSocket, so add SUPPORTED_WEBSOCKET_MODELS and Device.supports_websocket(), in line with SUPPORTED_V2_MODELS of #643. Connecting to another model raises UnsupportedError without opening a connection. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
has_v2_api()returned True for any device that answers401on the unauthenticatedhttps://<host>/api, without looking at the product type. So a device that enables the v2 API before it's officially supported (for example with beta firmware) was treated as a v2 device. Home Assistant then offered the "migrate to v2" repair, or went to the v2 authorize step during setup, for devices it can't handle on v2. This was seen with an Energy Socket on beta firmware and an older Home Assistant release.What changes
Allowlist: a new
SUPPORTED_V2_MODELSinconst.pylists the models this library supports on v2. It follows the device table in the API docs:HWE-P1)HWE-SKT), listed as "API support in beta" in Add documentation for the beta version of APIv2 for Energy Socket api-documentation#303HWE-KWH1,HWE-KWH3,SDM230-wifi,SDM630-wifi)HWE-BAT)Watermeter and Energy Display ("Planned" / "In development") are left out and keep using v1.
Product type from the certificate: the unauthenticated v2 API only answers
401, so the product type is read from the device's HTTPS certificate instead. Its common name isappliance/<type>/<serial>, the same name the v2 client already validates against (see the "SSL Certificate Validation" section of the docs). The certificate is validated against the HomeWizard CA, but the hostname isn't checked.One connection: the separate
GET /apirequest is gone. Only v2 devices serve HTTPS, so a valid certificate is enough. The check is now a single TLS handshake, which matters because devices have very few connection slots.websessionis still accepted, but no longer used.Optional
product_type: callers that already know the product type, from discovery or an existing config entry, can pass it. Unsupported models then return False without connecting to the device. A device of another product type at that address also returns False.Effect on Home Assistant
__init__.pyandasync_try_connectinconfig_flow.py) get the stricter check once HA bumps the library. HA's tests mockhas_v2_api.product_type, taken from the entry'sunique_idin the repair check and from the zeroconf properties in discovery.Tests
Unit tests cover the allowlist, an unknown or missing certificate type, connection and certificate errors,
product_type(supported, unsupported, mismatch), and parsing of the certificate subject.Checked manually against a local TLS server with a self-signed certificate (
CN=appliance/energysocket/…). With that certificate trusted, the type is read correctly. With the real HomeWizard CA, the certificate is rejected.Tested against a real Energy Socket on beta firmware with v2 enabled; details are in the test results comment. The certificate is
CN=appliance/energysocket/<serial>and validates against the library's CA.has_v2_apireturns True on its own and withproduct_type="HWE-SKT", and False for other product types. A Home Assistant instance kept polling the Socket without errors during the test. Other device types weren't tested on hardware.🤖 Generated with Claude Code