Repository: bimmerconnected/bimmer_connected
Pull request #696 (2024-11-29 22:23:00)

Ensure refresh token is stored
## 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.
-->
If an exception occured (e.g. connection issue or remote service issue), a new `refresh_token` was not saved to disk in CLI.
This PR ensures that tokens are saved.

## Type of change
- [x] 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.
-->

- [x] The code change is tested and works locally.
- [x] Tests have been added to verify that the new code works.



Patch:
diff --git a/bimmer_connected/cli.py b/bimmer_connected/cli.py
index e554921..809e253 100644
--- a/bimmer_connected/cli.py
+++ b/bimmer_connected/cli.py
@@ -348,21 +348,20 @@ def main():
     except Exception as ex:  # pylint: disable=broad-except
         sys.stderr.write(f"{type(ex).__name__}: {ex}\n")
         sys.exit(1)
-
-    if args.disable_oauth_store:
-        return
-
-    args.oauth_store.parent.mkdir(parents=True, exist_ok=True)
-    args.oauth_store.write_text(
-        json.dumps(
-            {
-                "refresh_token": account.config.authentication.refresh_token,
-                "gcid": account.config.authentication.gcid,
-                "access_token": account.config.authentication.access_token,
-                "session_id": account.config.authentication.session_id,
-            }
-        ),
-    )
+    finally:
+        # Ensure that the OAuth2 tokens are stored even if an exception occurred
+        if not args.disable_oauth_store:
+            args.oauth_store.parent.mkdir(parents=True, exist_ok=True)
+            args.oauth_store.write_text(
+                json.dumps(
+                    {
+                        "refresh_token": account.config.authentication.refresh_token,
+                        "gcid": account.config.authentication.gcid,
+                        "access_token": account.config.authentication.access_token,
+                        "session_id": account.config.authentication.session_id,
+                    }
+                ),
+            )
 
 
 if __name__ == "__main__":
diff --git a/bimmer_connected/models.py b/bimmer_connected/models.py
index d2f8bf7..3683b66 100644
--- a/bimmer_connected/models.py
+++ b/bimmer_connected/models.py
@@ -157,11 +157,12 @@ class PointOfInterest:
         position = GPSPosition(lat, lon)
         self.position = {
             "lat": position.latitude,
-            "lon": position.longitude,
+            "lng": position.longitude,
         }
 
         self.address = PointOfInterestAddress(str(street), str(postal_code), str(city), str(country))
         self.category = {"losCategory": "Address", "mguVehicleCategoryId": None, "name": "Address"}
+        self.entrances = [{"name": None, "position": self.position}]
         self.title = name
 
         if not self.formattedAddress:


Test patch:
diff --git a/bimmer_connected/tests/common.py b/bimmer_connected/tests/common.py
index 5d4a774..1ffc517 100644
--- a/bimmer_connected/tests/common.py
+++ b/bimmer_connected/tests/common.py
@@ -331,7 +331,7 @@ class MyBMWMockRouter(respx.MockRouter):
             [
                 len(data["vehicleInformation"]["vin"]) == 17,
                 isinstance(data["places"][0]["position"]["lat"], float),
-                isinstance(data["places"][0]["position"]["lon"], float),
+                isinstance(data["places"][0]["position"]["lng"], float),
                 len(data["places"][0]["title"]) > 0,
             ]
         )
diff --git a/bimmer_connected/tests/test_remote_services.py b/bimmer_connected/tests/test_remote_services.py
index 89e6785..8d581f6 100644
--- a/bimmer_connected/tests/test_remote_services.py
+++ b/bimmer_connected/tests/test_remote_services.py
@@ -400,27 +400,27 @@ def test_poi_parsing():
     # Check parsing of attributes required by API
     poi_data = PointOfInterest(**POI_DATA)
     assert poi_data.position["lat"] == POI_DATA["lat"]
-    assert poi_data.position["lon"] == POI_DATA["lon"]
+    assert poi_data.position["lng"] == POI_DATA["lon"]
     assert poi_data.title == POI_DATA["name"]
     assert poi_data.formattedAddress == f"{POI_DATA['street']}, {POI_DATA['postal_code']}, {POI_DATA['city']}"
 
     # Check the default attributes
     poi_data = PointOfInterest(lat=POI_DATA["lat"], lon=POI_DATA["lon"])
     assert poi_data.position["lat"] == POI_DATA["lat"]
-    assert poi_data.position["lon"] == POI_DATA["lon"]
+    assert poi_data.position["lng"] == POI_DATA["lon"]
     assert poi_data.title == "Sent with ♥ by bimmer_connected"
     assert poi_data.formattedAddress == "Coordinates only"
 
     # Check the default attributes with formatted address
     poi_data = PointOfInterest(lat=POI_DATA["lat"], lon=POI_DATA["lon"], formattedAddress="Somewhere over rainbow")
     assert poi_data.position["lat"] == POI_DATA["lat"]
-    assert poi_data.position["lon"] == POI_DATA["lon"]
+    assert poi_data.position["lng"] == POI_DATA["lon"]
     assert poi_data.title == "Sent with ♥ by bimmer_connected"
     assert poi_data.formattedAddress == "Somewhere over rainbow"
 
     # Check parsing with numeric postal code
     poi_data = PointOfInterest(lat=POI_DATA["lat"], lon=POI_DATA["lon"], postal_code=1234)
     assert poi_data.position["lat"] == POI_DATA["lat"]
-    assert poi_data.position["lon"] == POI_DATA["lon"]
+    assert poi_data.position["lng"] == POI_DATA["lon"]
     assert poi_data.title == "Sent with ♥ by bimmer_connected"
     assert poi_data.address.postalCode == "1234"