Repository: bimmerconnected/bimmer_connected
Pull request #535 (2023-05-16 18:38:01)

Add Interface required for China login
## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
Missed out on this: We need the ability to read & write `gcid` from HA.

## Type of change
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (which adds functionality to this library)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests

## Additional information
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #
- This PR is related to issue: 

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [ ] The code change is tested and works locally.
- [ ] Tests have been added to verify that the new code works.



Patch:
diff --git a/bimmer_connected/account.py b/bimmer_connected/account.py
index d588764..00e16bd 100644
--- a/bimmer_connected/account.py
+++ b/bimmer_connected/account.py
@@ -164,9 +164,10 @@ class MyBMWAccount:
         """Set the position of the observer for all vehicles."""
         self.config.observer_position = GPSPosition(latitude=latitude, longitude=longitude)
 
-    def set_refresh_token(self, refresh_token: str) -> None:
-        """Overwrite the current value of the MyBMW refresh token."""
+    def set_refresh_token(self, refresh_token: str, gcid: Optional[str] = None) -> None:
+        """Overwrite the current value of the MyBMW refresh token and GCID (if available)."""
         self.config.authentication.refresh_token = refresh_token
+        self.config.authentication.gcid = gcid
 
     def set_use_metric_units(self, use_metric_units: bool) -> None:
         """Change between using metric units (km, l) if True or imperial units (mi, gal) if False."""
@@ -194,6 +195,11 @@ class MyBMWAccount:
         """Returns the current refresh_token."""
         return self.config.authentication.refresh_token
 
+    @property
+    def gcid(self) -> Optional[str]:
+        """Returns the current GCID."""
+        return self.config.authentication.gcid
+
 
 @deprecated("MyBMWAccount")
 class ConnectedDriveAccount(MyBMWAccount):


Test patch:
diff --git a/test/test_account.py b/test/test_account.py
index fe86815..0383bcf 100644
--- a/test/test_account.py
+++ b/test/test_account.py
@@ -473,14 +473,24 @@ async def test_deprecated_account(caplog):
 @account_mock()
 @pytest.mark.asyncio
 async def test_refresh_token_getset():
-    """Test getting/setting the refresh_token."""
+    """Test getting/setting the refresh_token and gcid."""
     account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, TEST_REGION)
     assert account.refresh_token is None
     await account.get_vehicles()
     assert account.refresh_token == "another_token_string"
+    assert account.gcid is None
 
     account.set_refresh_token("new_refresh_token")
     assert account.refresh_token == "new_refresh_token"
+    assert account.gcid is None
+
+    account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, get_region_from_name("china"))
+    account.set_refresh_token("new_refresh_token", "dummy_gcid")
+    assert account.refresh_token == "new_refresh_token"
+    assert account.gcid == "dummy_gcid"
+    await account.get_vehicles()
+    assert account.refresh_token == "another_token_string"
+    assert account.gcid == "DUMMY"
 
 
 @pytest.mark.asyncio