Skip to content

BYD: Gast-Accounts (geteiltes Fahrzeug) unterstützen - #4359

Open
seaspotter wants to merge 3 commits into
openWB:masterfrom
seaspotter:byd-guest-user-type
Open

seaspotter wants to merge 3 commits into
openWB:masterfrom
seaspotter:byd-guest-user-type

Conversation

@seaspotter

@seaspotter seaspotter commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Seit dem 12.11.2026 beantwortet die BYD-Cloud die Echtzeit-Abfragen von Accounts, mit denen das Fahrzeug nur geteilt ist ("Familie und Freunde"), mit Code 1001. Das passiert, wenn die Anfrage kein signiertes userType: "1" enthält. In openWB steht dann "SoC kann nicht ausgelesen werden … Code 1001".

Das Verhalten und die Lösung stammen aus pyBYD: jkaberg/pyBYD#81 (betroffen laut jkaberg/hass-byd-vehicle#213 u. a. geteilte Accounts).

Änderungen:

  • Die Echtzeit-Abfragen senden bei Bedarf ein signiertes userType.
  • Das Modul erkennt den Account-Typ selbst: Bei Code 1001 wird einmal mit userType wiederholt, bei 8003 ohne. Das Ergebnis wird je Fahrzeug gemerkt.
  • Besitzer-Accounts bleiben unverändert.
  • Tests für die Wiederholung und die Signatur ergänzt.
  • Nicht gegen die echte BYD-Cloud (EU) getestet, nur mit Unit-Tests.

Summary by Sourcery

Support BYD shared-vehicle accounts in real-time data retrieval.

New Features:

  • Support BYD shared-vehicle guest accounts for real-time vehicle queries by automatically determining when a signed user type is required.

Bug Fixes:

  • Prevent shared-account real-time requests from failing with BYD error code 1001.

Enhancements:

  • Cache the detected account type per vehicle and endpoint, while allowing periodic revalidation and correcting incorrect account-type assumptions.

Tests:

  • Add unit tests covering signed user-type envelopes, account-type detection, retries, caching, error handling, and cache expiration.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="packages/modules/vehicles/byd/api.py" line_range="345" />
<code_context>
+            raise
+        except BydApiError as second:
+            if second.code == (GUEST_NOT_DECLARED_CODE if guest else NOT_A_GUEST_CODE):
+                _user_type_settled.add((*key, endpoint))
+                raise first from None
+            _guest_vins[key] = not guest
</code_context>
<issue_to_address>
**Changed access keeps failing**

When sharing permissions change after both user types were rejected and the endpoint was marked settled, `_user_type_settled` remains set, so `_post_vehicle_json` skips the alternate `userType` on later requests and the vehicle's SoC stays unavailable until the process restarts.

Clear or expire the settled marker when sharing permissions change so later requests can retry the alternate `userType`.

Also at `packages/modules/vehicles/byd/api.py:335`, `packages/modules/vehicles/byd/api.py:346`.
</issue_to_address>

### Comment 2
<location path="packages/modules/vehicles/byd/api.py" line_range="335" />
<code_context>
+        raise
+    except BydApiError as first:
+        wrong_user_type = NOT_A_GUEST_CODE if guest else GUEST_NOT_DECLARED_CODE
+        if first.code != wrong_user_type or (*key, endpoint) in _user_type_settled:
+            raise
+        retry_inner = {**inner, "random": secrets.token_hex(16).upper(),
</code_context>
<issue_to_address>
**Unsupported requests get misclassified as guest**

When an owner’s realtime endpoint returns `1001` because that endpoint is unsupported, and the retry returns an error other than the opposite-type code, `_post_vehicle_json` treats any `1001` as “guest not declared,” although `_post_token_json` also classifies `1001` as an unsupported-endpoint response. It retries with the opposite user type and can cache that guess from an unrelated retry error, so later requests use the wrong account type and callers see misleading or repeated failures.

Only infer and cache account type when the response is known to represent a user-type mismatch; otherwise preserve the original error without changing the cache.
</issue_to_address>

### Comment 3
<location path="packages/modules/vehicles/byd/api_test.py" line_range="178" />
<code_context>
+
+    @staticmethod
+    def _user_types(post_mock):
+        return [call.args[4] for call in post_mock.call_args_list]
+
+    def test_owner_is_asked_without_user_type_once(self, monkeypatch):
</code_context>
<issue_to_address>
**New BYD tests crash**

When the new `TestPostVehicleJson` tests reach `_user_types`, `TestPostVehicleJson` calls `_user_types`, whose `call.args[4]` raises `NameError` because `call` is not imported; the tests fail instead of checking the recorded user types.

Import `call` from `unittest.mock`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread packages/modules/vehicles/byd/api.py Resolved
Comment thread packages/modules/vehicles/byd/api.py Resolved
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.

1 participant