Repository: bimmerconnected/bimmer_connected
Pull request #227 (2020-11-02 20:23:45)

Added vehicle position-heading
<!--
  Thanks for your contribution to bimmer_connected!
-->
## Breaking change
<!--
  If your PR contains a breaking change, please explain that change here.
  Note: Remove this section if this PR is NOT a breaking change.
-->


## 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.
-->


## Type of change
- [ ] Bugfix (non-breaking change which fixes an issue)
- [X] 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.
- [ ] Tests have been added to verify that the new code works.



Patch:
diff --git a/bimmer_connected/vehicle_status.py b/bimmer_connected/vehicle_status.py
index 19552ff..fae550c 100644
--- a/bimmer_connected/vehicle_status.py
+++ b/bimmer_connected/vehicle_status.py
@@ -136,13 +136,25 @@ class VehicleStatus:  # pylint: disable=too-many-public-methods
         Returns a tuple of (latitude, longitude).
         This only provides data, if the vehicle tracking is enabled!
         """
-        pos = self._state.attributes[SERVICE_STATUS]['position']
         if not self.is_vehicle_tracking_enabled:
-            _LOGGER.warning('Positioning status is %s', pos['status'])
+            _LOGGER.warning('Vehicle tracking is disabled')
             return None
         pos = self._state.attributes[SERVICE_STATUS]['position']
         return float(pos['lat']), float(pos['lon'])
 
+    @property
+    @backend_parameter
+    def gps_heading(self) -> (int):
+        """Get the last known heading of the vehicle.
+
+        This only provides data, if the vehicle tracking is enabled!
+        """
+        if not self.is_vehicle_tracking_enabled:
+            _LOGGER.warning('Vehicle tracking is disabled')
+            return None
+        pos = self._state.attributes[SERVICE_STATUS]['position']
+        return int(pos['heading'])
+
     @property
     @backend_parameter
     def is_vehicle_tracking_enabled(self) -> bool:


Test patch:
diff --git a/test/test_vehicle_status.py b/test/test_vehicle_status.py
index 1708772..d33d99e 100644
--- a/test/test_vehicle_status.py
+++ b/test/test_vehicle_status.py
@@ -37,6 +37,7 @@ class TestState(unittest.TestCase):
         self.assertTrue(state.vehicle_status.is_vehicle_tracking_enabled)
         self.assertAlmostEqual(50.5050, state.vehicle_status.gps_position[0])
         self.assertAlmostEqual(10.1010, state.vehicle_status.gps_position[1])
+        self.assertAlmostEqual(174, state.vehicle_status.gps_heading)
 
         self.assertAlmostEqual(33, state.vehicle_status.remaining_fuel)
 
@@ -68,6 +69,7 @@ class TestState(unittest.TestCase):
 
         self.assertFalse(state.vehicle_status.is_vehicle_tracking_enabled)
         self.assertIsNone(state.vehicle_status.gps_position)
+        self.assertIsNone(state.vehicle_status.gps_heading)
 
     def test_parse_f48(self):
         """Test if the parsing of the attributes is working."""
@@ -84,6 +86,7 @@ class TestState(unittest.TestCase):
         self.assertTrue(state.vehicle_status.is_vehicle_tracking_enabled)
         self.assertAlmostEqual(50.505050, state.vehicle_status.gps_position[0])
         self.assertAlmostEqual(10.1010101, state.vehicle_status.gps_position[1])
+        self.assertAlmostEqual(141, state.vehicle_status.gps_heading)
 
         self.assertAlmostEqual(39, state.vehicle_status.remaining_fuel)