fix(login): try new PMS login URL with fallback to legacy URL#38
Open
blueleo076 wants to merge 3 commits into
Open
fix(login): try new PMS login URL with fallback to legacy URL#38blueleo076 wants to merge 3 commits into
blueleo076 wants to merge 3 commits into
Conversation
Newer LG ESS Home firmware (PMS 10.05.7438 and above) exposes the login endpoint at /v1/login and the timesync endpoint at /v1/timesync instead of the original /v1/user/setting/login and /v1/user/setting/timesync. Add LOGIN_URLS and TIMESYNC_URLS as ordered lists so aio_ess can try the new endpoint first and fall back to the legacy one when the device still speaks the old API. Keep the module-level LOGIN_URL and TIMESYNC_URL constants pointing at the legacy URLs for backward compatibility with callers that import them directly. Refs: gluap#37
On LG ESS Home PMS firmware 10.05.7438 and above, the device only exposes PUT /v1/login, not the legacy PUT /v1/user/setting/login. Previously the library hard-coded the legacy path, so every login attempt on current firmware got a 404 from the device which the client surfaced as a misleading 'wrong password' error. Walk the candidate LOGIN_URLS list at runtime, stop at the first URL that does not return 404, and pin the working URL on the client for the rest of the connection's lifetime. Treat TimeSync 404s the same way: the new firmware does not implement the endpoint at all, so a 404 there is no longer a hard failure that triggers the historical exponential-backoff retry loop. Other TimeSync errors (5xx, malformed body, status != success) still fall back to the existing retry behaviour. Also accept the new firmware's 'auth' field as an alternative to the legacy 'auth_key' response field, and make ESS.create / ESS.__init__ accept an optional pre-built aiohttp ClientSession so the new behaviour can be exercised by integration tests. Fixes: gluap#37
Six new tests in tests/aio_ess_endpoint_fallback_test.py:
* test_new_firmware_login_succeeds
Device exposes only /v1/login, returns 200 + auth_key. Client
must pick the new URL, ignore the legacy 404, and complete login.
* test_legacy_firmware_login_succeeds
Device exposes only /v1/user/setting/login. Client must skip the
404 on /v1/login and fall back to the legacy path.
* test_wrong_password_raises_auth_exception
Both URLs return 200 + password_mismatched. Client must raise
ESSAuthException, not retry forever, not raise the generic
ESSException.
* test_no_endpoint_found_raises_clear_exception
Both URLs return 404. Client must raise ESSException whose
message lists the URLs tried, and must NOT say 'wrong password'.
* test_new_firmware_timesync_404_does_not_loop
Regression test for the historical tight-retry loop that used to
fill Home Assistant logs when the device's TimeSync endpoint was
not implemented.
* test_url_lists_priority_order
Sanity check: the new URL is listed first in LOGIN_URLS, the
legacy LOGIN_URL / TIMESYNC_URL module-level constants still
exist and point at the legacy URLs (backward compatibility).
The tests use aiohttp.test_utils.TestServer in-process and
monkey-patch pyess.constants.LOGIN_URLS / TIMESYNC_URLS to point at
the local server, exercising the real HTTP path of the fallback
logic without needing a cassette or a real LG device on the network.
Refs: gluap#37
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.
Summary
On LG ESS Home PMS firmware 10.05.7438 and above, LG has shortened the auth endpoints:
PUT /v1/user/setting/loginPUT /v1/loginPUT /v1/user/setting/timesync(200)PUT /v1/timesync(404 — not implemented on this build)pyesspreviously hard-coded the legacy paths. On current firmware, the device returns 404 for the old URL, which the client mis-surfaced asESSAuthException("wrong password")— the password was always fine. The TimeSync 404 also triggered a tightJSONDecodeError → retry-loginloop that filled Home Assistant logs.This PR makes the login walk an ordered
LOGIN_URLSlist (new URL first, legacy as fallback), pins the working URL on the client, and treats a TimeSync 404 as "device has no time sync — skipping is safe" instead of as a fatal error.Changes
pyess/constants.py— addLOGIN_URLSandTIMESYNC_URLSordered lists. Keep module-levelLOGIN_URL/TIMESYNC_URLpointing at the legacy URLs so direct imports keep working.pyess/aio_ess.py_login()walksLOGIN_URLSin order, stops at the first non-404 response, pins the working URL on the client.authfield as an alternative to the legacyauth_keyfield.ESSExceptionwith a clear "tried: [...]" message instead of the misleading "wrong password".status != success) still fall back to the existing exponential-backoff retry.ESS.createandESS.__init__now accept an optional pre-builtaiohttp.ClientSessionso the fallback logic is testable in isolation.tests/aio_ess_endpoint_fallback_test.py— six new in-process tests usingaiohttp.test_utils.TestServer:/v1/loginanswers, legacy 404s)./v1/user/setting/loginanswers).ESSAuthException, not the genericESSException.ESSExceptionwith the URL list, not "wrong password".LOGIN_URL/TIMESYNC_URLback-compat.Test plan
pip install -e ".[test]" pip install pytest-aiohttp pytest tests/aio_ess_endpoint_fallback_test.py -vLocal result on Python 3.11:
Risk
ESS.create(name, password, ip)still works without the new optionalsessionparameter; existing imports ofLOGIN_URL/TIMESYNC_URLfrompyess.constantsstill resolve to the legacy URLs.Verification on a real device
The maintainer's comment on issue #37 confirmed their own inverter runs PMS 10.05.6511 (the legacy path), which the new fallback handles. The new-path behaviour is verified end-to-end against the in-process test server in this PR and against the user's PMS 10.05.7438 device described in #37 (whose
curlconfirmation is what identified the URL change in the first place).Closes #37