Skip to content

Conversation

@weymann
Copy link
Member

@weymann weymann commented Aug 2, 2025

Beginning of July forum members reports issue that MercedesMe Account is going OFFLINE with HTTP 429 response for websocket. Issue doesn't recover by itsself! User needs to disbale Account for several hours to get it running again!

Since approx 2 weeks I recieved positive results, with some fixing of minor issues.

I vote to bring this PR into the next patch release to resolve this blockage

Root cause:
Mercedes introduced some throttling mechanism which prevents opening the websocket druing the day. Mechanism isn't clear but together with HA Mercedes developer we came with the following idea:

  • data is now polled with refreshInterval using REST API - unfrequent updates
  • only if driving or charging state is recognized open websocket - frequent updates
  • if charging / driving ended switch back to REST API calls

New authorizaion flow
On top of the throttling Mercedes introduced again a new quite complicated authorization method.

These two changes

  • update strategy plus
  • new authorization flow

are inside this PR. Other changes are refactoring to structure api in authorization, REST API and websocket.

@lsiepel lsiepel requested a review from Copilot August 2, 2025 19:35
@lsiepel lsiepel added the bug An unexpected problem or unintended behavior of an add-on label Aug 2, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements HTTP 429 handling and a new authorization flow for the MercedesMe binding to address throttling issues that cause accounts to go offline. The changes include a dynamic update strategy that switches between REST API polling and websocket connections based on vehicle activity, plus a completely new authorization flow using user credentials instead of refresh tokens.

Key changes:

  • Replaced refresh token authentication with email/password login flow
  • Implemented adaptive update strategy (REST polling vs websocket based on vehicle activity)
  • Restructured API architecture with separate Authorization, RestApi, and Websocket classes

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
bridge-config.xml Replaced refreshToken parameter with password parameter
AccountConfiguration.java Updated configuration to use password instead of refreshToken
AccountHandler.java Refactored to use new API structure and adaptive update strategy
Authorization.java New class implementing OAuth2 login flow with email/password
RestApi.java New class for REST API calls separated from websocket functionality
Websocket.java Refactored websocket handling extending RestApi
Utils.java Added query parameter parsing and updated server selection logic
Test files Updated to work with new authentication flow and API structure
Comments suppressed due to low confidence (1)

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/utils/Utils.java:616

  • [nitpick] The parameter name 'query' is ambiguous. Consider renaming it to 'queryString' to clearly indicate it represents the query portion of a URL.
    public static Map<String, String> getQueryParams(String query) {

Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the extensive fix. I have added a few comments.

I don't know if this was brought up before, but have you investigated if it might be possible to use the standard OAuth2 implementation in core? See https://www.openhab.org/javadoc/latest/org/openhab/core/auth/client/oauth2/package-summary

listener.onAccessTokenResponse(token);
} catch (InterruptedException | TimeoutException | ExecutionException | UnsupportedEncodingException
| JsonSyntaxException e) {
logger.info("Failed to refresh token {}", e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be propagated to the user as a Thing status description?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do at this location. If a data call or websocket start fails with invalid token AccountHandler tries to recover the situation with a new login. If this fails then it's time to present OFFLINE status.

Copy link
Contributor

@jlaur jlaur Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will be offline until it successfully recovers then? It could be offline with text "Refresh token expired, attempting relogin" or similar. This would make it more visible for users not checking logs, especially in cases where it doesn't succeed. Anyway, this is minor and you can also choose to leave it as is.

@weymann
Copy link
Member Author

weymann commented Aug 6, 2025

I don't know if this was brought up before, but have you investigated if it might be possible to use the standard OAuth2 implementation in core? See https://www.openhab.org/javadoc/latest/org/openhab/core/auth/client/oauth2/package-summary

Yes, it was used in early versions before Mercedes presented more and more fancy auth methods. Look into Authorization.java and you'll see 6 requests are needed to finally get a token. Mercedes drifted away from OAuth2 to make it as hard as possible to use the car data without using their app.

@weymann weymann force-pushed the mercedesme/authorization-429 branch from e6e489a to 65424b0 Compare August 7, 2025 15:09
weymann added 8 commits August 7, 2025 17:15
Signed-off-by: Bernd Weymann <[email protected]>

429 recovery initial version

Signed-off-by: Bernd Weymann <[email protected]>

switch to pull requests

Signed-off-by: Bernd Weymann <[email protected]>

new update strategy

Signed-off-by: Bernd Weymann <[email protected]>

new update strategy

Signed-off-by: Bernd Weymann <[email protected]>

community test version

Signed-off-by: Bernd Weymann <[email protected]>

fix copilot findings AccountHandler

Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Bernd Weymann <[email protected]>

catch JSON Exception decoding stored toekn

Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Bernd Weymann <[email protected]>

junit test adaptions

Signed-off-by: Bernd Weymann <[email protected]>

junit test adaptions

Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Bernd Weymann <[email protected]>
@weymann weymann force-pushed the mercedesme/authorization-429 branch from 65424b0 to caf174e Compare August 7, 2025 15:16
@weymann weymann requested a review from jlaur August 11, 2025 18:27
@weymann
Copy link
Member Author

weymann commented Aug 11, 2025

@jlaur just check the last open comment and if you're fine with that let's merge.

Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. I resolved two very minor comments so this can make it into the upcoming 5.0.1 patch release.

@jlaur jlaur changed the title [mercedesme] HTTP 429 handling and new authorization flow [mercedesme] Improve HTTP 429 handling and implement new authorization flow Aug 18, 2025
@jlaur jlaur merged commit 5bab3f4 into openhab:main Aug 18, 2025
2 checks passed
@jlaur jlaur added this to the 5.1 milestone Aug 18, 2025
jlaur pushed a commit that referenced this pull request Aug 18, 2025
@jlaur jlaur added the backported A PR that has been cherry-picked to a patch release branch label Aug 18, 2025
@weymann weymann deleted the mercedesme/authorization-429 branch August 29, 2025 14:27
rogrun pushed a commit to rogrun/openhab-addons that referenced this pull request Sep 3, 2025
rogrun pushed a commit to rogrun/openhab-addons that referenced this pull request Sep 3, 2025
cipianpascu pushed a commit to cipianpascu/openhab-addons that referenced this pull request Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported A PR that has been cherry-picked to a patch release branch bug An unexpected problem or unintended behavior of an add-on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants